sun8i_thermal.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Thermal sensor driver for Allwinner SOC
  4. * Copyright (C) 2019 Yangtao Li
  5. *
  6. * Based on the work of Icenowy Zheng <icenowy@aosc.io>
  7. * Based on the work of Ondrej Jirman <megous@megous.com>
  8. * Based on the work of Josef Gajdusek <atx@atx.name>
  9. */
  10. #include <linux/bitmap.h>
  11. #include <linux/clk.h>
  12. #include <linux/device.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/module.h>
  15. #include <linux/nvmem-consumer.h>
  16. #include <linux/of.h>
  17. #include <linux/of_platform.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/regmap.h>
  20. #include <linux/reset.h>
  21. #include <linux/slab.h>
  22. #include <linux/thermal.h>
  23. #include "thermal_hwmon.h"
  24. #define MAX_SENSOR_NUM 4
  25. #define FT_TEMP_MASK GENMASK(11, 0)
  26. #define TEMP_CALIB_MASK GENMASK(11, 0)
  27. #define CALIBRATE_DEFAULT 0x800
  28. #define SUN8I_THS_CTRL0 0x00
  29. #define SUN8I_THS_CTRL2 0x40
  30. #define SUN8I_THS_IC 0x44
  31. #define SUN8I_THS_IS 0x48
  32. #define SUN8I_THS_MFC 0x70
  33. #define SUN8I_THS_TEMP_CALIB 0x74
  34. #define SUN8I_THS_TEMP_DATA 0x80
  35. #define SUN50I_THS_CTRL0 0x00
  36. #define SUN50I_H6_THS_ENABLE 0x04
  37. #define SUN50I_H6_THS_PC 0x08
  38. #define SUN50I_H6_THS_DIC 0x10
  39. #define SUN50I_H6_THS_DIS 0x20
  40. #define SUN50I_H6_THS_MFC 0x30
  41. #define SUN50I_H6_THS_TEMP_CALIB 0xa0
  42. #define SUN50I_H6_THS_TEMP_DATA 0xc0
  43. #define SUN8I_THS_CTRL0_T_ACQ0(x) (GENMASK(15, 0) & (x))
  44. #define SUN8I_THS_CTRL2_T_ACQ1(x) ((GENMASK(15, 0) & (x)) << 16)
  45. #define SUN8I_THS_DATA_IRQ_STS(x) BIT(x + 8)
  46. #define SUN50I_THS_CTRL0_T_ACQ(x) (GENMASK(15, 0) & ((x) - 1))
  47. #define SUN50I_THS_CTRL0_T_SAMPLE_PER(x) ((GENMASK(15, 0) & ((x) - 1)) << 16)
  48. #define SUN50I_THS_FILTER_EN BIT(2)
  49. #define SUN50I_THS_FILTER_TYPE(x) (GENMASK(1, 0) & (x))
  50. #define SUN50I_H6_THS_PC_TEMP_PERIOD(x) ((GENMASK(19, 0) & (x)) << 12)
  51. #define SUN50I_H6_THS_DATA_IRQ_STS(x) BIT(x)
  52. struct tsensor {
  53. struct ths_device *tmdev;
  54. struct thermal_zone_device *tzd;
  55. int id;
  56. };
  57. struct ths_thermal_chip {
  58. bool has_mod_clk;
  59. bool has_bus_clk_reset;
  60. bool needs_sram;
  61. int sensor_num;
  62. int offset;
  63. int scale;
  64. int ft_deviation;
  65. int temp_data_base;
  66. int (*calibrate)(struct ths_device *tmdev,
  67. u16 *caldata, int callen);
  68. int (*init)(struct ths_device *tmdev);
  69. unsigned long (*irq_ack)(struct ths_device *tmdev);
  70. int (*calc_temp)(struct ths_device *tmdev,
  71. int id, int reg);
  72. };
  73. struct ths_device {
  74. const struct ths_thermal_chip *chip;
  75. struct device *dev;
  76. struct regmap *regmap;
  77. struct regmap_field *sram_regmap_field;
  78. struct reset_control *reset;
  79. struct clk *bus_clk;
  80. struct clk *mod_clk;
  81. struct tsensor sensor[MAX_SENSOR_NUM];
  82. };
  83. /* The H616 needs to have a bit 16 in the SRAM control register cleared. */
  84. static const struct reg_field sun8i_ths_sram_reg_field = REG_FIELD(0x0, 16, 16);
  85. /* Temp Unit: millidegree Celsius */
  86. static int sun8i_ths_calc_temp(struct ths_device *tmdev,
  87. int id, int reg)
  88. {
  89. return tmdev->chip->offset - (reg * tmdev->chip->scale / 10);
  90. }
  91. static int sun50i_h5_calc_temp(struct ths_device *tmdev,
  92. int id, int reg)
  93. {
  94. if (reg >= 0x500)
  95. return -1191 * reg / 10 + 223000;
  96. else if (!id)
  97. return -1452 * reg / 10 + 259000;
  98. else
  99. return -1590 * reg / 10 + 276000;
  100. }
  101. static int sun8i_ths_get_temp(struct thermal_zone_device *tz, int *temp)
  102. {
  103. struct tsensor *s = thermal_zone_device_priv(tz);
  104. struct ths_device *tmdev = s->tmdev;
  105. int val = 0;
  106. regmap_read(tmdev->regmap, tmdev->chip->temp_data_base +
  107. 0x4 * s->id, &val);
  108. /* ths have no data yet */
  109. if (!val)
  110. return -EAGAIN;
  111. *temp = tmdev->chip->calc_temp(tmdev, s->id, val);
  112. /*
  113. * According to the original sdk, there are some platforms(rarely)
  114. * that add a fixed offset value after calculating the temperature
  115. * value. We can't simply put it on the formula for calculating the
  116. * temperature above, because the formula for calculating the
  117. * temperature above is also used when the sensor is calibrated. If
  118. * do this, the correct calibration formula is hard to know.
  119. */
  120. *temp += tmdev->chip->ft_deviation;
  121. return 0;
  122. }
  123. static const struct thermal_zone_device_ops ths_ops = {
  124. .get_temp = sun8i_ths_get_temp,
  125. };
  126. static const struct regmap_config config = {
  127. .reg_bits = 32,
  128. .val_bits = 32,
  129. .reg_stride = 4,
  130. .fast_io = true,
  131. .max_register = 0xfc,
  132. };
  133. static unsigned long sun8i_h3_irq_ack(struct ths_device *tmdev)
  134. {
  135. unsigned long irq_bitmap = 0;
  136. int i, state;
  137. regmap_read(tmdev->regmap, SUN8I_THS_IS, &state);
  138. for (i = 0; i < tmdev->chip->sensor_num; i++) {
  139. if (state & SUN8I_THS_DATA_IRQ_STS(i)) {
  140. regmap_write(tmdev->regmap, SUN8I_THS_IS,
  141. SUN8I_THS_DATA_IRQ_STS(i));
  142. bitmap_set(&irq_bitmap, i, 1);
  143. }
  144. }
  145. return irq_bitmap;
  146. }
  147. static unsigned long sun50i_h6_irq_ack(struct ths_device *tmdev)
  148. {
  149. unsigned long irq_bitmap = 0;
  150. int i, state;
  151. regmap_read(tmdev->regmap, SUN50I_H6_THS_DIS, &state);
  152. for (i = 0; i < tmdev->chip->sensor_num; i++) {
  153. if (state & SUN50I_H6_THS_DATA_IRQ_STS(i)) {
  154. regmap_write(tmdev->regmap, SUN50I_H6_THS_DIS,
  155. SUN50I_H6_THS_DATA_IRQ_STS(i));
  156. bitmap_set(&irq_bitmap, i, 1);
  157. }
  158. }
  159. return irq_bitmap;
  160. }
  161. static irqreturn_t sun8i_irq_thread(int irq, void *data)
  162. {
  163. struct ths_device *tmdev = data;
  164. unsigned long irq_bitmap = tmdev->chip->irq_ack(tmdev);
  165. int i;
  166. for_each_set_bit(i, &irq_bitmap, tmdev->chip->sensor_num) {
  167. /* We allow some zones to not register. */
  168. if (IS_ERR(tmdev->sensor[i].tzd))
  169. continue;
  170. thermal_zone_device_update(tmdev->sensor[i].tzd,
  171. THERMAL_EVENT_UNSPECIFIED);
  172. }
  173. return IRQ_HANDLED;
  174. }
  175. static int sun8i_h3_ths_calibrate(struct ths_device *tmdev,
  176. u16 *caldata, int callen)
  177. {
  178. int i;
  179. if (!caldata[0] || callen < 2 * tmdev->chip->sensor_num)
  180. return -EINVAL;
  181. for (i = 0; i < tmdev->chip->sensor_num; i++) {
  182. int offset = (i % 2) << 4;
  183. regmap_update_bits(tmdev->regmap,
  184. SUN8I_THS_TEMP_CALIB + (4 * (i >> 1)),
  185. TEMP_CALIB_MASK << offset,
  186. caldata[i] << offset);
  187. }
  188. return 0;
  189. }
  190. static int sun50i_h6_ths_calibrate(struct ths_device *tmdev,
  191. u16 *caldata, int callen)
  192. {
  193. struct device *dev = tmdev->dev;
  194. int i, ft_temp;
  195. if (!caldata[0])
  196. return -EINVAL;
  197. /*
  198. * efuse layout:
  199. *
  200. * 0 11 16 27 32 43 48 57
  201. * +----------+-----------+-----------+-----------+
  202. * | temp | |sensor0| |sensor1| |sensor2| |
  203. * +----------+-----------+-----------+-----------+
  204. * ^ ^ ^
  205. * | | |
  206. * | | sensor3[11:8]
  207. * | sensor3[7:4]
  208. * sensor3[3:0]
  209. *
  210. * The calibration data on the H6 is the ambient temperature and
  211. * sensor values that are filled during the factory test stage.
  212. *
  213. * The unit of stored FT temperature is 0.1 degree celsius.
  214. *
  215. * We need to calculate a delta between measured and caluclated
  216. * register values and this will become a calibration offset.
  217. */
  218. ft_temp = (caldata[0] & FT_TEMP_MASK) * 100;
  219. for (i = 0; i < tmdev->chip->sensor_num; i++) {
  220. int sensor_reg, sensor_temp, cdata, offset;
  221. if (i == 3)
  222. sensor_reg = (caldata[1] >> 12)
  223. | ((caldata[2] >> 12) << 4)
  224. | ((caldata[3] >> 12) << 8);
  225. else
  226. sensor_reg = caldata[i + 1] & TEMP_CALIB_MASK;
  227. sensor_temp = tmdev->chip->calc_temp(tmdev, i, sensor_reg);
  228. /*
  229. * Calibration data is CALIBRATE_DEFAULT - (calculated
  230. * temperature from sensor reading at factory temperature
  231. * minus actual factory temperature) * 14.88 (scale from
  232. * temperature to register values)
  233. */
  234. cdata = CALIBRATE_DEFAULT -
  235. ((sensor_temp - ft_temp) * 10 / tmdev->chip->scale);
  236. if (cdata & ~TEMP_CALIB_MASK) {
  237. /*
  238. * Calibration value more than 12-bit, but calibration
  239. * register is 12-bit. In this case, ths hardware can
  240. * still work without calibration, although the data
  241. * won't be so accurate.
  242. */
  243. dev_warn(dev, "sensor%d is not calibrated.\n", i);
  244. continue;
  245. }
  246. offset = (i % 2) * 16;
  247. regmap_update_bits(tmdev->regmap,
  248. SUN50I_H6_THS_TEMP_CALIB + (i / 2 * 4),
  249. TEMP_CALIB_MASK << offset,
  250. cdata << offset);
  251. }
  252. return 0;
  253. }
  254. static int sun8i_ths_calibrate(struct ths_device *tmdev)
  255. {
  256. struct nvmem_cell *calcell;
  257. struct device *dev = tmdev->dev;
  258. u16 *caldata;
  259. size_t callen;
  260. int ret = 0;
  261. calcell = nvmem_cell_get(dev, "calibration");
  262. if (IS_ERR(calcell)) {
  263. if (PTR_ERR(calcell) == -EPROBE_DEFER)
  264. return -EPROBE_DEFER;
  265. /*
  266. * Even if the external calibration data stored in sid is
  267. * not accessible, the THS hardware can still work, although
  268. * the data won't be so accurate.
  269. *
  270. * The default value of calibration register is 0x800 for
  271. * every sensor, and the calibration value is usually 0x7xx
  272. * or 0x8xx, so they won't be away from the default value
  273. * for a lot.
  274. *
  275. * So here we do not return error if the calibration data is
  276. * not available, except the probe needs deferring.
  277. */
  278. goto out;
  279. }
  280. caldata = nvmem_cell_read(calcell, &callen);
  281. if (IS_ERR(caldata)) {
  282. ret = PTR_ERR(caldata);
  283. goto out;
  284. }
  285. tmdev->chip->calibrate(tmdev, caldata, callen);
  286. kfree(caldata);
  287. out:
  288. if (!IS_ERR(calcell))
  289. nvmem_cell_put(calcell);
  290. return ret;
  291. }
  292. static void sun8i_ths_reset_control_assert(void *data)
  293. {
  294. reset_control_assert(data);
  295. }
  296. static struct regmap *sun8i_ths_get_sram_regmap(struct device_node *node)
  297. {
  298. struct device_node *sram_node;
  299. struct platform_device *sram_pdev;
  300. struct regmap *regmap = NULL;
  301. sram_node = of_parse_phandle(node, "allwinner,sram", 0);
  302. if (!sram_node)
  303. return ERR_PTR(-ENODEV);
  304. sram_pdev = of_find_device_by_node(sram_node);
  305. if (!sram_pdev) {
  306. /* platform device might not be probed yet */
  307. regmap = ERR_PTR(-EPROBE_DEFER);
  308. goto out_put_node;
  309. }
  310. /* If no regmap is found then the other device driver is at fault */
  311. regmap = dev_get_regmap(&sram_pdev->dev, NULL);
  312. if (!regmap)
  313. regmap = ERR_PTR(-EINVAL);
  314. platform_device_put(sram_pdev);
  315. out_put_node:
  316. of_node_put(sram_node);
  317. return regmap;
  318. }
  319. static int sun8i_ths_resource_init(struct ths_device *tmdev)
  320. {
  321. struct device *dev = tmdev->dev;
  322. struct platform_device *pdev = to_platform_device(dev);
  323. void __iomem *base;
  324. int ret;
  325. base = devm_platform_ioremap_resource(pdev, 0);
  326. if (IS_ERR(base))
  327. return PTR_ERR(base);
  328. tmdev->regmap = devm_regmap_init_mmio(dev, base, &config);
  329. if (IS_ERR(tmdev->regmap))
  330. return PTR_ERR(tmdev->regmap);
  331. if (tmdev->chip->has_bus_clk_reset) {
  332. tmdev->reset = devm_reset_control_get(dev, NULL);
  333. if (IS_ERR(tmdev->reset))
  334. return PTR_ERR(tmdev->reset);
  335. ret = reset_control_deassert(tmdev->reset);
  336. if (ret)
  337. return ret;
  338. ret = devm_add_action_or_reset(dev, sun8i_ths_reset_control_assert,
  339. tmdev->reset);
  340. if (ret)
  341. return ret;
  342. tmdev->bus_clk = devm_clk_get_enabled(&pdev->dev, "bus");
  343. if (IS_ERR(tmdev->bus_clk))
  344. return PTR_ERR(tmdev->bus_clk);
  345. }
  346. if (tmdev->chip->has_mod_clk) {
  347. tmdev->mod_clk = devm_clk_get_enabled(&pdev->dev, "mod");
  348. if (IS_ERR(tmdev->mod_clk))
  349. return PTR_ERR(tmdev->mod_clk);
  350. }
  351. ret = clk_set_rate(tmdev->mod_clk, 24000000);
  352. if (ret)
  353. return ret;
  354. if (tmdev->chip->needs_sram) {
  355. struct regmap *regmap;
  356. regmap = sun8i_ths_get_sram_regmap(dev->of_node);
  357. if (IS_ERR(regmap))
  358. return PTR_ERR(regmap);
  359. tmdev->sram_regmap_field = devm_regmap_field_alloc(dev,
  360. regmap,
  361. sun8i_ths_sram_reg_field);
  362. if (IS_ERR(tmdev->sram_regmap_field))
  363. return PTR_ERR(tmdev->sram_regmap_field);
  364. }
  365. ret = sun8i_ths_calibrate(tmdev);
  366. if (ret)
  367. return ret;
  368. return 0;
  369. }
  370. static int sun8i_h3_thermal_init(struct ths_device *tmdev)
  371. {
  372. int val;
  373. /* average over 4 samples */
  374. regmap_write(tmdev->regmap, SUN8I_THS_MFC,
  375. SUN50I_THS_FILTER_EN |
  376. SUN50I_THS_FILTER_TYPE(1));
  377. /*
  378. * clkin = 24MHz
  379. * filter_samples = 4
  380. * period = 0.25s
  381. *
  382. * x = period * clkin / 4096 / filter_samples - 1
  383. * = 365
  384. */
  385. val = GENMASK(7 + tmdev->chip->sensor_num, 8);
  386. regmap_write(tmdev->regmap, SUN8I_THS_IC,
  387. SUN50I_H6_THS_PC_TEMP_PERIOD(365) | val);
  388. /*
  389. * T_acq = 20us
  390. * clkin = 24MHz
  391. *
  392. * x = T_acq * clkin - 1
  393. * = 479
  394. */
  395. regmap_write(tmdev->regmap, SUN8I_THS_CTRL0,
  396. SUN8I_THS_CTRL0_T_ACQ0(479));
  397. val = GENMASK(tmdev->chip->sensor_num - 1, 0);
  398. regmap_write(tmdev->regmap, SUN8I_THS_CTRL2,
  399. SUN8I_THS_CTRL2_T_ACQ1(479) | val);
  400. return 0;
  401. }
  402. static int sun50i_h6_thermal_init(struct ths_device *tmdev)
  403. {
  404. int val;
  405. /* The H616 needs to have a bit in the SRAM control register cleared. */
  406. if (tmdev->sram_regmap_field)
  407. regmap_field_write(tmdev->sram_regmap_field, 0);
  408. /*
  409. * The manual recommends an overall sample frequency of 50 KHz (20us,
  410. * 480 cycles at 24 MHz), which provides plenty of time for both the
  411. * acquisition time (>24 cycles) and the actual conversion time
  412. * (>14 cycles).
  413. * The lower half of the CTRL register holds the "acquire time", in
  414. * clock cycles, which the manual recommends to be 2us:
  415. * 24MHz * 2us = 48 cycles.
  416. * The high half of THS_CTRL encodes the sample frequency, in clock
  417. * cycles: 24MHz * 20us = 480 cycles.
  418. * This is explained in the H616 manual, but apparently wrongly
  419. * described in the H6 manual, although the BSP code does the same
  420. * for both SoCs.
  421. */
  422. regmap_write(tmdev->regmap, SUN50I_THS_CTRL0,
  423. SUN50I_THS_CTRL0_T_ACQ(48) |
  424. SUN50I_THS_CTRL0_T_SAMPLE_PER(480));
  425. /* average over 4 samples */
  426. regmap_write(tmdev->regmap, SUN50I_H6_THS_MFC,
  427. SUN50I_THS_FILTER_EN |
  428. SUN50I_THS_FILTER_TYPE(1));
  429. /*
  430. * clkin = 24MHz
  431. * filter_samples = 4
  432. * period = 0.25s
  433. *
  434. * x = period * clkin / 4096 / filter_samples - 1
  435. * = 365
  436. */
  437. regmap_write(tmdev->regmap, SUN50I_H6_THS_PC,
  438. SUN50I_H6_THS_PC_TEMP_PERIOD(365));
  439. /* enable sensor */
  440. val = GENMASK(tmdev->chip->sensor_num - 1, 0);
  441. regmap_write(tmdev->regmap, SUN50I_H6_THS_ENABLE, val);
  442. /* thermal data interrupt enable */
  443. val = GENMASK(tmdev->chip->sensor_num - 1, 0);
  444. regmap_write(tmdev->regmap, SUN50I_H6_THS_DIC, val);
  445. return 0;
  446. }
  447. static int sun8i_ths_register(struct ths_device *tmdev)
  448. {
  449. int i;
  450. for (i = 0; i < tmdev->chip->sensor_num; i++) {
  451. tmdev->sensor[i].tmdev = tmdev;
  452. tmdev->sensor[i].id = i;
  453. tmdev->sensor[i].tzd =
  454. devm_thermal_of_zone_register(tmdev->dev,
  455. i,
  456. &tmdev->sensor[i],
  457. &ths_ops);
  458. /*
  459. * If an individual zone fails to register for reasons
  460. * other than probe deferral (eg, a bad DT) then carry
  461. * on, other zones might register successfully.
  462. */
  463. if (IS_ERR(tmdev->sensor[i].tzd)) {
  464. if (PTR_ERR(tmdev->sensor[i].tzd) == -EPROBE_DEFER)
  465. return PTR_ERR(tmdev->sensor[i].tzd);
  466. continue;
  467. }
  468. devm_thermal_add_hwmon_sysfs(tmdev->dev, tmdev->sensor[i].tzd);
  469. }
  470. return 0;
  471. }
  472. static int sun8i_ths_probe(struct platform_device *pdev)
  473. {
  474. struct ths_device *tmdev;
  475. struct device *dev = &pdev->dev;
  476. int ret, irq;
  477. tmdev = devm_kzalloc(dev, sizeof(*tmdev), GFP_KERNEL);
  478. if (!tmdev)
  479. return -ENOMEM;
  480. tmdev->dev = dev;
  481. tmdev->chip = of_device_get_match_data(&pdev->dev);
  482. if (!tmdev->chip)
  483. return -EINVAL;
  484. ret = sun8i_ths_resource_init(tmdev);
  485. if (ret)
  486. return ret;
  487. irq = platform_get_irq(pdev, 0);
  488. if (irq < 0)
  489. return irq;
  490. ret = tmdev->chip->init(tmdev);
  491. if (ret)
  492. return ret;
  493. ret = sun8i_ths_register(tmdev);
  494. if (ret)
  495. return ret;
  496. /*
  497. * Avoid entering the interrupt handler, the thermal device is not
  498. * registered yet, we deffer the registration of the interrupt to
  499. * the end.
  500. */
  501. ret = devm_request_threaded_irq(dev, irq, NULL,
  502. sun8i_irq_thread,
  503. IRQF_ONESHOT, "ths", tmdev);
  504. if (ret)
  505. return ret;
  506. return 0;
  507. }
  508. static const struct ths_thermal_chip sun8i_a83t_ths = {
  509. .sensor_num = 3,
  510. .scale = 705,
  511. .offset = 191668,
  512. .temp_data_base = SUN8I_THS_TEMP_DATA,
  513. .calibrate = sun8i_h3_ths_calibrate,
  514. .init = sun8i_h3_thermal_init,
  515. .irq_ack = sun8i_h3_irq_ack,
  516. .calc_temp = sun8i_ths_calc_temp,
  517. };
  518. static const struct ths_thermal_chip sun8i_h3_ths = {
  519. .sensor_num = 1,
  520. .scale = 1211,
  521. .offset = 217000,
  522. .has_mod_clk = true,
  523. .has_bus_clk_reset = true,
  524. .temp_data_base = SUN8I_THS_TEMP_DATA,
  525. .calibrate = sun8i_h3_ths_calibrate,
  526. .init = sun8i_h3_thermal_init,
  527. .irq_ack = sun8i_h3_irq_ack,
  528. .calc_temp = sun8i_ths_calc_temp,
  529. };
  530. static const struct ths_thermal_chip sun8i_r40_ths = {
  531. .sensor_num = 2,
  532. .offset = 251086,
  533. .scale = 1130,
  534. .has_mod_clk = true,
  535. .has_bus_clk_reset = true,
  536. .temp_data_base = SUN8I_THS_TEMP_DATA,
  537. .calibrate = sun8i_h3_ths_calibrate,
  538. .init = sun8i_h3_thermal_init,
  539. .irq_ack = sun8i_h3_irq_ack,
  540. .calc_temp = sun8i_ths_calc_temp,
  541. };
  542. static const struct ths_thermal_chip sun50i_a64_ths = {
  543. .sensor_num = 3,
  544. .offset = 260890,
  545. .scale = 1170,
  546. .has_mod_clk = true,
  547. .has_bus_clk_reset = true,
  548. .temp_data_base = SUN8I_THS_TEMP_DATA,
  549. .calibrate = sun8i_h3_ths_calibrate,
  550. .init = sun8i_h3_thermal_init,
  551. .irq_ack = sun8i_h3_irq_ack,
  552. .calc_temp = sun8i_ths_calc_temp,
  553. };
  554. static const struct ths_thermal_chip sun50i_a100_ths = {
  555. .sensor_num = 3,
  556. .has_bus_clk_reset = true,
  557. .ft_deviation = 8000,
  558. .offset = 187744,
  559. .scale = 672,
  560. .temp_data_base = SUN50I_H6_THS_TEMP_DATA,
  561. .calibrate = sun50i_h6_ths_calibrate,
  562. .init = sun50i_h6_thermal_init,
  563. .irq_ack = sun50i_h6_irq_ack,
  564. .calc_temp = sun8i_ths_calc_temp,
  565. };
  566. static const struct ths_thermal_chip sun50i_h5_ths = {
  567. .sensor_num = 2,
  568. .has_mod_clk = true,
  569. .has_bus_clk_reset = true,
  570. .temp_data_base = SUN8I_THS_TEMP_DATA,
  571. .calibrate = sun8i_h3_ths_calibrate,
  572. .init = sun8i_h3_thermal_init,
  573. .irq_ack = sun8i_h3_irq_ack,
  574. .calc_temp = sun50i_h5_calc_temp,
  575. };
  576. static const struct ths_thermal_chip sun50i_h6_ths = {
  577. .sensor_num = 2,
  578. .has_bus_clk_reset = true,
  579. .ft_deviation = 7000,
  580. .offset = 187744,
  581. .scale = 672,
  582. .temp_data_base = SUN50I_H6_THS_TEMP_DATA,
  583. .calibrate = sun50i_h6_ths_calibrate,
  584. .init = sun50i_h6_thermal_init,
  585. .irq_ack = sun50i_h6_irq_ack,
  586. .calc_temp = sun8i_ths_calc_temp,
  587. };
  588. static const struct ths_thermal_chip sun20i_d1_ths = {
  589. .sensor_num = 1,
  590. .has_bus_clk_reset = true,
  591. .offset = 188552,
  592. .scale = 673,
  593. .temp_data_base = SUN50I_H6_THS_TEMP_DATA,
  594. .calibrate = sun50i_h6_ths_calibrate,
  595. .init = sun50i_h6_thermal_init,
  596. .irq_ack = sun50i_h6_irq_ack,
  597. .calc_temp = sun8i_ths_calc_temp,
  598. };
  599. static const struct ths_thermal_chip sun50i_h616_ths = {
  600. .sensor_num = 4,
  601. .has_bus_clk_reset = true,
  602. .needs_sram = true,
  603. .ft_deviation = 8000,
  604. .offset = 263655,
  605. .scale = 810,
  606. .temp_data_base = SUN50I_H6_THS_TEMP_DATA,
  607. .calibrate = sun50i_h6_ths_calibrate,
  608. .init = sun50i_h6_thermal_init,
  609. .irq_ack = sun50i_h6_irq_ack,
  610. .calc_temp = sun8i_ths_calc_temp,
  611. };
  612. static const struct of_device_id of_ths_match[] = {
  613. { .compatible = "allwinner,sun8i-a83t-ths", .data = &sun8i_a83t_ths },
  614. { .compatible = "allwinner,sun8i-h3-ths", .data = &sun8i_h3_ths },
  615. { .compatible = "allwinner,sun8i-r40-ths", .data = &sun8i_r40_ths },
  616. { .compatible = "allwinner,sun50i-a64-ths", .data = &sun50i_a64_ths },
  617. { .compatible = "allwinner,sun50i-a100-ths", .data = &sun50i_a100_ths },
  618. { .compatible = "allwinner,sun50i-h5-ths", .data = &sun50i_h5_ths },
  619. { .compatible = "allwinner,sun50i-h6-ths", .data = &sun50i_h6_ths },
  620. { .compatible = "allwinner,sun20i-d1-ths", .data = &sun20i_d1_ths },
  621. { .compatible = "allwinner,sun50i-h616-ths", .data = &sun50i_h616_ths },
  622. { /* sentinel */ },
  623. };
  624. MODULE_DEVICE_TABLE(of, of_ths_match);
  625. static struct platform_driver ths_driver = {
  626. .probe = sun8i_ths_probe,
  627. .driver = {
  628. .name = "sun8i-thermal",
  629. .of_match_table = of_ths_match,
  630. },
  631. };
  632. module_platform_driver(ths_driver);
  633. MODULE_DESCRIPTION("Thermal sensor driver for Allwinner SOC");
  634. MODULE_LICENSE("GPL v2");