2 * Copyright (c) 2017, Fuzhou Rockchip Electronics Co., Ltd
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 #include <linux/clk.h>
15 #include <linux/delay.h>
16 #include <linux/interrupt.h>
18 #include <linux/module.h>
20 #include <linux/of_address.h>
21 #include <linux/of_irq.h>
22 #include <linux/platform_device.h>
23 #include <linux/reset.h>
24 #include <linux/thermal.h>
25 #include <linux/timer.h>
26 #include <linux/mfd/syscon.h>
27 #include <linux/regmap.h>
28 #include <linux/gpio.h>
29 #include <linux/of_gpio.h>
30 #include <linux/rockchip/common.h>
31 #include <linux/reboot.h>
32 #include <linux/regulator/consumer.h>
33 #include <linux/slab.h>
34 #include <linux/mutex.h>
35 #include <linux/nvmem-consumer.h>
38 * If the temperature over a period of time High,
39 * the resulting TSHUT gave CRU module,let it reset the entire chip,
40 * or via GPIO give PMIC.
53 * the system Temperature Sensors tshut(tshut) polarity
54 * the bit 8 is tshut polarity.
55 * 0: low active, 1: high active
64 /* TSADC V2 Sensor info define: */
65 #define TSADCV2_USER_CON 0x00
66 #define TSADCV2_AUTO_CON 0x04
67 #define TSADCV2_INT_EN 0x08
68 #define TSADCV2_INT_PD 0x0c
69 #define TSADCV2_DATA(chn) (0x20 + (chn) * 0x04)
71 #define TSADC_CLK_CYCLE_TIME 32 /* usec */
72 #define TSADCV3_DATA_MASK 0x3ff
75 * The conversion table has the adc value and temperature.
76 * ADC_DECREMENT: the adc value is of diminishing.(e.g. rk3288_code_table)
77 * ADC_INCREMENT: the adc value is incremental.(e.g. rk3368_code_table)
84 #define TIME_OUT_TOTAL 2000
85 #define INVALID_EFUSE_VALUE 0xff
91 #define MIN_TEMP (-40000)
92 #define MAX_TEMP (125000)
95 #define BASE_SHIFT (10)
96 #define START_BOUNDING_COUNT (100)
97 #define HIGHER_BOUNDING_TEMP (30)
98 #define LOWER_BOUNDING_TEMP (15)
101 * struct tsadc_table - hold information about code and temp mapping
102 * @code: raw code from tsadc ip
103 * @temp: the mapping temperature
112 * struct chip_tsadc_table - hold information about chip-specific differences
113 * @id: conversion table
114 * @length: size of conversion table
115 * @data_mask: mask to apply on data inputs
116 * @mode: sort mode of this adc variant (incrementing or decrementing)
118 struct chip_tsadc_table {
119 const struct tsadc_table *id;
122 enum adc_sort_mode mode;
126 * struct rk3368_tsadc_chip - hold the private data of tsadc chip
127 * @chn_id[SOC_MAX_SENSORS]: the sensor id of chip correspond to the channel
128 * @chn_num: the channel number of tsadc chip
129 * @tshut_temp: the hardware-controlled shutdown temperature value
130 * @tshut_mode: the hardware-controlled shutdown mode (0:CRU 1:GPIO)
131 * @tshut_polarity: the hardware-controlled active polarity (0:LOW 1:HIGH)
132 * @chip_tsadc_table: the chip-specific conversion table
133 * @get_temp: get the temperature
134 * @set_alarm_temp: set the high temperature interrupt
135 * @set_tshut_temp: set the hardware-controlled shutdown temperature
136 * @set_tshut_mode: set the hardware-controlled shutdown mode
138 struct rk3368_tsadc_chip {
139 int chn_id[NUM_SENSORS];
142 enum tshut_mode tshut_mode;
143 enum tsadc_mode mode;
144 enum tshut_polarity tshut_polarity;
146 const struct chip_tsadc_table *temp_table;
148 /* Per-sensor methods */
149 int (*get_temp)(const struct chip_tsadc_table *table,
150 int chn, void __iomem *reg, int *temp);
151 void (*set_alarm_temp)(const struct chip_tsadc_table *table,
152 int chn, void __iomem *reg, int temp);
153 void (*set_tshut_temp)(const struct chip_tsadc_table *table,
154 int chn, void __iomem *reg, int temp);
155 void (*set_tshut_mode)(int chn, void __iomem *reg, enum tshut_mode m);
159 * struct rk3368_thermal_sensor - hold the information of thermal sensor
160 * @ctx: pointer to the platform/configuration data
161 * @tzd: pointer to a thermal zone
162 * @id: identifier of the thermal sensor
164 struct rk3368_thermal_sensor {
165 struct rk3368_thermal_data *ctx;
166 struct thermal_zone_device *tzd;
171 * struct rk3368_thermal_data - hold the private data of thermal driver
172 * @chip: pointer to the platform/configuration data
173 * @pdev: platform device of thermal
174 * @reset: the reset controller of tsadc
175 * @sensors[SOC_MAX_SENSORS]: the thermal sensor
176 * @clk: the controller clock is divided by the external 24MHz
177 * @pclk: the advanced peripherals bus clock
178 * @regs: the base address of tsadc controller
179 * @tshut_temp: the hardware-controlled shutdown temperature value
180 * @tshut_mode: the hardware-controlled shutdown mode (0:CRU 1:GPIO)
181 * @tshut_polarity: the hardware-controlled active polarity (0:LOW 1:HIGH)
182 * @cpu_temp_adjust: efuse value used to ajust the temperature
183 * @gpu_temp_adjust: efuse value used to ajust the temperature
184 * @cpu_temp: the current cpu's temperature
185 * @logout: switch to control log output or not
186 * @rk3368_thermal_kobj: node in sys fs
188 struct rk3368_thermal_data {
189 const struct rk3368_tsadc_chip *chip;
190 struct platform_device *pdev;
191 struct reset_control *reset;
193 struct rk3368_thermal_sensor sensors[NUM_SENSORS];
200 enum tshut_mode tshut_mode;
201 enum tshut_polarity tshut_polarity;
207 struct kobject *rk3368_thermal_kobj;
208 struct regulator *ref_regulator;
210 struct notifier_block tsadc_nb;
213 static struct rk3368_thermal_data *thermal_ctx;
215 static DEFINE_MUTEX(thermal_reg_mutex);
217 static const struct tsadc_table code_table_3368[] = {
253 {TSADCV3_DATA_MASK, MAX_TEMP},
256 static const struct chip_tsadc_table tsadc_table_3368 = {
257 .id = code_table_3368,
258 .length = ARRAY_SIZE(code_table_3368),
259 .data_mask = TSADCV3_DATA_MASK,
260 .mode = ADC_INCREMENT,
263 static int rk3368_get_ajust_code(struct device_node *np, u8 *ajust_code)
265 struct nvmem_cell *cell;
269 cell = of_nvmem_cell_get(np, "temp_adjust");
271 pr_err("avs failed to get temp_adjust cell\n");
272 return PTR_ERR(cell);
275 buf = (unsigned char *)nvmem_cell_read(cell, &len);
277 nvmem_cell_put(cell);
282 if (buf[0] == INVALID_EFUSE_VALUE)
285 *ajust_code = buf[0];
291 static struct rk3368_thermal_data *rk3368_thermal_get_data(void)
293 WARN_ON(!thermal_ctx);
297 static int rk3368_temp_to_code(const struct chip_tsadc_table *tmp_table,
298 long temp, u32 *code)
300 unsigned int low = 1;
301 unsigned int high = tmp_table->length - 1;
302 unsigned int mid = (low + high) / 2;
305 *code = tmp_table->data_mask;
307 WARN_ON(tmp_table->length < 2);
309 if (temp < tmp_table->id[low].temp)
310 return -EAGAIN; /* Incorrect reading */
312 while (low <= high) {
313 if (temp == tmp_table->id[mid].temp) {
314 *code = tmp_table->id[mid].code;
316 } else if (temp > tmp_table->id[mid].temp) {
322 mid = (low + high) / 2;
325 * The 5C granularity provided by the table is too much. Let's
326 * assume that the relationship between sensor readings and
327 * temperature between 2 table entries is linear and interpolate
328 * to produce less granular result.
330 if (*code == tmp_table->data_mask) {
331 num = abs(tmp_table->id[low].code - tmp_table->id[high].code);
332 num *= abs(tmp_table->id[high].temp - temp);
333 denom = abs(tmp_table->id[high].temp - tmp_table->id[low].temp);
334 *code = tmp_table->id[high].code + (num / denom);
340 static int rk3368_code_to_temp(const struct chip_tsadc_table *tmp_table,
343 unsigned int low = 1;
344 unsigned int high = tmp_table->length - 1;
345 unsigned int mid = (low + high) / 2;
348 *temp = INVALID_TEMP;
350 WARN_ON(tmp_table->length < 2);
352 switch (tmp_table->mode) {
354 code &= tmp_table->data_mask;
355 if (code < tmp_table->id[high].code)
356 return -EAGAIN; /* Incorrect reading */
358 while (low <= high) {
359 if (code == tmp_table->id[mid].code) {
360 *temp = tmp_table->id[mid].temp;
362 } else if (code < tmp_table->id[mid].code) {
368 mid = (low + high) / 2;
372 code &= tmp_table->data_mask;
373 if (code < tmp_table->id[low].code)
374 return -EAGAIN; /* Incorrect reading */
376 while (low <= high) {
377 if (code == tmp_table->id[mid].code) {
378 *temp = tmp_table->id[mid].temp;
380 } else if (code > tmp_table->id[mid].code) {
386 mid = (low + high) / 2;
390 pr_err("Invalid the conversion table\n");
394 * The 5C granularity provided by the table is too much. Let's
395 * assume that the relationship between sensor readings and
396 * temperature between 2 table entries is linear and interpolate
397 * to produce less granular result.
399 if (*temp == INVALID_TEMP) {
400 num = abs(tmp_table->id[low].temp - tmp_table->id[high].temp);
401 num *= abs(tmp_table->id[high].code - code);
402 denom = abs(tmp_table->id[high].code - tmp_table->id[low].code);
403 *temp = tmp_table->id[high].temp + (num / denom);
409 static const struct rk3368_tsadc_chip rk3368_tsadc_data = {
410 .tshut_mode = TSHUT_MODE_GPIO, /* default TSHUT via GPIO give PMIC */
411 .tshut_polarity = TSHUT_LOW_ACTIVE, /* default TSHUT LOW ACTIVE */
412 .hw_shut_temp = 125000,
413 .mode = TSHUT_USER_MODE,
417 .temp_table = &tsadc_table_3368,
420 static int rk3368_configure_from_dt(struct device *dev,
421 struct device_node *np,
422 struct rk3368_thermal_data *thermal)
428 if (of_property_read_u32(np, "clock-frequency", &rate)) {
429 dev_err(dev, "Missing clock-frequency property in the DT.\n");
432 ret = clk_set_rate(thermal->clk, rate);
434 if (of_property_read_u32(np, "hw-shut-temp", &shut_temp)) {
436 "Missing tshut temp property, using default %ld\n",
437 thermal->chip->hw_shut_temp);
438 thermal->hw_shut_temp = thermal->chip->hw_shut_temp;
440 thermal->hw_shut_temp = shut_temp;
443 if (thermal->hw_shut_temp > INT_MAX) {
444 dev_err(dev, "Invalid tshut temperature specified: %ld\n",
445 thermal->hw_shut_temp);
452 static int predict_temp(int temp)
462 static int temp_last = 25;
463 static int prob_last = 20;
464 static int bounding_cnt;
466 struct rk3368_thermal_data *ctx = rk3368_thermal_get_data();
471 if (bounding_cnt++ > START_BOUNDING_COUNT) {
472 bounding_cnt = START_BOUNDING_COUNT;
473 if (temp - temp_last > HIGHER_BOUNDING_TEMP)
474 temp = temp_last + HIGHER_BOUNDING_TEMP / 3;
475 if (temp_last - temp > LOWER_BOUNDING_TEMP)
476 temp = temp_last - LOWER_BOUNDING_TEMP / 3;
479 temp_mid = temp_last;
480 prob_mid = prob_last + cov_q;
481 gain = (prob_mid * BASE) / (prob_mid + cov_r);
483 temp_now = temp_mid + (gain * (temp - temp_mid) >> BASE_SHIFT);
484 prob_now = ((BASE - gain) * prob_mid) >> BASE_SHIFT;
486 prob_last = prob_now;
487 temp_last = temp_now;
490 pr_info("prob_now %d, temp_last %d, temp %d gain %d", prob_now,
491 temp_now, temp, gain);
496 static int get_raw_code_internal(void)
501 struct rk3368_thermal_data *ctx = rk3368_thermal_get_data();
506 /* power up, channel 0 */
507 writel_relaxed(0x18, ctx->regs + TSADCV2_USER_CON);
509 udelay(TSADC_CLK_CYCLE_TIME * 2);
511 writel_relaxed(0x38, ctx->regs + TSADCV2_USER_CON);
512 udelay(TSADC_CLK_CYCLE_TIME * 13);
515 for (i = 0; i < 50; i++) {
516 udelay(TSADC_CLK_CYCLE_TIME);
517 val_cpu_pd = readl_relaxed(ctx->regs + TSADCV2_INT_PD);
519 if ((val_cpu_pd & 0x100) == 0x100) {
522 writel_relaxed(0x100, ctx->regs + TSADCV2_INT_PD);
524 val_cpu = readl_relaxed(ctx->regs + TSADCV2_DATA(0));
528 /*power down, channel 0 */
529 writel_relaxed(0x0, ctx->regs + TSADCV2_USER_CON);
534 #define RAW_CODE_MIN (50)
535 #define RAW_CODE_MAX (225)
537 static int rk3368_get_raw_code(void)
539 static int old_data = 130;
542 tsadc_data = get_raw_code_internal();
544 if ((tsadc_data < RAW_CODE_MIN) || (tsadc_data > RAW_CODE_MAX))
545 tsadc_data = old_data;
547 old_data = tsadc_data;
552 static int rk3368_convert_code_2_temp(int tsadc_data, int voltage)
554 struct rk3368_thermal_data *ctx = rk3368_thermal_get_data();
555 const struct rk3368_tsadc_chip *tsadc;
569 rk3368_temp_to_code(tsadc->temp_table,
570 ctx->cpu_temp_adjust * 1000, &tmp_code1);
571 rk3368_temp_to_code(tsadc->temp_table, 0, &tmp_code2);
572 data_adjust = tmp_code1 - tmp_code2;
574 ((tsadc_data * voltage - data_adjust * 1000000) + 500000) / 1000000;
575 rk3368_code_to_temp(tsadc->temp_table, code_temp, &out_temp);
578 pr_info("cpu code temp:[%d, %d], voltage: %d\n",
579 tsadc_data, out_temp / 1000, voltage);
581 if ((out_temp < MIN_TEMP) || (out_temp > MAX_TEMP))
586 ctx->cpu_temp = out_temp / 1000;
590 static int rk3368_thermal_set_trips(void *_sensor, int low, int high)
595 static int rk3368_thermal_get_temp(void *_sensor, int *out_temp)
599 struct rk3368_thermal_data *ctx = rk3368_thermal_get_data();
600 struct platform_device *pdev;
607 mutex_lock(&thermal_reg_mutex);
608 raw_code = rk3368_get_raw_code();
609 temp = rk3368_convert_code_2_temp(raw_code, ctx->regulator_uv);
610 *out_temp = predict_temp(temp / 1000) * 1000;
611 mutex_unlock(&thermal_reg_mutex);
616 static const struct thermal_zone_of_device_ops rk3368_of_thermal_ops = {
617 .get_temp = rk3368_thermal_get_temp,
618 .set_trips = rk3368_thermal_set_trips,
622 rk3368_thermal_register_sensor(struct platform_device *pdev,
623 struct rk3368_thermal_data *ctx,
624 struct rk3368_thermal_sensor *sensor, int id)
630 sensor->tzd = devm_thermal_zone_of_sensor_register(&pdev->dev, id,
632 &rk3368_of_thermal_ops);
633 if (IS_ERR(sensor->tzd)) {
634 error = PTR_ERR(sensor->tzd);
635 dev_err(&pdev->dev, "failed to register sensor %d: %d\n",
644 * Reset TSADC Controller, reset all tsadc registers.
646 static void rk3368_thermal_reset_controller(struct reset_control *reset)
648 reset_control_assert(reset);
650 reset_control_deassert(reset);
653 static ssize_t rk3368_thermal_temp_adjust_test_store(struct kobject *kobj,
654 struct kobj_attribute
655 *attr, const char *buf,
658 struct rk3368_thermal_data *ctx = rk3368_thermal_get_data();
661 const char *buftmp = buf;
667 ret = sscanf(buftmp, "%c ", &cmd);
673 ret = sscanf(buftmp, "%c %d", &cmd, &getdata);
676 ctx->cpu_temp_adjust = getdata;
677 pr_info("get cpu_temp_adjust value = %d\n", getdata);
681 ret = sscanf(buftmp, "%c %d", &cmd, &getdata);
684 ctx->gpu_temp_adjust = getdata;
685 pr_info("get gpu_temp_adjust value = %d\n", getdata);
689 pr_info("Unknown command\n");
696 static ssize_t rk3368_thermal_temp_adjust_test_show(struct kobject *kobj,
697 struct kobj_attribute
700 struct rk3368_thermal_data *ctx = rk3368_thermal_get_data();
707 sprintf(str, "rk3368_thermal: cpu:%d, gpu:%d\n",
708 ctx->cpu_temp_adjust, ctx->gpu_temp_adjust);
712 static ssize_t rk3368_thermal_temp_test_store(struct kobject *kobj,
713 struct kobj_attribute *attr,
714 const char *buf, size_t n)
716 struct rk3368_thermal_data *ctx = rk3368_thermal_get_data();
718 const char *buftmp = buf;
724 ret = sscanf(buftmp, "%c", &cmd);
736 pr_info("Unknown command\n");
743 static ssize_t rk3368_thermal_temp_test_show(struct kobject *kobj,
744 struct kobj_attribute *attr,
747 struct rk3368_thermal_data *ctx = rk3368_thermal_get_data();
753 str += sprintf(str, "current cpu_temp:%d\n", ctx->cpu_temp);
757 struct rk3368_thermal_attribute {
758 struct attribute attr;
759 ssize_t (*show) (struct kobject *kobj, struct kobj_attribute *attr,
761 ssize_t (*store) (struct kobject *kobj, struct kobj_attribute *attr,
762 const char *buf, size_t n);
765 static struct rk3368_thermal_attribute rk3368_thermal_attrs[] = {
766 /*node_name permission show_func store_func */
767 __ATTR(temp_adjust, 0644,
768 rk3368_thermal_temp_adjust_test_show,
769 rk3368_thermal_temp_adjust_test_store),
770 __ATTR(temp, 0644, rk3368_thermal_temp_test_show,
771 rk3368_thermal_temp_test_store),
774 static void rk3368_dump_temperature(void)
776 struct rk3368_thermal_data *ctx = rk3368_thermal_get_data();
777 struct platform_device *pdev;
784 if (ctx->cpu_temp != INVALID_TEMP)
785 dev_warn(&pdev->dev, "cpu channal temperature(%d C)\n",
789 pr_warn("THERMAL REGS:\n");
790 print_hex_dump(KERN_WARNING, "", DUMP_PREFIX_OFFSET,
791 32, 4, ctx->regs, 0x88, false);
794 EXPORT_SYMBOL_GPL(rk3368_dump_temperature);
796 static int rk3368_thermal_panic(struct notifier_block *this,
797 unsigned long ev, void *ptr)
799 rk3368_dump_temperature();
803 static struct notifier_block rk3368_thermal_panic_block = {
804 .notifier_call = rk3368_thermal_panic,
807 static int rk3368_thermal_notify(struct notifier_block *nb,
808 unsigned long event, void *data)
810 struct rk3368_thermal_data *ctx = rk3368_thermal_get_data();
811 struct platform_device *pdev;
818 if (event & REGULATOR_EVENT_PRE_VOLTAGE_CHANGE) {
819 mutex_lock(&thermal_reg_mutex);
820 } else if (event & (REGULATOR_EVENT_VOLTAGE_CHANGE |
821 REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE)) {
822 ctx->regulator_uv = (unsigned long)data;
823 mutex_unlock(&thermal_reg_mutex);
830 static const struct of_device_id of_rk3368_thermal_match[] = {
832 .compatible = "rockchip,rk3368-tsadc-legacy",
833 .data = (void *)&rk3368_tsadc_data,
838 MODULE_DEVICE_TABLE(of, of_rk3368_thermal_match);
840 static int rk3368_thermal_probe(struct platform_device *pdev)
842 struct device_node *np = pdev->dev.of_node;
843 struct rk3368_thermal_data *ctx;
844 const struct of_device_id *match;
845 struct resource *res;
852 match = of_match_node(of_rk3368_thermal_match, np);
856 irq = platform_get_irq(pdev, 0);
858 dev_err(&pdev->dev, "no irq resource?\n");
862 ctx = devm_kzalloc(&pdev->dev, sizeof(struct rk3368_thermal_data),
869 ctx->chip = (const struct rk3368_tsadc_chip *)match->data;
873 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
874 ctx->regs = devm_ioremap_resource(&pdev->dev, res);
875 if (IS_ERR(ctx->regs))
876 return PTR_ERR(ctx->regs);
878 ctx->reset = devm_reset_control_get(&pdev->dev, "tsadc-apb");
879 if (IS_ERR(ctx->reset)) {
880 error = PTR_ERR(ctx->reset);
881 dev_err(&pdev->dev, "failed to get tsadc reset: %d\n", error);
885 ctx->clk = devm_clk_get(&pdev->dev, "tsadc");
886 if (IS_ERR(ctx->clk)) {
887 error = PTR_ERR(ctx->clk);
888 dev_err(&pdev->dev, "failed to get tsadc clock: %d\n", error);
892 ctx->pclk = devm_clk_get(&pdev->dev, "apb_pclk");
893 if (IS_ERR(ctx->pclk)) {
894 error = PTR_ERR(ctx->pclk);
895 dev_err(&pdev->dev, "failed to get apb_pclk clock: %d\n",
900 error = clk_prepare_enable(ctx->clk);
902 dev_err(&pdev->dev, "failed to enable converter clock: %d\n",
907 error = clk_prepare_enable(ctx->pclk);
909 dev_err(&pdev->dev, "failed to enable pclk: %d\n", error);
910 goto err_disable_clk;
913 rk3368_thermal_reset_controller(ctx->reset);
915 error = rk3368_configure_from_dt(&pdev->dev, np, ctx);
917 dev_err(&pdev->dev, "failed to parse device tree data: %d\n",
919 goto err_disable_pclk;
923 ctx->ref_regulator = devm_regulator_get_optional(&pdev->dev, "tsadc");
925 if (IS_ERR(ctx->ref_regulator)) {
926 error = PTR_ERR(ctx->ref_regulator);
928 if (error != -EPROBE_DEFER)
930 "couldn't get regulator tsadc-supply\n");
931 goto err_disable_pclk;
934 ctx->tsadc_nb.notifier_call = rk3368_thermal_notify;
936 /* register regulator notifier */
938 regulator_register_notifier(ctx->ref_regulator, &ctx->tsadc_nb);
940 dev_err(&pdev->dev, "regulator notifier request failed\n");
941 goto err_disable_pclk;
944 uv = regulator_get_voltage(ctx->ref_regulator);
946 dev_WARN(&pdev->dev, "regulator get failed\n");
950 mutex_lock(&thermal_reg_mutex);
951 if (!ctx->regulator_uv)
952 ctx->regulator_uv = uv;
953 mutex_unlock(&thermal_reg_mutex);
955 rk3368_get_ajust_code(np, &ajust_code);
957 ctx->cpu_temp_adjust = (int)ajust_code;
959 for (i = 0; i < ctx->chip->chn_num; i++) {
960 error = rk3368_thermal_register_sensor(pdev, ctx,
962 ctx->chip->chn_id[i]);
965 "failed to register thermal sensor %d : error= %d\n",
967 for (j = 0; j < i; j++)
968 thermal_zone_of_sensor_unregister(&pdev->dev,
969 ctx->sensors[j].tzd);
970 goto err_unreg_notifier;
974 ctx->rk3368_thermal_kobj =
975 kobject_create_and_add("rk3368_thermal", NULL);
976 if (!ctx->rk3368_thermal_kobj) {
979 "failed to creat debug node : error= %d\n", error);
980 goto err_unreg_notifier;
983 for (i = 0; i < ARRAY_SIZE(rk3368_thermal_attrs); i++) {
985 sysfs_create_file(ctx->rk3368_thermal_kobj,
986 &rk3368_thermal_attrs[i].attr);
989 "failed to register thermal sensor %d : error= %d\n",
991 for (j = 0; j < i; j++)
992 sysfs_remove_file(ctx->rk3368_thermal_kobj,
993 &rk3368_thermal_attrs[j].attr);
995 goto err_unreg_notifier;
999 platform_set_drvdata(pdev, ctx);
1001 atomic_notifier_chain_register(&panic_notifier_list,
1002 &rk3368_thermal_panic_block);
1004 ctx->cpu_temp = INVALID_TEMP;
1006 pr_info("rk3368 tsadc probed successfully\n");
1011 regulator_unregister_notifier(ctx->ref_regulator, &ctx->tsadc_nb);
1014 clk_disable_unprepare(ctx->pclk);
1016 clk_disable_unprepare(ctx->clk);
1021 static int rk3368_thermal_remove(struct platform_device *pdev)
1023 struct rk3368_thermal_data *ctx = platform_get_drvdata(pdev);
1026 for (i = 0; i < ctx->chip->chn_num; i++) {
1027 struct rk3368_thermal_sensor *sensor = &ctx->sensors[i];
1029 thermal_zone_of_sensor_unregister(&pdev->dev, sensor->tzd);
1031 clk_disable_unprepare(ctx->pclk);
1032 clk_disable_unprepare(ctx->clk);
1037 static int __maybe_unused rk3368_thermal_suspend(struct device *dev)
1039 struct platform_device *pdev = to_platform_device(dev);
1040 struct rk3368_thermal_data *ctx = platform_get_drvdata(pdev);
1042 clk_disable(ctx->pclk);
1043 clk_disable(ctx->clk);
1047 static int __maybe_unused rk3368_thermal_resume(struct device *dev)
1049 struct platform_device *pdev = to_platform_device(dev);
1050 struct rk3368_thermal_data *ctx = platform_get_drvdata(pdev);
1053 error = clk_enable(ctx->clk);
1057 error = clk_enable(ctx->pclk);
1059 clk_disable(ctx->clk);
1063 rk3368_thermal_reset_controller(ctx->reset);
1068 static SIMPLE_DEV_PM_OPS(rk3368_thermal_pm_ops,
1069 rk3368_thermal_suspend, rk3368_thermal_resume);
1071 static struct platform_driver rk3368_thermal_driver = {
1073 .name = "rk3368-thermal",
1074 .pm = &rk3368_thermal_pm_ops,
1075 .of_match_table = of_rk3368_thermal_match,
1077 .probe = rk3368_thermal_probe,
1078 .remove = rk3368_thermal_remove,
1081 /* rk3368 thermal needs a clock source of 32k from rk818, so this init process
1084 static int __init rk3368_thermal_init_driver(void)
1086 return platform_driver_register(&rk3368_thermal_driver);
1088 late_initcall(rk3368_thermal_init_driver);
1090 MODULE_DESCRIPTION("ROCKCHIP THERMAL Driver");
1091 MODULE_AUTHOR("Rockchip, Inc.");
1092 MODULE_LICENSE("GPL v2");
1093 MODULE_ALIAS("platform:rk3368-thermal");