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, int *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)
286 *ajust_code = -(buf[0] & 0x7f);
288 *ajust_code = buf[0];
295 static struct rk3368_thermal_data *rk3368_thermal_get_data(void)
297 WARN_ON(!thermal_ctx);
301 static int rk3368_temp_to_code(const struct chip_tsadc_table *tmp_table,
302 long temp, u32 *code)
304 unsigned int low = 1;
305 unsigned int high = tmp_table->length - 1;
306 unsigned int mid = (low + high) / 2;
309 *code = tmp_table->data_mask;
311 WARN_ON(tmp_table->length < 2);
313 if (temp < tmp_table->id[low].temp)
314 return -EAGAIN; /* Incorrect reading */
316 while (low <= high) {
317 if (temp == tmp_table->id[mid].temp) {
318 *code = tmp_table->id[mid].code;
320 } else if (temp > tmp_table->id[mid].temp) {
326 mid = (low + high) / 2;
329 * The 5C granularity provided by the table is too much. Let's
330 * assume that the relationship between sensor readings and
331 * temperature between 2 table entries is linear and interpolate
332 * to produce less granular result.
334 if (*code == tmp_table->data_mask) {
335 num = abs(tmp_table->id[low].code - tmp_table->id[high].code);
336 num *= abs(tmp_table->id[high].temp - temp);
337 denom = abs(tmp_table->id[high].temp - tmp_table->id[low].temp);
338 *code = tmp_table->id[high].code + (num / denom);
344 static int rk3368_code_to_temp(const struct chip_tsadc_table *tmp_table,
347 unsigned int low = 1;
348 unsigned int high = tmp_table->length - 1;
349 unsigned int mid = (low + high) / 2;
352 *temp = INVALID_TEMP;
354 WARN_ON(tmp_table->length < 2);
356 switch (tmp_table->mode) {
358 code &= tmp_table->data_mask;
359 if (code < tmp_table->id[high].code)
360 return -EAGAIN; /* Incorrect reading */
362 while (low <= high) {
363 if (code == tmp_table->id[mid].code) {
364 *temp = tmp_table->id[mid].temp;
366 } else if (code < tmp_table->id[mid].code) {
372 mid = (low + high) / 2;
376 code &= tmp_table->data_mask;
377 if (code < tmp_table->id[low].code)
378 return -EAGAIN; /* Incorrect reading */
380 while (low <= high) {
381 if (code == tmp_table->id[mid].code) {
382 *temp = tmp_table->id[mid].temp;
384 } else if (code > tmp_table->id[mid].code) {
390 mid = (low + high) / 2;
394 pr_err("Invalid the conversion table\n");
398 * The 5C granularity provided by the table is too much. Let's
399 * assume that the relationship between sensor readings and
400 * temperature between 2 table entries is linear and interpolate
401 * to produce less granular result.
403 if (*temp == INVALID_TEMP) {
404 num = abs(tmp_table->id[low].temp - tmp_table->id[high].temp);
405 num *= abs(tmp_table->id[high].code - code);
406 denom = abs(tmp_table->id[high].code - tmp_table->id[low].code);
407 *temp = tmp_table->id[high].temp + (num / denom);
413 static const struct rk3368_tsadc_chip rk3368_tsadc_data = {
414 .tshut_mode = TSHUT_MODE_GPIO, /* default TSHUT via GPIO give PMIC */
415 .tshut_polarity = TSHUT_LOW_ACTIVE, /* default TSHUT LOW ACTIVE */
416 .hw_shut_temp = 125000,
417 .mode = TSHUT_USER_MODE,
421 .temp_table = &tsadc_table_3368,
424 static int rk3368_configure_from_dt(struct device *dev,
425 struct device_node *np,
426 struct rk3368_thermal_data *thermal)
432 if (of_property_read_u32(np, "clock-frequency", &rate)) {
433 dev_err(dev, "Missing clock-frequency property in the DT.\n");
436 ret = clk_set_rate(thermal->clk, rate);
438 if (of_property_read_u32(np, "hw-shut-temp", &shut_temp)) {
440 "Missing tshut temp property, using default %ld\n",
441 thermal->chip->hw_shut_temp);
442 thermal->hw_shut_temp = thermal->chip->hw_shut_temp;
444 thermal->hw_shut_temp = shut_temp;
447 if (thermal->hw_shut_temp > INT_MAX) {
448 dev_err(dev, "Invalid tshut temperature specified: %ld\n",
449 thermal->hw_shut_temp);
456 static int predict_temp(int temp)
466 static int temp_last = 25;
467 static int prob_last = 20;
468 static int bounding_cnt;
470 struct rk3368_thermal_data *ctx = rk3368_thermal_get_data();
475 if (bounding_cnt++ > START_BOUNDING_COUNT) {
476 bounding_cnt = START_BOUNDING_COUNT;
477 if (temp - temp_last > HIGHER_BOUNDING_TEMP)
478 temp = temp_last + HIGHER_BOUNDING_TEMP / 3;
479 if (temp_last - temp > LOWER_BOUNDING_TEMP)
480 temp = temp_last - LOWER_BOUNDING_TEMP / 3;
483 temp_mid = temp_last;
484 prob_mid = prob_last + cov_q;
485 gain = (prob_mid * BASE) / (prob_mid + cov_r);
487 temp_now = temp_mid + (gain * (temp - temp_mid) >> BASE_SHIFT);
488 prob_now = ((BASE - gain) * prob_mid) >> BASE_SHIFT;
490 prob_last = prob_now;
491 temp_last = temp_now;
494 pr_info("prob_now %d, temp_last %d, temp %d gain %d", prob_now,
495 temp_now, temp, gain);
500 static int get_raw_code_internal(void)
505 struct rk3368_thermal_data *ctx = rk3368_thermal_get_data();
510 /* power up, channel 0 */
511 writel_relaxed(0x18, ctx->regs + TSADCV2_USER_CON);
513 udelay(TSADC_CLK_CYCLE_TIME * 2);
515 writel_relaxed(0x38, ctx->regs + TSADCV2_USER_CON);
516 udelay(TSADC_CLK_CYCLE_TIME * 13);
519 for (i = 0; i < 50; i++) {
520 udelay(TSADC_CLK_CYCLE_TIME);
521 val_cpu_pd = readl_relaxed(ctx->regs + TSADCV2_INT_PD);
523 if ((val_cpu_pd & 0x100) == 0x100) {
526 writel_relaxed(0x100, ctx->regs + TSADCV2_INT_PD);
528 val_cpu = readl_relaxed(ctx->regs + TSADCV2_DATA(0));
532 /*power down, channel 0 */
533 writel_relaxed(0x0, ctx->regs + TSADCV2_USER_CON);
538 #define RAW_CODE_MIN (50)
539 #define RAW_CODE_MAX (225)
541 static int rk3368_get_raw_code(void)
543 static int old_data = 130;
546 tsadc_data = get_raw_code_internal();
548 if ((tsadc_data < RAW_CODE_MIN) || (tsadc_data > RAW_CODE_MAX))
549 tsadc_data = old_data;
551 old_data = tsadc_data;
556 static int rk3368_convert_code_2_temp(int tsadc_data, int voltage)
558 struct rk3368_thermal_data *ctx = rk3368_thermal_get_data();
559 const struct rk3368_tsadc_chip *tsadc;
573 rk3368_temp_to_code(tsadc->temp_table,
574 ctx->cpu_temp_adjust * 1000, &tmp_code1);
575 rk3368_temp_to_code(tsadc->temp_table, 0, &tmp_code2);
576 data_adjust = tmp_code1 - tmp_code2;
578 ((tsadc_data * voltage - data_adjust * 1000000) + 500000) / 1000000;
579 rk3368_code_to_temp(tsadc->temp_table, code_temp, &out_temp);
582 pr_info("cpu code temp:[%d, %d], voltage: %d\n",
583 tsadc_data, out_temp / 1000, voltage);
585 if ((out_temp < MIN_TEMP) || (out_temp > MAX_TEMP))
590 ctx->cpu_temp = out_temp / 1000;
594 static int rk3368_thermal_set_trips(void *_sensor, int low, int high)
599 static int rk3368_thermal_get_temp(void *_sensor, int *out_temp)
603 struct rk3368_thermal_data *ctx = rk3368_thermal_get_data();
604 struct platform_device *pdev;
611 mutex_lock(&thermal_reg_mutex);
612 raw_code = rk3368_get_raw_code();
613 temp = rk3368_convert_code_2_temp(raw_code, ctx->regulator_uv);
614 *out_temp = predict_temp(temp / 1000) * 1000;
615 mutex_unlock(&thermal_reg_mutex);
620 static const struct thermal_zone_of_device_ops rk3368_of_thermal_ops = {
621 .get_temp = rk3368_thermal_get_temp,
622 .set_trips = rk3368_thermal_set_trips,
626 rk3368_thermal_register_sensor(struct platform_device *pdev,
627 struct rk3368_thermal_data *ctx,
628 struct rk3368_thermal_sensor *sensor, int id)
634 sensor->tzd = devm_thermal_zone_of_sensor_register(&pdev->dev, id,
636 &rk3368_of_thermal_ops);
637 if (IS_ERR(sensor->tzd)) {
638 error = PTR_ERR(sensor->tzd);
639 dev_err(&pdev->dev, "failed to register sensor %d: %d\n",
648 * Reset TSADC Controller, reset all tsadc registers.
650 static void rk3368_thermal_reset_controller(struct reset_control *reset)
652 reset_control_assert(reset);
654 reset_control_deassert(reset);
657 static ssize_t rk3368_thermal_temp_adjust_test_store(struct kobject *kobj,
658 struct kobj_attribute
659 *attr, const char *buf,
662 struct rk3368_thermal_data *ctx = rk3368_thermal_get_data();
665 const char *buftmp = buf;
671 ret = sscanf(buftmp, "%c ", &cmd);
677 ret = sscanf(buftmp, "%c %d", &cmd, &getdata);
680 ctx->cpu_temp_adjust = getdata;
681 pr_info("get cpu_temp_adjust value = %d\n", getdata);
685 ret = sscanf(buftmp, "%c %d", &cmd, &getdata);
688 ctx->gpu_temp_adjust = getdata;
689 pr_info("get gpu_temp_adjust value = %d\n", getdata);
693 pr_info("Unknown command\n");
700 static ssize_t rk3368_thermal_temp_adjust_test_show(struct kobject *kobj,
701 struct kobj_attribute
704 struct rk3368_thermal_data *ctx = rk3368_thermal_get_data();
711 sprintf(str, "rk3368_thermal: cpu:%d, gpu:%d\n",
712 ctx->cpu_temp_adjust, ctx->gpu_temp_adjust);
716 static ssize_t rk3368_thermal_temp_test_store(struct kobject *kobj,
717 struct kobj_attribute *attr,
718 const char *buf, size_t n)
720 struct rk3368_thermal_data *ctx = rk3368_thermal_get_data();
722 const char *buftmp = buf;
728 ret = sscanf(buftmp, "%c", &cmd);
740 pr_info("Unknown command\n");
747 static ssize_t rk3368_thermal_temp_test_show(struct kobject *kobj,
748 struct kobj_attribute *attr,
751 struct rk3368_thermal_data *ctx = rk3368_thermal_get_data();
757 str += sprintf(str, "current cpu_temp:%d\n", ctx->cpu_temp);
761 struct rk3368_thermal_attribute {
762 struct attribute attr;
763 ssize_t (*show) (struct kobject *kobj, struct kobj_attribute *attr,
765 ssize_t (*store) (struct kobject *kobj, struct kobj_attribute *attr,
766 const char *buf, size_t n);
769 static struct rk3368_thermal_attribute rk3368_thermal_attrs[] = {
770 /*node_name permission show_func store_func */
771 __ATTR(temp_adjust, 0644,
772 rk3368_thermal_temp_adjust_test_show,
773 rk3368_thermal_temp_adjust_test_store),
774 __ATTR(temp, 0644, rk3368_thermal_temp_test_show,
775 rk3368_thermal_temp_test_store),
778 static void rk3368_dump_temperature(void)
780 struct rk3368_thermal_data *ctx = rk3368_thermal_get_data();
781 struct platform_device *pdev;
788 if (ctx->cpu_temp != INVALID_TEMP)
789 dev_warn(&pdev->dev, "cpu channal temperature(%d C)\n",
793 pr_warn("THERMAL REGS:\n");
794 print_hex_dump(KERN_WARNING, "", DUMP_PREFIX_OFFSET,
795 32, 4, ctx->regs, 0x88, false);
798 EXPORT_SYMBOL_GPL(rk3368_dump_temperature);
800 static int rk3368_thermal_panic(struct notifier_block *this,
801 unsigned long ev, void *ptr)
803 rk3368_dump_temperature();
807 static struct notifier_block rk3368_thermal_panic_block = {
808 .notifier_call = rk3368_thermal_panic,
811 static int rk3368_thermal_notify(struct notifier_block *nb,
812 unsigned long event, void *data)
814 struct rk3368_thermal_data *ctx = rk3368_thermal_get_data();
815 struct platform_device *pdev;
822 if (event & REGULATOR_EVENT_PRE_VOLTAGE_CHANGE) {
823 mutex_lock(&thermal_reg_mutex);
824 } else if (event & (REGULATOR_EVENT_VOLTAGE_CHANGE |
825 REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE)) {
826 ctx->regulator_uv = (unsigned long)data;
827 mutex_unlock(&thermal_reg_mutex);
834 static const struct of_device_id of_rk3368_thermal_match[] = {
836 .compatible = "rockchip,rk3368-tsadc-legacy",
837 .data = (void *)&rk3368_tsadc_data,
842 MODULE_DEVICE_TABLE(of, of_rk3368_thermal_match);
844 static int rk3368_thermal_probe(struct platform_device *pdev)
846 struct device_node *np = pdev->dev.of_node;
847 struct rk3368_thermal_data *ctx;
848 const struct of_device_id *match;
849 struct resource *res;
856 match = of_match_node(of_rk3368_thermal_match, np);
860 irq = platform_get_irq(pdev, 0);
862 dev_err(&pdev->dev, "no irq resource?\n");
866 ctx = devm_kzalloc(&pdev->dev, sizeof(struct rk3368_thermal_data),
873 ctx->chip = (const struct rk3368_tsadc_chip *)match->data;
877 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
878 ctx->regs = devm_ioremap_resource(&pdev->dev, res);
879 if (IS_ERR(ctx->regs))
880 return PTR_ERR(ctx->regs);
882 ctx->reset = devm_reset_control_get(&pdev->dev, "tsadc-apb");
883 if (IS_ERR(ctx->reset)) {
884 error = PTR_ERR(ctx->reset);
885 dev_err(&pdev->dev, "failed to get tsadc reset: %d\n", error);
889 ctx->clk = devm_clk_get(&pdev->dev, "tsadc");
890 if (IS_ERR(ctx->clk)) {
891 error = PTR_ERR(ctx->clk);
892 dev_err(&pdev->dev, "failed to get tsadc clock: %d\n", error);
896 ctx->pclk = devm_clk_get(&pdev->dev, "apb_pclk");
897 if (IS_ERR(ctx->pclk)) {
898 error = PTR_ERR(ctx->pclk);
899 dev_err(&pdev->dev, "failed to get apb_pclk clock: %d\n",
904 error = clk_prepare_enable(ctx->clk);
906 dev_err(&pdev->dev, "failed to enable converter clock: %d\n",
911 error = clk_prepare_enable(ctx->pclk);
913 dev_err(&pdev->dev, "failed to enable pclk: %d\n", error);
914 goto err_disable_clk;
917 rk3368_thermal_reset_controller(ctx->reset);
919 error = rk3368_configure_from_dt(&pdev->dev, np, ctx);
921 dev_err(&pdev->dev, "failed to parse device tree data: %d\n",
923 goto err_disable_pclk;
927 ctx->ref_regulator = devm_regulator_get_optional(&pdev->dev, "tsadc");
929 if (IS_ERR(ctx->ref_regulator)) {
930 error = PTR_ERR(ctx->ref_regulator);
932 if (error != -EPROBE_DEFER)
934 "couldn't get regulator tsadc-supply\n");
935 goto err_disable_pclk;
938 ctx->tsadc_nb.notifier_call = rk3368_thermal_notify;
940 /* register regulator notifier */
942 regulator_register_notifier(ctx->ref_regulator, &ctx->tsadc_nb);
944 dev_err(&pdev->dev, "regulator notifier request failed\n");
945 goto err_disable_pclk;
948 uv = regulator_get_voltage(ctx->ref_regulator);
950 dev_WARN(&pdev->dev, "regulator get failed\n");
954 mutex_lock(&thermal_reg_mutex);
955 if (!ctx->regulator_uv)
956 ctx->regulator_uv = uv;
957 mutex_unlock(&thermal_reg_mutex);
959 rk3368_get_ajust_code(np, &ajust_code);
961 ctx->cpu_temp_adjust = (int)ajust_code;
963 for (i = 0; i < ctx->chip->chn_num; i++) {
964 error = rk3368_thermal_register_sensor(pdev, ctx,
966 ctx->chip->chn_id[i]);
969 "failed to register thermal sensor %d : error= %d\n",
971 for (j = 0; j < i; j++)
972 thermal_zone_of_sensor_unregister(&pdev->dev,
973 ctx->sensors[j].tzd);
974 goto err_unreg_notifier;
978 ctx->rk3368_thermal_kobj =
979 kobject_create_and_add("rk3368_thermal", NULL);
980 if (!ctx->rk3368_thermal_kobj) {
983 "failed to creat debug node : error= %d\n", error);
984 goto err_unreg_notifier;
987 for (i = 0; i < ARRAY_SIZE(rk3368_thermal_attrs); i++) {
989 sysfs_create_file(ctx->rk3368_thermal_kobj,
990 &rk3368_thermal_attrs[i].attr);
993 "failed to register thermal sensor %d : error= %d\n",
995 for (j = 0; j < i; j++)
996 sysfs_remove_file(ctx->rk3368_thermal_kobj,
997 &rk3368_thermal_attrs[j].attr);
999 goto err_unreg_notifier;
1003 platform_set_drvdata(pdev, ctx);
1005 atomic_notifier_chain_register(&panic_notifier_list,
1006 &rk3368_thermal_panic_block);
1008 ctx->cpu_temp = INVALID_TEMP;
1010 pr_info("rk3368 tsadc probed successfully\n");
1015 regulator_unregister_notifier(ctx->ref_regulator, &ctx->tsadc_nb);
1018 clk_disable_unprepare(ctx->pclk);
1020 clk_disable_unprepare(ctx->clk);
1025 static int rk3368_thermal_remove(struct platform_device *pdev)
1027 struct rk3368_thermal_data *ctx = platform_get_drvdata(pdev);
1030 for (i = 0; i < ctx->chip->chn_num; i++) {
1031 struct rk3368_thermal_sensor *sensor = &ctx->sensors[i];
1033 thermal_zone_of_sensor_unregister(&pdev->dev, sensor->tzd);
1035 clk_disable_unprepare(ctx->pclk);
1036 clk_disable_unprepare(ctx->clk);
1041 static int __maybe_unused rk3368_thermal_suspend(struct device *dev)
1043 struct platform_device *pdev = to_platform_device(dev);
1044 struct rk3368_thermal_data *ctx = platform_get_drvdata(pdev);
1046 clk_disable(ctx->pclk);
1047 clk_disable(ctx->clk);
1051 static int __maybe_unused rk3368_thermal_resume(struct device *dev)
1053 struct platform_device *pdev = to_platform_device(dev);
1054 struct rk3368_thermal_data *ctx = platform_get_drvdata(pdev);
1057 error = clk_enable(ctx->clk);
1061 error = clk_enable(ctx->pclk);
1063 clk_disable(ctx->clk);
1067 rk3368_thermal_reset_controller(ctx->reset);
1072 static SIMPLE_DEV_PM_OPS(rk3368_thermal_pm_ops,
1073 rk3368_thermal_suspend, rk3368_thermal_resume);
1075 static struct platform_driver rk3368_thermal_driver = {
1077 .name = "rk3368-thermal",
1078 .pm = &rk3368_thermal_pm_ops,
1079 .of_match_table = of_rk3368_thermal_match,
1081 .probe = rk3368_thermal_probe,
1082 .remove = rk3368_thermal_remove,
1085 /* rk3368 thermal needs a clock source of 32k from rk818, so this init process
1088 static int __init rk3368_thermal_init_driver(void)
1090 return platform_driver_register(&rk3368_thermal_driver);
1092 late_initcall(rk3368_thermal_init_driver);
1094 MODULE_DESCRIPTION("ROCKCHIP THERMAL Driver");
1095 MODULE_AUTHOR("Rockchip, Inc.");
1096 MODULE_LICENSE("GPL v2");
1097 MODULE_ALIAS("platform:rk3368-thermal");