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>
36 #include <linux/pm_qos.h>
37 #include <soc/rockchip/scpi.h>
40 * If the temperature over a period of time High,
41 * the resulting TSHUT gave CRU module,let it reset the entire chip,
42 * or via GPIO give PMIC.
55 * the system Temperature Sensors tshut(tshut) polarity
56 * the bit 8 is tshut polarity.
57 * 0: low active, 1: high active
66 /* TSADC V2 Sensor info define: */
67 #define TSADCV2_USER_CON 0x00
68 #define TSADCV2_AUTO_CON 0x04
69 #define TSADCV2_INT_EN 0x08
70 #define TSADCV2_INT_PD 0x0c
71 #define TSADCV2_DATA(chn) (0x20 + (chn) * 0x04)
73 #define TSADC_CLK_CYCLE_TIME 32 /* usec */
74 #define TSADCV3_DATA_MASK 0x3ff
77 * The conversion table has the adc value and temperature.
78 * ADC_DECREMENT: the adc value is of diminishing.(e.g. rk3288_code_table)
79 * ADC_INCREMENT: the adc value is incremental.(e.g. rk3368_code_table)
86 #define TIME_OUT_TOTAL 2000
87 #define INVALID_EFUSE_VALUE 0xff
93 #define MIN_TEMP (-40000)
94 #define MAX_TEMP (125000)
97 #define BASE_SHIFT (10)
98 #define START_BOUNDING_COUNT (100)
99 #define HIGHER_BOUNDING_TEMP (30)
100 #define LOWER_BOUNDING_TEMP (15)
103 * struct tsadc_table - hold information about code and temp mapping
104 * @code: raw code from tsadc ip
105 * @temp: the mapping temperature
114 * struct chip_tsadc_table - hold information about chip-specific differences
115 * @id: conversion table
116 * @length: size of conversion table
117 * @data_mask: mask to apply on data inputs
118 * @mode: sort mode of this adc variant (incrementing or decrementing)
120 struct chip_tsadc_table {
121 const struct tsadc_table *id;
124 enum adc_sort_mode mode;
128 * struct rk3368_tsadc_chip - hold the private data of tsadc chip
129 * @chn_id[SOC_MAX_SENSORS]: the sensor id of chip correspond to the channel
130 * @chn_num: the channel number of tsadc chip
131 * @tshut_temp: the hardware-controlled shutdown temperature value
132 * @tshut_mode: the hardware-controlled shutdown mode (0:CRU 1:GPIO)
133 * @tshut_polarity: the hardware-controlled active polarity (0:LOW 1:HIGH)
134 * @chip_tsadc_table: the chip-specific conversion table
135 * @get_temp: get the temperature
136 * @set_alarm_temp: set the high temperature interrupt
137 * @set_tshut_temp: set the hardware-controlled shutdown temperature
138 * @set_tshut_mode: set the hardware-controlled shutdown mode
140 struct rk3368_tsadc_chip {
141 int chn_id[NUM_SENSORS];
144 enum tshut_mode tshut_mode;
145 enum tsadc_mode mode;
146 enum tshut_polarity tshut_polarity;
149 const struct chip_tsadc_table *temp_table;
151 /* Per-sensor methods */
152 int (*get_temp)(const struct chip_tsadc_table *table,
153 int chn, void __iomem *reg, int *temp);
154 void (*set_alarm_temp)(const struct chip_tsadc_table *table,
155 int chn, void __iomem *reg, int temp);
156 void (*set_tshut_temp)(const struct chip_tsadc_table *table,
157 int chn, void __iomem *reg, int temp);
158 void (*set_tshut_mode)(int chn, void __iomem *reg, enum tshut_mode m);
162 * struct rk3368_thermal_sensor - hold the information of thermal sensor
163 * @ctx: pointer to the platform/configuration data
164 * @tzd: pointer to a thermal zone
165 * @id: identifier of the thermal sensor
167 struct rk3368_thermal_sensor {
168 struct rk3368_thermal_data *ctx;
169 struct thermal_zone_device *tzd;
174 * struct rk3368_thermal_data - hold the private data of thermal driver
175 * @chip: pointer to the platform/configuration data
176 * @pdev: platform device of thermal
177 * @reset: the reset controller of tsadc
178 * @sensors[SOC_MAX_SENSORS]: the thermal sensor
179 * @clk: the controller clock is divided by the external 24MHz
180 * @pclk: the advanced peripherals bus clock
181 * @regs: the base address of tsadc controller
182 * @tshut_temp: the hardware-controlled shutdown temperature value
183 * @tshut_mode: the hardware-controlled shutdown mode (0:CRU 1:GPIO)
184 * @tshut_polarity: the hardware-controlled active polarity (0:LOW 1:HIGH)
185 * @cpu_temp_adjust: efuse value used to ajust the temperature
186 * @gpu_temp_adjust: efuse value used to ajust the temperature
187 * @cpu_temp: the current cpu's temperature
188 * @logout: switch to control log output or not
189 * @rk3368_thermal_kobj: node in sys fs
191 struct rk3368_thermal_data {
192 const struct rk3368_tsadc_chip *chip;
193 struct platform_device *pdev;
194 struct reset_control *reset;
196 struct rk3368_thermal_sensor sensors[NUM_SENSORS];
203 enum tshut_mode tshut_mode;
204 enum tshut_polarity tshut_polarity;
210 struct kobject *rk3368_thermal_kobj;
211 struct regulator *ref_regulator;
215 struct notifier_block tsadc_nb;
218 static struct rk3368_thermal_data *thermal_ctx;
220 static DEFINE_MUTEX(thermal_reg_mutex);
222 static DEFINE_MUTEX(thermal_lat_mutex);
224 static const struct tsadc_table code_table_3368[] = {
260 {TSADCV3_DATA_MASK, MAX_TEMP},
263 static const struct chip_tsadc_table tsadc_table_3368 = {
264 .id = code_table_3368,
265 .length = ARRAY_SIZE(code_table_3368),
266 .data_mask = TSADCV3_DATA_MASK,
267 .mode = ADC_INCREMENT,
270 static int rk3368_get_ajust_code(struct device_node *np, int *ajust_code)
272 struct nvmem_cell *cell;
276 cell = of_nvmem_cell_get(np, "temp_adjust");
278 pr_err("avs failed to get temp_adjust cell\n");
279 return PTR_ERR(cell);
282 buf = (unsigned char *)nvmem_cell_read(cell, &len);
284 nvmem_cell_put(cell);
289 if (buf[0] == INVALID_EFUSE_VALUE)
293 *ajust_code = -(buf[0] & 0x7f);
295 *ajust_code = buf[0];
302 static struct rk3368_thermal_data *rk3368_thermal_get_data(void)
304 WARN_ON(!thermal_ctx);
308 static int rk3368_temp_to_code(const struct chip_tsadc_table *tmp_table,
309 long temp, u32 *code)
311 unsigned int low = 1;
312 unsigned int high = tmp_table->length - 1;
313 unsigned int mid = (low + high) / 2;
316 *code = tmp_table->data_mask;
318 WARN_ON(tmp_table->length < 2);
320 if (temp < tmp_table->id[low].temp)
321 return -EAGAIN; /* Incorrect reading */
323 while (low <= high) {
324 if (temp == tmp_table->id[mid].temp) {
325 *code = tmp_table->id[mid].code;
327 } else if (temp > tmp_table->id[mid].temp) {
333 mid = (low + high) / 2;
336 * The 5C granularity provided by the table is too much. Let's
337 * assume that the relationship between sensor readings and
338 * temperature between 2 table entries is linear and interpolate
339 * to produce less granular result.
341 if (*code == tmp_table->data_mask) {
342 num = abs(tmp_table->id[low].code - tmp_table->id[high].code);
343 num *= abs(tmp_table->id[high].temp - temp);
344 denom = abs(tmp_table->id[high].temp - tmp_table->id[low].temp);
345 *code = tmp_table->id[high].code + (num / denom);
351 static int rk3368_code_to_temp(const struct chip_tsadc_table *tmp_table,
354 unsigned int low = 1;
355 unsigned int high = tmp_table->length - 1;
356 unsigned int mid = (low + high) / 2;
359 *temp = INVALID_TEMP;
361 WARN_ON(tmp_table->length < 2);
363 switch (tmp_table->mode) {
365 code &= tmp_table->data_mask;
366 if (code < tmp_table->id[high].code)
367 return -EAGAIN; /* Incorrect reading */
369 while (low <= high) {
370 if (code == tmp_table->id[mid].code) {
371 *temp = tmp_table->id[mid].temp;
373 } else if (code < tmp_table->id[mid].code) {
379 mid = (low + high) / 2;
383 code &= tmp_table->data_mask;
384 if (code < tmp_table->id[low].code)
385 return -EAGAIN; /* Incorrect reading */
387 while (low <= high) {
388 if (code == tmp_table->id[mid].code) {
389 *temp = tmp_table->id[mid].temp;
391 } else if (code > tmp_table->id[mid].code) {
397 mid = (low + high) / 2;
401 pr_err("Invalid the conversion table\n");
405 * The 5C granularity provided by the table is too much. Let's
406 * assume that the relationship between sensor readings and
407 * temperature between 2 table entries is linear and interpolate
408 * to produce less granular result.
410 if (*temp == INVALID_TEMP) {
411 num = abs(tmp_table->id[low].temp - tmp_table->id[high].temp);
412 num *= abs(tmp_table->id[high].code - code);
413 denom = abs(tmp_table->id[high].code - tmp_table->id[low].code);
414 *temp = tmp_table->id[high].temp + (num / denom);
420 static const struct rk3368_tsadc_chip rk3368_tsadc_data = {
421 .tshut_mode = TSHUT_MODE_GPIO, /* default TSHUT via GPIO give PMIC */
422 .tshut_polarity = TSHUT_LOW_ACTIVE, /* default TSHUT LOW ACTIVE */
423 .latency_bound = 50000, /* default 50000 us */
424 .hw_shut_temp = 125000,
425 .mode = TSHUT_USER_MODE,
429 .temp_table = &tsadc_table_3368,
432 static int rk3368_configure_from_dt(struct device *dev,
433 struct device_node *np,
434 struct rk3368_thermal_data *thermal)
442 if (of_property_read_u32(np, "clock-frequency", &rate)) {
443 dev_err(dev, "Missing clock-frequency property in the DT.\n");
446 ret = clk_set_rate(thermal->clk, rate);
448 cycle = DIV_ROUND_UP(1000000000, rate) / 1000;
450 if (scpi_thermal_set_clk_cycle(cycle)) {
451 dev_err(dev, "scpi_thermal_set_clk_cycle error.\n");
455 if (of_property_read_u32(np, "hw-shut-temp", &shut_temp)) {
457 "Missing tshut temp property, using default %ld\n",
458 thermal->chip->hw_shut_temp);
459 thermal->hw_shut_temp = thermal->chip->hw_shut_temp;
461 thermal->hw_shut_temp = shut_temp;
464 if (of_property_read_u32(np, "latency-bound", &lat_bound)) {
466 "Missing latency-bound property, using default %d\n",
467 thermal->chip->latency_bound);
468 thermal->latency_bound = thermal->chip->latency_bound;
470 thermal->latency_bound = lat_bound;
473 if (thermal->hw_shut_temp > INT_MAX) {
474 dev_err(dev, "Invalid tshut temperature specified: %ld\n",
475 thermal->hw_shut_temp);
482 static int predict_temp(int temp)
492 static int temp_last = 25;
493 static int prob_last = 20;
494 static int bounding_cnt;
496 struct rk3368_thermal_data *ctx = rk3368_thermal_get_data();
501 if (bounding_cnt++ > START_BOUNDING_COUNT) {
502 bounding_cnt = START_BOUNDING_COUNT;
503 if (temp - temp_last > HIGHER_BOUNDING_TEMP)
504 temp = temp_last + HIGHER_BOUNDING_TEMP / 3;
505 if (temp_last - temp > LOWER_BOUNDING_TEMP)
506 temp = temp_last - LOWER_BOUNDING_TEMP / 3;
509 temp_mid = temp_last;
510 prob_mid = prob_last + cov_q;
511 gain = (prob_mid * BASE) / (prob_mid + cov_r);
513 temp_now = temp_mid + (gain * (temp - temp_mid) >> BASE_SHIFT);
514 prob_now = ((BASE - gain) * prob_mid) >> BASE_SHIFT;
516 prob_last = prob_now;
517 temp_last = temp_now;
520 pr_info("prob_now %d, temp_last %d, temp %d gain %d", prob_now,
521 temp_now, temp, gain);
526 static int get_raw_code_internal(void)
531 struct rk3368_thermal_data *ctx = rk3368_thermal_get_data();
536 /* power up, channel 0 */
537 writel_relaxed(0x18, ctx->regs + TSADCV2_USER_CON);
539 udelay(TSADC_CLK_CYCLE_TIME * 2);
541 writel_relaxed(0x38, ctx->regs + TSADCV2_USER_CON);
542 udelay(TSADC_CLK_CYCLE_TIME * 13);
545 for (i = 0; i < 50; i++) {
546 udelay(TSADC_CLK_CYCLE_TIME);
547 val_cpu_pd = readl_relaxed(ctx->regs + TSADCV2_INT_PD);
549 if ((val_cpu_pd & 0x100) == 0x100) {
552 writel_relaxed(0x100, ctx->regs + TSADCV2_INT_PD);
554 val_cpu = readl_relaxed(ctx->regs + TSADCV2_DATA(0));
558 /*power down, channel 0 */
559 writel_relaxed(0x0, ctx->regs + TSADCV2_USER_CON);
564 #define RAW_CODE_MIN (50)
565 #define RAW_CODE_MAX (225)
567 static int rk3368_get_raw_code(struct rk3368_thermal_data *ctx)
569 static int old_data = 130;
572 if (ctx->latency_req > ctx->latency_bound)
573 tsadc_data = scpi_thermal_get_temperature();
575 tsadc_data = get_raw_code_internal();
577 if ((tsadc_data < RAW_CODE_MIN) || (tsadc_data > RAW_CODE_MAX))
578 tsadc_data = old_data;
580 old_data = tsadc_data;
585 static int rk3368_convert_code_2_temp(int tsadc_data, int voltage)
587 struct rk3368_thermal_data *ctx = rk3368_thermal_get_data();
588 const struct rk3368_tsadc_chip *tsadc;
602 rk3368_temp_to_code(tsadc->temp_table,
603 ctx->cpu_temp_adjust * 1000, &tmp_code1);
604 rk3368_temp_to_code(tsadc->temp_table, 0, &tmp_code2);
605 data_adjust = tmp_code1 - tmp_code2;
607 ((tsadc_data * voltage - data_adjust * 1000000) + 500000) / 1000000;
608 rk3368_code_to_temp(tsadc->temp_table, code_temp, &out_temp);
611 pr_info("cpu code temp:[%d, %d], voltage: %d\n",
612 tsadc_data, out_temp / 1000, voltage);
614 if ((out_temp < MIN_TEMP) || (out_temp > MAX_TEMP))
619 ctx->cpu_temp = out_temp / 1000;
623 static int rk3368_thermal_set_trips(void *_sensor, int low, int high)
628 static int rk3368_thermal_get_temp(void *_sensor, int *out_temp)
632 struct rk3368_thermal_data *ctx = rk3368_thermal_get_data();
633 struct platform_device *pdev;
640 mutex_lock(&thermal_reg_mutex);
641 raw_code = rk3368_get_raw_code(ctx);
642 temp = rk3368_convert_code_2_temp(raw_code, ctx->regulator_uv);
643 *out_temp = predict_temp(temp / 1000) * 1000;
644 mutex_unlock(&thermal_reg_mutex);
649 static const struct thermal_zone_of_device_ops rk3368_of_thermal_ops = {
650 .get_temp = rk3368_thermal_get_temp,
651 .set_trips = rk3368_thermal_set_trips,
655 rk3368_thermal_register_sensor(struct platform_device *pdev,
656 struct rk3368_thermal_data *ctx,
657 struct rk3368_thermal_sensor *sensor, int id)
663 sensor->tzd = devm_thermal_zone_of_sensor_register(&pdev->dev, id,
665 &rk3368_of_thermal_ops);
666 if (IS_ERR(sensor->tzd)) {
667 error = PTR_ERR(sensor->tzd);
668 dev_err(&pdev->dev, "failed to register sensor %d: %d\n",
677 * Reset TSADC Controller, reset all tsadc registers.
679 static void rk3368_thermal_reset_controller(struct reset_control *reset)
681 reset_control_assert(reset);
683 reset_control_deassert(reset);
686 static ssize_t rk3368_thermal_temp_adjust_test_store(struct kobject *kobj,
687 struct kobj_attribute
688 *attr, const char *buf,
691 struct rk3368_thermal_data *ctx = rk3368_thermal_get_data();
694 const char *buftmp = buf;
700 ret = sscanf(buftmp, "%c ", &cmd);
706 ret = sscanf(buftmp, "%c %d", &cmd, &getdata);
709 ctx->cpu_temp_adjust = getdata;
710 pr_info("get cpu_temp_adjust value = %d\n", getdata);
714 ret = sscanf(buftmp, "%c %d", &cmd, &getdata);
717 ctx->gpu_temp_adjust = getdata;
718 pr_info("get gpu_temp_adjust value = %d\n", getdata);
722 pr_info("Unknown command\n");
729 static ssize_t rk3368_thermal_temp_adjust_test_show(struct kobject *kobj,
730 struct kobj_attribute
733 struct rk3368_thermal_data *ctx = rk3368_thermal_get_data();
740 sprintf(str, "rk3368_thermal: cpu:%d, gpu:%d\n",
741 ctx->cpu_temp_adjust, ctx->gpu_temp_adjust);
745 static ssize_t rk3368_thermal_temp_test_store(struct kobject *kobj,
746 struct kobj_attribute *attr,
747 const char *buf, size_t n)
749 struct rk3368_thermal_data *ctx = rk3368_thermal_get_data();
751 const char *buftmp = buf;
757 ret = sscanf(buftmp, "%c", &cmd);
769 pr_info("Unknown command\n");
776 static ssize_t rk3368_thermal_temp_test_show(struct kobject *kobj,
777 struct kobj_attribute *attr,
780 struct rk3368_thermal_data *ctx = rk3368_thermal_get_data();
786 str += sprintf(str, "current cpu_temp:%d\n", ctx->cpu_temp);
790 struct rk3368_thermal_attribute {
791 struct attribute attr;
792 ssize_t (*show) (struct kobject *kobj, struct kobj_attribute *attr,
794 ssize_t (*store) (struct kobject *kobj, struct kobj_attribute *attr,
795 const char *buf, size_t n);
798 static struct rk3368_thermal_attribute rk3368_thermal_attrs[] = {
799 /*node_name permission show_func store_func */
800 __ATTR(temp_adjust, 0644,
801 rk3368_thermal_temp_adjust_test_show,
802 rk3368_thermal_temp_adjust_test_store),
803 __ATTR(temp, 0644, rk3368_thermal_temp_test_show,
804 rk3368_thermal_temp_test_store),
807 static void rk3368_dump_temperature(void)
809 struct rk3368_thermal_data *ctx = rk3368_thermal_get_data();
810 struct platform_device *pdev;
817 if (ctx->cpu_temp != INVALID_TEMP)
818 dev_warn(&pdev->dev, "cpu channal temperature(%d C)\n",
822 pr_warn("THERMAL REGS:\n");
823 print_hex_dump(KERN_WARNING, "", DUMP_PREFIX_OFFSET,
824 32, 4, ctx->regs, 0x88, false);
827 EXPORT_SYMBOL_GPL(rk3368_dump_temperature);
829 static int rk3368_thermal_panic(struct notifier_block *this,
830 unsigned long ev, void *ptr)
832 rk3368_dump_temperature();
836 static struct notifier_block rk3368_thermal_panic_block = {
837 .notifier_call = rk3368_thermal_panic,
840 static int rk3368_thermal_notify(struct notifier_block *nb,
841 unsigned long event, void *data)
843 struct rk3368_thermal_data *ctx = rk3368_thermal_get_data();
844 struct platform_device *pdev;
851 if (event & REGULATOR_EVENT_PRE_VOLTAGE_CHANGE) {
852 mutex_lock(&thermal_reg_mutex);
853 } else if (event & (REGULATOR_EVENT_VOLTAGE_CHANGE |
854 REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE)) {
855 ctx->regulator_uv = (unsigned long)data;
856 mutex_unlock(&thermal_reg_mutex);
864 * This function gets called when a part of the kernel has a new latency
865 * requirement. We record this requirement to instruct us to get temperature.
867 static int tsadc_latency_notify(struct notifier_block *b,
868 unsigned long l, void *v)
870 struct rk3368_thermal_data *ctx = rk3368_thermal_get_data();
875 mutex_lock(&thermal_lat_mutex);
876 ctx->latency_req = (int)l;
877 mutex_unlock(&thermal_lat_mutex);
882 static struct notifier_block tsadc_latency_notifier = {
883 .notifier_call = tsadc_latency_notify,
886 static inline int tsadc_add_latency_notifier(struct notifier_block *n)
888 return pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY, n);
891 static inline int tsadc_remove_latency_notifier(struct notifier_block *n)
893 return pm_qos_remove_notifier(PM_QOS_CPU_DMA_LATENCY, n);
896 static const struct of_device_id of_rk3368_thermal_match[] = {
898 .compatible = "rockchip,rk3368-tsadc-legacy",
899 .data = (void *)&rk3368_tsadc_data,
904 MODULE_DEVICE_TABLE(of, of_rk3368_thermal_match);
906 static int rk3368_thermal_probe(struct platform_device *pdev)
908 struct device_node *np = pdev->dev.of_node;
909 struct rk3368_thermal_data *ctx;
910 const struct of_device_id *match;
911 struct resource *res;
919 match = of_match_node(of_rk3368_thermal_match, np);
923 irq = platform_get_irq(pdev, 0);
925 dev_err(&pdev->dev, "no irq resource?\n");
929 ctx = devm_kzalloc(&pdev->dev, sizeof(struct rk3368_thermal_data),
936 ctx->chip = (const struct rk3368_tsadc_chip *)match->data;
940 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
941 ctx->regs = devm_ioremap_resource(&pdev->dev, res);
942 if (IS_ERR(ctx->regs))
943 return PTR_ERR(ctx->regs);
945 ctx->reset = devm_reset_control_get(&pdev->dev, "tsadc-apb");
946 if (IS_ERR(ctx->reset)) {
947 error = PTR_ERR(ctx->reset);
948 dev_err(&pdev->dev, "failed to get tsadc reset: %d\n", error);
952 ctx->clk = devm_clk_get(&pdev->dev, "tsadc");
953 if (IS_ERR(ctx->clk)) {
954 error = PTR_ERR(ctx->clk);
955 dev_err(&pdev->dev, "failed to get tsadc clock: %d\n", error);
959 ctx->pclk = devm_clk_get(&pdev->dev, "apb_pclk");
960 if (IS_ERR(ctx->pclk)) {
961 error = PTR_ERR(ctx->pclk);
962 dev_err(&pdev->dev, "failed to get apb_pclk clock: %d\n",
967 error = clk_prepare_enable(ctx->clk);
969 dev_err(&pdev->dev, "failed to enable converter clock: %d\n",
974 error = clk_prepare_enable(ctx->pclk);
976 dev_err(&pdev->dev, "failed to enable pclk: %d\n", error);
977 goto err_disable_clk;
980 rk3368_thermal_reset_controller(ctx->reset);
982 error = rk3368_configure_from_dt(&pdev->dev, np, ctx);
984 dev_err(&pdev->dev, "failed to parse device tree data: %d\n",
986 goto err_disable_pclk;
990 ctx->ref_regulator = devm_regulator_get_optional(&pdev->dev, "tsadc");
992 if (IS_ERR(ctx->ref_regulator)) {
993 error = PTR_ERR(ctx->ref_regulator);
995 if (error != -EPROBE_DEFER)
997 "couldn't get regulator tsadc-supply\n");
998 goto err_disable_pclk;
1001 ctx->tsadc_nb.notifier_call = rk3368_thermal_notify;
1003 /* register regulator notifier */
1005 regulator_register_notifier(ctx->ref_regulator, &ctx->tsadc_nb);
1007 dev_err(&pdev->dev, "regulator notifier request failed\n");
1008 goto err_disable_pclk;
1011 uv = regulator_get_voltage(ctx->ref_regulator);
1013 dev_WARN(&pdev->dev, "regulator get failed\n");
1017 mutex_lock(&thermal_reg_mutex);
1018 if (!ctx->regulator_uv)
1019 ctx->regulator_uv = uv;
1020 mutex_unlock(&thermal_reg_mutex);
1022 error = tsadc_add_latency_notifier(&tsadc_latency_notifier);
1024 dev_err(&pdev->dev, "latency notifier request failed\n");
1025 goto err_unreg_notifier;
1028 latency_req = pm_qos_request(PM_QOS_CPU_DMA_LATENCY);
1030 mutex_lock(&thermal_lat_mutex);
1031 if (!ctx->latency_req)
1032 ctx->latency_req = latency_req;
1033 mutex_unlock(&thermal_lat_mutex);
1035 rk3368_get_ajust_code(np, &ajust_code);
1037 ctx->cpu_temp_adjust = (int)ajust_code;
1039 for (i = 0; i < ctx->chip->chn_num; i++) {
1040 error = rk3368_thermal_register_sensor(pdev, ctx,
1042 ctx->chip->chn_id[i]);
1045 "failed to register thermal sensor %d : error= %d\n",
1047 for (j = 0; j < i; j++)
1048 thermal_zone_of_sensor_unregister(&pdev->dev,
1049 ctx->sensors[j].tzd);
1050 goto err_remove_latancy_notifier;
1054 ctx->rk3368_thermal_kobj =
1055 kobject_create_and_add("rk3368_thermal", NULL);
1056 if (!ctx->rk3368_thermal_kobj) {
1059 "failed to creat debug node : error= %d\n", error);
1060 goto err_remove_latancy_notifier;
1063 for (i = 0; i < ARRAY_SIZE(rk3368_thermal_attrs); i++) {
1065 sysfs_create_file(ctx->rk3368_thermal_kobj,
1066 &rk3368_thermal_attrs[i].attr);
1069 "failed to register thermal sensor %d : error= %d\n",
1071 for (j = 0; j < i; j++)
1072 sysfs_remove_file(ctx->rk3368_thermal_kobj,
1073 &rk3368_thermal_attrs[j].attr);
1075 goto err_remove_latancy_notifier;
1079 platform_set_drvdata(pdev, ctx);
1081 atomic_notifier_chain_register(&panic_notifier_list,
1082 &rk3368_thermal_panic_block);
1084 ctx->cpu_temp = INVALID_TEMP;
1086 pr_info("rk3368 tsadc probed successfully\n");
1090 err_remove_latancy_notifier:
1091 tsadc_remove_latency_notifier(&tsadc_latency_notifier);
1093 regulator_unregister_notifier(ctx->ref_regulator, &ctx->tsadc_nb);
1096 clk_disable_unprepare(ctx->pclk);
1098 clk_disable_unprepare(ctx->clk);
1103 static int rk3368_thermal_remove(struct platform_device *pdev)
1105 struct rk3368_thermal_data *ctx = platform_get_drvdata(pdev);
1108 atomic_notifier_chain_unregister(&panic_notifier_list,
1109 &rk3368_thermal_panic_block);
1110 for (i = 0; i < ctx->chip->chn_num; i++) {
1111 struct rk3368_thermal_sensor *sensor = &ctx->sensors[i];
1113 thermal_zone_of_sensor_unregister(&pdev->dev, sensor->tzd);
1115 tsadc_remove_latency_notifier(&tsadc_latency_notifier);
1116 regulator_unregister_notifier(ctx->ref_regulator, &ctx->tsadc_nb);
1117 clk_disable_unprepare(ctx->pclk);
1118 clk_disable_unprepare(ctx->clk);
1123 static int __maybe_unused rk3368_thermal_suspend(struct device *dev)
1125 struct platform_device *pdev = to_platform_device(dev);
1126 struct rk3368_thermal_data *ctx = platform_get_drvdata(pdev);
1128 clk_disable(ctx->pclk);
1129 clk_disable(ctx->clk);
1133 static int __maybe_unused rk3368_thermal_resume(struct device *dev)
1135 struct platform_device *pdev = to_platform_device(dev);
1136 struct rk3368_thermal_data *ctx = platform_get_drvdata(pdev);
1139 error = clk_enable(ctx->clk);
1143 error = clk_enable(ctx->pclk);
1145 clk_disable(ctx->clk);
1149 rk3368_thermal_reset_controller(ctx->reset);
1154 static SIMPLE_DEV_PM_OPS(rk3368_thermal_pm_ops,
1155 rk3368_thermal_suspend, rk3368_thermal_resume);
1157 static struct platform_driver rk3368_thermal_driver = {
1159 .name = "rk3368-thermal",
1160 .pm = &rk3368_thermal_pm_ops,
1161 .of_match_table = of_rk3368_thermal_match,
1163 .probe = rk3368_thermal_probe,
1164 .remove = rk3368_thermal_remove,
1167 /* rk3368 thermal needs a clock source of 32k from rk818, so this init process
1170 static int __init rk3368_thermal_init_driver(void)
1172 return platform_driver_register(&rk3368_thermal_driver);
1174 late_initcall(rk3368_thermal_init_driver);
1176 MODULE_DESCRIPTION("ROCKCHIP THERMAL Driver");
1177 MODULE_AUTHOR("Rockchip, Inc.");
1178 MODULE_LICENSE("GPL v2");
1179 MODULE_ALIAS("platform:rk3368-thermal");