tegra30-tsensor.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Tegra30 SoC Thermal Sensor driver
  4. *
  5. * Based on downstream HWMON driver from NVIDIA.
  6. * Copyright (C) 2011 NVIDIA Corporation
  7. *
  8. * Author: Dmitry Osipenko <digetx@gmail.com>
  9. * Copyright (C) 2021 GRATE-DRIVER project
  10. */
  11. #include <linux/bitfield.h>
  12. #include <linux/clk.h>
  13. #include <linux/delay.h>
  14. #include <linux/errno.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/io.h>
  17. #include <linux/iopoll.h>
  18. #include <linux/math.h>
  19. #include <linux/module.h>
  20. #include <linux/of.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/pm.h>
  23. #include <linux/reset.h>
  24. #include <linux/slab.h>
  25. #include <linux/thermal.h>
  26. #include <linux/types.h>
  27. #include <soc/tegra/fuse.h>
  28. #include "../thermal_hwmon.h"
  29. #define TSENSOR_SENSOR0_CONFIG0 0x0
  30. #define TSENSOR_SENSOR0_CONFIG0_SENSOR_STOP BIT(0)
  31. #define TSENSOR_SENSOR0_CONFIG0_HW_FREQ_DIV_EN BIT(1)
  32. #define TSENSOR_SENSOR0_CONFIG0_THERMAL_RST_EN BIT(2)
  33. #define TSENSOR_SENSOR0_CONFIG0_DVFS_EN BIT(3)
  34. #define TSENSOR_SENSOR0_CONFIG0_INTR_OVERFLOW_EN BIT(4)
  35. #define TSENSOR_SENSOR0_CONFIG0_INTR_HW_FREQ_DIV_EN BIT(5)
  36. #define TSENSOR_SENSOR0_CONFIG0_INTR_THERMAL_RST_EN BIT(6)
  37. #define TSENSOR_SENSOR0_CONFIG0_M GENMASK(23, 8)
  38. #define TSENSOR_SENSOR0_CONFIG0_N GENMASK(31, 24)
  39. #define TSENSOR_SENSOR0_CONFIG1 0x8
  40. #define TSENSOR_SENSOR0_CONFIG1_TH1 GENMASK(15, 0)
  41. #define TSENSOR_SENSOR0_CONFIG1_TH2 GENMASK(31, 16)
  42. #define TSENSOR_SENSOR0_CONFIG2 0xc
  43. #define TSENSOR_SENSOR0_CONFIG2_TH3 GENMASK(15, 0)
  44. #define TSENSOR_SENSOR0_STATUS0 0x18
  45. #define TSENSOR_SENSOR0_STATUS0_STATE GENMASK(2, 0)
  46. #define TSENSOR_SENSOR0_STATUS0_INTR BIT(8)
  47. #define TSENSOR_SENSOR0_STATUS0_CURRENT_VALID BIT(9)
  48. #define TSENSOR_SENSOR0_TS_STATUS1 0x1c
  49. #define TSENSOR_SENSOR0_TS_STATUS1_CURRENT_COUNT GENMASK(31, 16)
  50. #define TEGRA30_FUSE_TEST_PROG_VER 0x28
  51. #define TEGRA30_FUSE_TSENSOR_CALIB 0x98
  52. #define TEGRA30_FUSE_TSENSOR_CALIB_LOW GENMASK(15, 0)
  53. #define TEGRA30_FUSE_TSENSOR_CALIB_HIGH GENMASK(31, 16)
  54. #define TEGRA30_FUSE_SPARE_BIT 0x144
  55. struct tegra_tsensor;
  56. struct tegra_tsensor_calibration_data {
  57. int a, b, m, n, p, r;
  58. };
  59. struct tegra_tsensor_channel {
  60. void __iomem *regs;
  61. unsigned int id;
  62. struct tegra_tsensor *ts;
  63. struct thermal_zone_device *tzd;
  64. };
  65. struct tegra_tsensor {
  66. void __iomem *regs;
  67. bool swap_channels;
  68. struct clk *clk;
  69. struct device *dev;
  70. struct reset_control *rst;
  71. struct tegra_tsensor_channel ch[2];
  72. struct tegra_tsensor_calibration_data calib;
  73. };
  74. static int tegra_tsensor_hw_enable(const struct tegra_tsensor *ts)
  75. {
  76. u32 val;
  77. int err;
  78. err = reset_control_assert(ts->rst);
  79. if (err) {
  80. dev_err(ts->dev, "failed to assert hardware reset: %d\n", err);
  81. return err;
  82. }
  83. err = clk_prepare_enable(ts->clk);
  84. if (err) {
  85. dev_err(ts->dev, "failed to enable clock: %d\n", err);
  86. return err;
  87. }
  88. fsleep(1000);
  89. err = reset_control_deassert(ts->rst);
  90. if (err) {
  91. dev_err(ts->dev, "failed to deassert hardware reset: %d\n", err);
  92. goto disable_clk;
  93. }
  94. /*
  95. * Sensors are enabled after reset by default, but not gauging
  96. * until clock counter is programmed.
  97. *
  98. * M: number of reference clock pulses after which every
  99. * temperature / voltage measurement is made
  100. *
  101. * N: number of reference clock counts for which the counter runs
  102. */
  103. val = FIELD_PREP(TSENSOR_SENSOR0_CONFIG0_M, 12500);
  104. val |= FIELD_PREP(TSENSOR_SENSOR0_CONFIG0_N, 255);
  105. /* apply the same configuration to both channels */
  106. writel_relaxed(val, ts->regs + 0x40 + TSENSOR_SENSOR0_CONFIG0);
  107. writel_relaxed(val, ts->regs + 0x80 + TSENSOR_SENSOR0_CONFIG0);
  108. return 0;
  109. disable_clk:
  110. clk_disable_unprepare(ts->clk);
  111. return err;
  112. }
  113. static int tegra_tsensor_hw_disable(const struct tegra_tsensor *ts)
  114. {
  115. int err;
  116. err = reset_control_assert(ts->rst);
  117. if (err) {
  118. dev_err(ts->dev, "failed to assert hardware reset: %d\n", err);
  119. return err;
  120. }
  121. clk_disable_unprepare(ts->clk);
  122. return 0;
  123. }
  124. static void devm_tegra_tsensor_hw_disable(void *data)
  125. {
  126. const struct tegra_tsensor *ts = data;
  127. tegra_tsensor_hw_disable(ts);
  128. }
  129. static int tegra_tsensor_get_temp(struct thermal_zone_device *tz, int *temp)
  130. {
  131. const struct tegra_tsensor_channel *tsc = thermal_zone_device_priv(tz);
  132. const struct tegra_tsensor *ts = tsc->ts;
  133. int err, c1, c2, c3, c4, counter;
  134. u32 val;
  135. /*
  136. * Counter will be invalid if hardware is misprogrammed or not enough
  137. * time passed since the time when sensor was enabled.
  138. */
  139. err = readl_relaxed_poll_timeout(tsc->regs + TSENSOR_SENSOR0_STATUS0, val,
  140. val & TSENSOR_SENSOR0_STATUS0_CURRENT_VALID,
  141. 21 * USEC_PER_MSEC,
  142. 21 * USEC_PER_MSEC * 50);
  143. if (err) {
  144. dev_err_once(ts->dev, "ch%u: counter invalid\n", tsc->id);
  145. return err;
  146. }
  147. val = readl_relaxed(tsc->regs + TSENSOR_SENSOR0_TS_STATUS1);
  148. counter = FIELD_GET(TSENSOR_SENSOR0_TS_STATUS1_CURRENT_COUNT, val);
  149. /*
  150. * This shouldn't happen with a valid counter status, nevertheless
  151. * lets verify the value since it's in a separate (from status)
  152. * register.
  153. */
  154. if (counter == 0xffff) {
  155. dev_err_once(ts->dev, "ch%u: counter overflow\n", tsc->id);
  156. return -EINVAL;
  157. }
  158. /*
  159. * temperature = a * counter + b
  160. * temperature = m * (temperature ^ 2) + n * temperature + p
  161. */
  162. c1 = DIV_ROUND_CLOSEST(ts->calib.a * counter + ts->calib.b, 1000000);
  163. c1 = c1 ?: 1;
  164. c2 = DIV_ROUND_CLOSEST(ts->calib.p, c1);
  165. c3 = c1 * ts->calib.m;
  166. c4 = ts->calib.n;
  167. *temp = DIV_ROUND_CLOSEST(c1 * (c2 + c3 + c4), 1000);
  168. return 0;
  169. }
  170. static int tegra_tsensor_temp_to_counter(const struct tegra_tsensor *ts, int temp)
  171. {
  172. int c1, c2;
  173. c1 = DIV_ROUND_CLOSEST(ts->calib.p - temp * 1000, ts->calib.m);
  174. c2 = -ts->calib.r - int_sqrt(ts->calib.r * ts->calib.r - c1);
  175. return DIV_ROUND_CLOSEST(c2 * 1000000 - ts->calib.b, ts->calib.a);
  176. }
  177. static int tegra_tsensor_set_trips(struct thermal_zone_device *tz, int low, int high)
  178. {
  179. const struct tegra_tsensor_channel *tsc = thermal_zone_device_priv(tz);
  180. const struct tegra_tsensor *ts = tsc->ts;
  181. u32 val;
  182. /*
  183. * TSENSOR doesn't trigger interrupt on the "low" temperature breach,
  184. * hence bail out if high temperature is unspecified.
  185. */
  186. if (high == INT_MAX)
  187. return 0;
  188. val = readl_relaxed(tsc->regs + TSENSOR_SENSOR0_CONFIG1);
  189. val &= ~TSENSOR_SENSOR0_CONFIG1_TH1;
  190. high = tegra_tsensor_temp_to_counter(ts, high);
  191. val |= FIELD_PREP(TSENSOR_SENSOR0_CONFIG1_TH1, high);
  192. writel_relaxed(val, tsc->regs + TSENSOR_SENSOR0_CONFIG1);
  193. return 0;
  194. }
  195. static const struct thermal_zone_device_ops ops = {
  196. .get_temp = tegra_tsensor_get_temp,
  197. .set_trips = tegra_tsensor_set_trips,
  198. };
  199. static bool
  200. tegra_tsensor_handle_channel_interrupt(const struct tegra_tsensor *ts,
  201. unsigned int id)
  202. {
  203. const struct tegra_tsensor_channel *tsc = &ts->ch[id];
  204. u32 val;
  205. val = readl_relaxed(tsc->regs + TSENSOR_SENSOR0_STATUS0);
  206. writel_relaxed(val, tsc->regs + TSENSOR_SENSOR0_STATUS0);
  207. if (FIELD_GET(TSENSOR_SENSOR0_STATUS0_STATE, val) == 5)
  208. dev_err_ratelimited(ts->dev, "ch%u: counter overflowed\n", id);
  209. if (!FIELD_GET(TSENSOR_SENSOR0_STATUS0_INTR, val))
  210. return false;
  211. thermal_zone_device_update(tsc->tzd, THERMAL_EVENT_UNSPECIFIED);
  212. return true;
  213. }
  214. static irqreturn_t tegra_tsensor_isr(int irq, void *data)
  215. {
  216. const struct tegra_tsensor *ts = data;
  217. bool handled = false;
  218. unsigned int i;
  219. for (i = 0; i < ARRAY_SIZE(ts->ch); i++)
  220. handled |= tegra_tsensor_handle_channel_interrupt(ts, i);
  221. return handled ? IRQ_HANDLED : IRQ_NONE;
  222. }
  223. static int tegra_tsensor_disable_hw_channel(const struct tegra_tsensor *ts,
  224. unsigned int id)
  225. {
  226. const struct tegra_tsensor_channel *tsc = &ts->ch[id];
  227. struct thermal_zone_device *tzd = tsc->tzd;
  228. u32 val;
  229. int err;
  230. if (!tzd)
  231. goto stop_channel;
  232. err = thermal_zone_device_disable(tzd);
  233. if (err) {
  234. dev_err(ts->dev, "ch%u: failed to disable zone: %d\n", id, err);
  235. return err;
  236. }
  237. stop_channel:
  238. /* stop channel gracefully */
  239. val = readl_relaxed(tsc->regs + TSENSOR_SENSOR0_CONFIG0);
  240. val |= FIELD_PREP(TSENSOR_SENSOR0_CONFIG0_SENSOR_STOP, 1);
  241. writel_relaxed(val, tsc->regs + TSENSOR_SENSOR0_CONFIG0);
  242. return 0;
  243. }
  244. struct trip_temps {
  245. int hot_trip;
  246. int crit_trip;
  247. };
  248. static int tegra_tsensor_get_trips_cb(struct thermal_trip *trip, void *arg)
  249. {
  250. struct trip_temps *temps = arg;
  251. if (trip->type == THERMAL_TRIP_HOT)
  252. temps->hot_trip = trip->temperature;
  253. else if (trip->type == THERMAL_TRIP_CRITICAL)
  254. temps->crit_trip = trip->temperature;
  255. return 0;
  256. }
  257. static void tegra_tsensor_get_hw_channel_trips(struct thermal_zone_device *tzd,
  258. struct trip_temps *temps)
  259. {
  260. /*
  261. * 90C is the maximal critical temperature of all Tegra30 SoC variants,
  262. * use it for the default trip if unspecified in a device-tree.
  263. */
  264. temps->hot_trip = 85000;
  265. temps->crit_trip = 90000;
  266. thermal_zone_for_each_trip(tzd, tegra_tsensor_get_trips_cb, temps);
  267. /* clamp hardware trips to the calibration limits */
  268. temps->hot_trip = clamp(temps->hot_trip, 25000, 90000);
  269. /*
  270. * Kernel will perform a normal system shut down if it will
  271. * see that critical temperature is breached, hence set the
  272. * hardware limit by 5C higher in order to allow system to
  273. * shut down gracefully before sending signal to the Power
  274. * Management controller.
  275. */
  276. temps->crit_trip = clamp(temps->crit_trip + 5000, 25000, 90000);
  277. }
  278. static int tegra_tsensor_enable_hw_channel(const struct tegra_tsensor *ts,
  279. unsigned int id)
  280. {
  281. const struct tegra_tsensor_channel *tsc = &ts->ch[id];
  282. struct thermal_zone_device *tzd = tsc->tzd;
  283. struct trip_temps temps = { 0 };
  284. int err;
  285. u32 val;
  286. if (!tzd) {
  287. val = readl_relaxed(tsc->regs + TSENSOR_SENSOR0_CONFIG0);
  288. val &= ~TSENSOR_SENSOR0_CONFIG0_SENSOR_STOP;
  289. writel_relaxed(val, tsc->regs + TSENSOR_SENSOR0_CONFIG0);
  290. return 0;
  291. }
  292. tegra_tsensor_get_hw_channel_trips(tzd, &temps);
  293. dev_info_once(ts->dev, "ch%u: PMC emergency shutdown trip set to %dC\n",
  294. id, DIV_ROUND_CLOSEST(temps.crit_trip, 1000));
  295. temps.hot_trip = tegra_tsensor_temp_to_counter(ts, temps.hot_trip);
  296. temps.crit_trip = tegra_tsensor_temp_to_counter(ts, temps.crit_trip);
  297. /* program LEVEL2 counter threshold */
  298. val = readl_relaxed(tsc->regs + TSENSOR_SENSOR0_CONFIG1);
  299. val &= ~TSENSOR_SENSOR0_CONFIG1_TH2;
  300. val |= FIELD_PREP(TSENSOR_SENSOR0_CONFIG1_TH2, temps.hot_trip);
  301. writel_relaxed(val, tsc->regs + TSENSOR_SENSOR0_CONFIG1);
  302. /* program LEVEL3 counter threshold */
  303. val = readl_relaxed(tsc->regs + TSENSOR_SENSOR0_CONFIG2);
  304. val &= ~TSENSOR_SENSOR0_CONFIG2_TH3;
  305. val |= FIELD_PREP(TSENSOR_SENSOR0_CONFIG2_TH3, temps.crit_trip);
  306. writel_relaxed(val, tsc->regs + TSENSOR_SENSOR0_CONFIG2);
  307. /*
  308. * Enable sensor, emergency shutdown, interrupts for level 1/2/3
  309. * breaches and counter overflow condition.
  310. *
  311. * Disable DIV2 throttle for now since we need to figure out how
  312. * to integrate it properly with the thermal framework.
  313. *
  314. * Thermal levels supported by hardware:
  315. *
  316. * Level 0 = cold
  317. * Level 1 = passive cooling (cpufreq DVFS)
  318. * Level 2 = passive cooling assisted by hardware (DIV2)
  319. * Level 3 = emergency shutdown assisted by hardware (PMC)
  320. */
  321. val = readl_relaxed(tsc->regs + TSENSOR_SENSOR0_CONFIG0);
  322. val &= ~TSENSOR_SENSOR0_CONFIG0_SENSOR_STOP;
  323. val |= FIELD_PREP(TSENSOR_SENSOR0_CONFIG0_DVFS_EN, 1);
  324. val |= FIELD_PREP(TSENSOR_SENSOR0_CONFIG0_HW_FREQ_DIV_EN, 0);
  325. val |= FIELD_PREP(TSENSOR_SENSOR0_CONFIG0_THERMAL_RST_EN, 1);
  326. val |= FIELD_PREP(TSENSOR_SENSOR0_CONFIG0_INTR_OVERFLOW_EN, 1);
  327. val |= FIELD_PREP(TSENSOR_SENSOR0_CONFIG0_INTR_HW_FREQ_DIV_EN, 1);
  328. val |= FIELD_PREP(TSENSOR_SENSOR0_CONFIG0_INTR_THERMAL_RST_EN, 1);
  329. writel_relaxed(val, tsc->regs + TSENSOR_SENSOR0_CONFIG0);
  330. err = thermal_zone_device_enable(tzd);
  331. if (err) {
  332. dev_err(ts->dev, "ch%u: failed to enable zone: %d\n", id, err);
  333. return err;
  334. }
  335. return 0;
  336. }
  337. static bool tegra_tsensor_fuse_read_spare(unsigned int spare)
  338. {
  339. u32 val = 0;
  340. tegra_fuse_readl(TEGRA30_FUSE_SPARE_BIT + spare * 4, &val);
  341. return !!val;
  342. }
  343. static int tegra_tsensor_nvmem_setup(struct tegra_tsensor *ts)
  344. {
  345. u32 i, ate_ver = 0, cal = 0, t1_25C = 0, t2_90C = 0;
  346. int err, c1_25C, c2_90C;
  347. err = tegra_fuse_readl(TEGRA30_FUSE_TEST_PROG_VER, &ate_ver);
  348. if (err) {
  349. dev_err_probe(ts->dev, err, "failed to get ATE version\n");
  350. return err;
  351. }
  352. if (ate_ver < 8) {
  353. dev_info(ts->dev, "unsupported ATE version: %u\n", ate_ver);
  354. return -ENODEV;
  355. }
  356. /*
  357. * We have two TSENSOR channels in a two different spots on SoC.
  358. * Second channel provides more accurate data on older SoC versions,
  359. * use it as a primary channel.
  360. */
  361. if (ate_ver <= 21) {
  362. dev_info_once(ts->dev,
  363. "older ATE version detected, channels remapped\n");
  364. ts->swap_channels = true;
  365. }
  366. err = tegra_fuse_readl(TEGRA30_FUSE_TSENSOR_CALIB, &cal);
  367. if (err) {
  368. dev_err(ts->dev, "failed to get calibration data: %d\n", err);
  369. return err;
  370. }
  371. /* get calibrated counter values for 25C/90C thresholds */
  372. c1_25C = FIELD_GET(TEGRA30_FUSE_TSENSOR_CALIB_LOW, cal);
  373. c2_90C = FIELD_GET(TEGRA30_FUSE_TSENSOR_CALIB_HIGH, cal);
  374. /* and calibrated temperatures corresponding to the counter values */
  375. for (i = 0; i < 7; i++) {
  376. t1_25C |= tegra_tsensor_fuse_read_spare(14 + i) << i;
  377. t1_25C |= tegra_tsensor_fuse_read_spare(21 + i) << i;
  378. t2_90C |= tegra_tsensor_fuse_read_spare(0 + i) << i;
  379. t2_90C |= tegra_tsensor_fuse_read_spare(7 + i) << i;
  380. }
  381. if (c2_90C - c1_25C <= t2_90C - t1_25C) {
  382. dev_err(ts->dev, "invalid calibration data: %d %d %u %u\n",
  383. c2_90C, c1_25C, t2_90C, t1_25C);
  384. return -EINVAL;
  385. }
  386. /* all calibration coefficients are premultiplied by 1000000 */
  387. ts->calib.a = DIV_ROUND_CLOSEST((t2_90C - t1_25C) * 1000000,
  388. (c2_90C - c1_25C));
  389. ts->calib.b = t1_25C * 1000000 - ts->calib.a * c1_25C;
  390. if (tegra_sku_info.revision == TEGRA_REVISION_A01) {
  391. ts->calib.m = -2775;
  392. ts->calib.n = 1338811;
  393. ts->calib.p = -7300000;
  394. } else {
  395. ts->calib.m = -3512;
  396. ts->calib.n = 1528943;
  397. ts->calib.p = -11100000;
  398. }
  399. /* except the coefficient of a reduced quadratic equation */
  400. ts->calib.r = DIV_ROUND_CLOSEST(ts->calib.n, ts->calib.m * 2);
  401. dev_info_once(ts->dev,
  402. "calibration: %d %d %u %u ATE ver: %u SoC rev: %u\n",
  403. c2_90C, c1_25C, t2_90C, t1_25C, ate_ver,
  404. tegra_sku_info.revision);
  405. return 0;
  406. }
  407. static int tegra_tsensor_register_channel(struct tegra_tsensor *ts,
  408. unsigned int id)
  409. {
  410. struct tegra_tsensor_channel *tsc = &ts->ch[id];
  411. unsigned int hw_id = ts->swap_channels ? !id : id;
  412. tsc->ts = ts;
  413. tsc->id = id;
  414. tsc->regs = ts->regs + 0x40 * (hw_id + 1);
  415. tsc->tzd = devm_thermal_of_zone_register(ts->dev, id, tsc, &ops);
  416. if (IS_ERR(tsc->tzd)) {
  417. if (PTR_ERR(tsc->tzd) != -ENODEV)
  418. return dev_err_probe(ts->dev, PTR_ERR(tsc->tzd),
  419. "failed to register thermal zone\n");
  420. /*
  421. * It's okay if sensor isn't assigned to any thermal zone
  422. * in a device-tree.
  423. */
  424. tsc->tzd = NULL;
  425. return 0;
  426. }
  427. devm_thermal_add_hwmon_sysfs(ts->dev, tsc->tzd);
  428. return 0;
  429. }
  430. static int tegra_tsensor_probe(struct platform_device *pdev)
  431. {
  432. struct tegra_tsensor *ts;
  433. unsigned int i;
  434. int err, irq;
  435. ts = devm_kzalloc(&pdev->dev, sizeof(*ts), GFP_KERNEL);
  436. if (!ts)
  437. return -ENOMEM;
  438. irq = platform_get_irq(pdev, 0);
  439. if (irq < 0)
  440. return irq;
  441. ts->dev = &pdev->dev;
  442. platform_set_drvdata(pdev, ts);
  443. ts->regs = devm_platform_ioremap_resource(pdev, 0);
  444. if (IS_ERR(ts->regs))
  445. return PTR_ERR(ts->regs);
  446. ts->clk = devm_clk_get(&pdev->dev, NULL);
  447. if (IS_ERR(ts->clk))
  448. return dev_err_probe(&pdev->dev, PTR_ERR(ts->clk),
  449. "failed to get clock\n");
  450. ts->rst = devm_reset_control_get_exclusive(&pdev->dev, NULL);
  451. if (IS_ERR(ts->rst))
  452. return dev_err_probe(&pdev->dev, PTR_ERR(ts->rst),
  453. "failed to get reset control\n");
  454. err = tegra_tsensor_nvmem_setup(ts);
  455. if (err)
  456. return err;
  457. err = tegra_tsensor_hw_enable(ts);
  458. if (err)
  459. return err;
  460. err = devm_add_action_or_reset(&pdev->dev,
  461. devm_tegra_tsensor_hw_disable,
  462. ts);
  463. if (err)
  464. return err;
  465. for (i = 0; i < ARRAY_SIZE(ts->ch); i++) {
  466. err = tegra_tsensor_register_channel(ts, i);
  467. if (err)
  468. return err;
  469. }
  470. /*
  471. * Enable the channels before setting the interrupt so
  472. * set_trips() can not be called while we are setting up the
  473. * register TSENSOR_SENSOR0_CONFIG1. With this we close a
  474. * potential race window where we are setting up the TH2 and
  475. * the temperature hits TH1 resulting to an update of the
  476. * TSENSOR_SENSOR0_CONFIG1 register in the ISR.
  477. */
  478. for (i = 0; i < ARRAY_SIZE(ts->ch); i++) {
  479. err = tegra_tsensor_enable_hw_channel(ts, i);
  480. if (err)
  481. return err;
  482. }
  483. err = devm_request_threaded_irq(&pdev->dev, irq, NULL,
  484. tegra_tsensor_isr, IRQF_ONESHOT,
  485. "tegra_tsensor", ts);
  486. if (err)
  487. return dev_err_probe(&pdev->dev, err,
  488. "failed to request interrupt\n");
  489. return 0;
  490. }
  491. static int __maybe_unused tegra_tsensor_suspend(struct device *dev)
  492. {
  493. struct tegra_tsensor *ts = dev_get_drvdata(dev);
  494. unsigned int i;
  495. int err;
  496. for (i = 0; i < ARRAY_SIZE(ts->ch); i++) {
  497. err = tegra_tsensor_disable_hw_channel(ts, i);
  498. if (err)
  499. goto enable_channel;
  500. }
  501. err = tegra_tsensor_hw_disable(ts);
  502. if (err)
  503. goto enable_channel;
  504. return 0;
  505. enable_channel:
  506. while (i--)
  507. tegra_tsensor_enable_hw_channel(ts, i);
  508. return err;
  509. }
  510. static int __maybe_unused tegra_tsensor_resume(struct device *dev)
  511. {
  512. struct tegra_tsensor *ts = dev_get_drvdata(dev);
  513. unsigned int i;
  514. int err;
  515. err = tegra_tsensor_hw_enable(ts);
  516. if (err)
  517. return err;
  518. for (i = 0; i < ARRAY_SIZE(ts->ch); i++) {
  519. err = tegra_tsensor_enable_hw_channel(ts, i);
  520. if (err)
  521. return err;
  522. }
  523. return 0;
  524. }
  525. static const struct dev_pm_ops tegra_tsensor_pm_ops = {
  526. SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(tegra_tsensor_suspend,
  527. tegra_tsensor_resume)
  528. };
  529. static const struct of_device_id tegra_tsensor_of_match[] = {
  530. { .compatible = "nvidia,tegra30-tsensor", },
  531. {},
  532. };
  533. MODULE_DEVICE_TABLE(of, tegra_tsensor_of_match);
  534. static struct platform_driver tegra_tsensor_driver = {
  535. .probe = tegra_tsensor_probe,
  536. .driver = {
  537. .name = "tegra30-tsensor",
  538. .of_match_table = tegra_tsensor_of_match,
  539. .pm = &tegra_tsensor_pm_ops,
  540. },
  541. };
  542. module_platform_driver(tegra_tsensor_driver);
  543. MODULE_DESCRIPTION("NVIDIA Tegra30 Thermal Sensor driver");
  544. MODULE_AUTHOR("Dmitry Osipenko <digetx@gmail.com>");
  545. MODULE_LICENSE("GPL");