tegra30-tsensor.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  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 <[email protected]>
  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_device.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_core.h"
  29. #include "../thermal_hwmon.h"
  30. #define TSENSOR_SENSOR0_CONFIG0 0x0
  31. #define TSENSOR_SENSOR0_CONFIG0_SENSOR_STOP BIT(0)
  32. #define TSENSOR_SENSOR0_CONFIG0_HW_FREQ_DIV_EN BIT(1)
  33. #define TSENSOR_SENSOR0_CONFIG0_THERMAL_RST_EN BIT(2)
  34. #define TSENSOR_SENSOR0_CONFIG0_DVFS_EN BIT(3)
  35. #define TSENSOR_SENSOR0_CONFIG0_INTR_OVERFLOW_EN BIT(4)
  36. #define TSENSOR_SENSOR0_CONFIG0_INTR_HW_FREQ_DIV_EN BIT(5)
  37. #define TSENSOR_SENSOR0_CONFIG0_INTR_THERMAL_RST_EN BIT(6)
  38. #define TSENSOR_SENSOR0_CONFIG0_M GENMASK(23, 8)
  39. #define TSENSOR_SENSOR0_CONFIG0_N GENMASK(31, 24)
  40. #define TSENSOR_SENSOR0_CONFIG1 0x8
  41. #define TSENSOR_SENSOR0_CONFIG1_TH1 GENMASK(15, 0)
  42. #define TSENSOR_SENSOR0_CONFIG1_TH2 GENMASK(31, 16)
  43. #define TSENSOR_SENSOR0_CONFIG2 0xc
  44. #define TSENSOR_SENSOR0_CONFIG2_TH3 GENMASK(15, 0)
  45. #define TSENSOR_SENSOR0_STATUS0 0x18
  46. #define TSENSOR_SENSOR0_STATUS0_STATE GENMASK(2, 0)
  47. #define TSENSOR_SENSOR0_STATUS0_INTR BIT(8)
  48. #define TSENSOR_SENSOR0_STATUS0_CURRENT_VALID BIT(9)
  49. #define TSENSOR_SENSOR0_TS_STATUS1 0x1c
  50. #define TSENSOR_SENSOR0_TS_STATUS1_CURRENT_COUNT GENMASK(31, 16)
  51. #define TEGRA30_FUSE_TEST_PROG_VER 0x28
  52. #define TEGRA30_FUSE_TSENSOR_CALIB 0x98
  53. #define TEGRA30_FUSE_TSENSOR_CALIB_LOW GENMASK(15, 0)
  54. #define TEGRA30_FUSE_TSENSOR_CALIB_HIGH GENMASK(31, 16)
  55. #define TEGRA30_FUSE_SPARE_BIT 0x144
  56. struct tegra_tsensor;
  57. struct tegra_tsensor_calibration_data {
  58. int a, b, m, n, p, r;
  59. };
  60. struct tegra_tsensor_channel {
  61. void __iomem *regs;
  62. unsigned int id;
  63. struct tegra_tsensor *ts;
  64. struct thermal_zone_device *tzd;
  65. };
  66. struct tegra_tsensor {
  67. void __iomem *regs;
  68. bool swap_channels;
  69. struct clk *clk;
  70. struct device *dev;
  71. struct reset_control *rst;
  72. struct tegra_tsensor_channel ch[2];
  73. struct tegra_tsensor_calibration_data calib;
  74. };
  75. static int tegra_tsensor_hw_enable(const struct tegra_tsensor *ts)
  76. {
  77. u32 val;
  78. int err;
  79. err = reset_control_assert(ts->rst);
  80. if (err) {
  81. dev_err(ts->dev, "failed to assert hardware reset: %d\n", err);
  82. return err;
  83. }
  84. err = clk_prepare_enable(ts->clk);
  85. if (err) {
  86. dev_err(ts->dev, "failed to enable clock: %d\n", err);
  87. return err;
  88. }
  89. fsleep(1000);
  90. err = reset_control_deassert(ts->rst);
  91. if (err) {
  92. dev_err(ts->dev, "failed to deassert hardware reset: %d\n", err);
  93. goto disable_clk;
  94. }
  95. /*
  96. * Sensors are enabled after reset by default, but not gauging
  97. * until clock counter is programmed.
  98. *
  99. * M: number of reference clock pulses after which every
  100. * temperature / voltage measurement is made
  101. *
  102. * N: number of reference clock counts for which the counter runs
  103. */
  104. val = FIELD_PREP(TSENSOR_SENSOR0_CONFIG0_M, 12500);
  105. val |= FIELD_PREP(TSENSOR_SENSOR0_CONFIG0_N, 255);
  106. /* apply the same configuration to both channels */
  107. writel_relaxed(val, ts->regs + 0x40 + TSENSOR_SENSOR0_CONFIG0);
  108. writel_relaxed(val, ts->regs + 0x80 + TSENSOR_SENSOR0_CONFIG0);
  109. return 0;
  110. disable_clk:
  111. clk_disable_unprepare(ts->clk);
  112. return err;
  113. }
  114. static int tegra_tsensor_hw_disable(const struct tegra_tsensor *ts)
  115. {
  116. int err;
  117. err = reset_control_assert(ts->rst);
  118. if (err) {
  119. dev_err(ts->dev, "failed to assert hardware reset: %d\n", err);
  120. return err;
  121. }
  122. clk_disable_unprepare(ts->clk);
  123. return 0;
  124. }
  125. static void devm_tegra_tsensor_hw_disable(void *data)
  126. {
  127. const struct tegra_tsensor *ts = data;
  128. tegra_tsensor_hw_disable(ts);
  129. }
  130. static int tegra_tsensor_get_temp(struct thermal_zone_device *tz, int *temp)
  131. {
  132. const struct tegra_tsensor_channel *tsc = tz->devdata;
  133. const struct tegra_tsensor *ts = tsc->ts;
  134. int err, c1, c2, c3, c4, counter;
  135. u32 val;
  136. /*
  137. * Counter will be invalid if hardware is misprogrammed or not enough
  138. * time passed since the time when sensor was enabled.
  139. */
  140. err = readl_relaxed_poll_timeout(tsc->regs + TSENSOR_SENSOR0_STATUS0, val,
  141. val & TSENSOR_SENSOR0_STATUS0_CURRENT_VALID,
  142. 21 * USEC_PER_MSEC,
  143. 21 * USEC_PER_MSEC * 50);
  144. if (err) {
  145. dev_err_once(ts->dev, "ch%u: counter invalid\n", tsc->id);
  146. return err;
  147. }
  148. val = readl_relaxed(tsc->regs + TSENSOR_SENSOR0_TS_STATUS1);
  149. counter = FIELD_GET(TSENSOR_SENSOR0_TS_STATUS1_CURRENT_COUNT, val);
  150. /*
  151. * This shouldn't happen with a valid counter status, nevertheless
  152. * lets verify the value since it's in a separate (from status)
  153. * register.
  154. */
  155. if (counter == 0xffff) {
  156. dev_err_once(ts->dev, "ch%u: counter overflow\n", tsc->id);
  157. return -EINVAL;
  158. }
  159. /*
  160. * temperature = a * counter + b
  161. * temperature = m * (temperature ^ 2) + n * temperature + p
  162. */
  163. c1 = DIV_ROUND_CLOSEST(ts->calib.a * counter + ts->calib.b, 1000000);
  164. c1 = c1 ?: 1;
  165. c2 = DIV_ROUND_CLOSEST(ts->calib.p, c1);
  166. c3 = c1 * ts->calib.m;
  167. c4 = ts->calib.n;
  168. *temp = DIV_ROUND_CLOSEST(c1 * (c2 + c3 + c4), 1000);
  169. return 0;
  170. }
  171. static int tegra_tsensor_temp_to_counter(const struct tegra_tsensor *ts, int temp)
  172. {
  173. int c1, c2;
  174. c1 = DIV_ROUND_CLOSEST(ts->calib.p - temp * 1000, ts->calib.m);
  175. c2 = -ts->calib.r - int_sqrt(ts->calib.r * ts->calib.r - c1);
  176. return DIV_ROUND_CLOSEST(c2 * 1000000 - ts->calib.b, ts->calib.a);
  177. }
  178. static int tegra_tsensor_set_trips(struct thermal_zone_device *tz, int low, int high)
  179. {
  180. const struct tegra_tsensor_channel *tsc = tz->devdata;
  181. const struct tegra_tsensor *ts = tsc->ts;
  182. u32 val;
  183. /*
  184. * TSENSOR doesn't trigger interrupt on the "low" temperature breach,
  185. * hence bail out if high temperature is unspecified.
  186. */
  187. if (high == INT_MAX)
  188. return 0;
  189. val = readl_relaxed(tsc->regs + TSENSOR_SENSOR0_CONFIG1);
  190. val &= ~TSENSOR_SENSOR0_CONFIG1_TH1;
  191. high = tegra_tsensor_temp_to_counter(ts, high);
  192. val |= FIELD_PREP(TSENSOR_SENSOR0_CONFIG1_TH1, high);
  193. writel_relaxed(val, tsc->regs + TSENSOR_SENSOR0_CONFIG1);
  194. return 0;
  195. }
  196. static const struct thermal_zone_device_ops ops = {
  197. .get_temp = tegra_tsensor_get_temp,
  198. .set_trips = tegra_tsensor_set_trips,
  199. };
  200. static bool
  201. tegra_tsensor_handle_channel_interrupt(const struct tegra_tsensor *ts,
  202. unsigned int id)
  203. {
  204. const struct tegra_tsensor_channel *tsc = &ts->ch[id];
  205. u32 val;
  206. val = readl_relaxed(tsc->regs + TSENSOR_SENSOR0_STATUS0);
  207. writel_relaxed(val, tsc->regs + TSENSOR_SENSOR0_STATUS0);
  208. if (FIELD_GET(TSENSOR_SENSOR0_STATUS0_STATE, val) == 5)
  209. dev_err_ratelimited(ts->dev, "ch%u: counter overflowed\n", id);
  210. if (!FIELD_GET(TSENSOR_SENSOR0_STATUS0_INTR, val))
  211. return false;
  212. thermal_zone_device_update(tsc->tzd, THERMAL_EVENT_UNSPECIFIED);
  213. return true;
  214. }
  215. static irqreturn_t tegra_tsensor_isr(int irq, void *data)
  216. {
  217. const struct tegra_tsensor *ts = data;
  218. bool handled = false;
  219. unsigned int i;
  220. for (i = 0; i < ARRAY_SIZE(ts->ch); i++)
  221. handled |= tegra_tsensor_handle_channel_interrupt(ts, i);
  222. return handled ? IRQ_HANDLED : IRQ_NONE;
  223. }
  224. static int tegra_tsensor_disable_hw_channel(const struct tegra_tsensor *ts,
  225. unsigned int id)
  226. {
  227. const struct tegra_tsensor_channel *tsc = &ts->ch[id];
  228. struct thermal_zone_device *tzd = tsc->tzd;
  229. u32 val;
  230. int err;
  231. if (!tzd)
  232. goto stop_channel;
  233. err = thermal_zone_device_disable(tzd);
  234. if (err) {
  235. dev_err(ts->dev, "ch%u: failed to disable zone: %d\n", id, err);
  236. return err;
  237. }
  238. stop_channel:
  239. /* stop channel gracefully */
  240. val = readl_relaxed(tsc->regs + TSENSOR_SENSOR0_CONFIG0);
  241. val |= FIELD_PREP(TSENSOR_SENSOR0_CONFIG0_SENSOR_STOP, 1);
  242. writel_relaxed(val, tsc->regs + TSENSOR_SENSOR0_CONFIG0);
  243. return 0;
  244. }
  245. static void tegra_tsensor_get_hw_channel_trips(struct thermal_zone_device *tzd,
  246. int *hot_trip, int *crit_trip)
  247. {
  248. unsigned int i;
  249. /*
  250. * 90C is the maximal critical temperature of all Tegra30 SoC variants,
  251. * use it for the default trip if unspecified in a device-tree.
  252. */
  253. *hot_trip = 85000;
  254. *crit_trip = 90000;
  255. for (i = 0; i < tzd->num_trips; i++) {
  256. enum thermal_trip_type type;
  257. int trip_temp;
  258. tzd->ops->get_trip_temp(tzd, i, &trip_temp);
  259. tzd->ops->get_trip_type(tzd, i, &type);
  260. if (type == THERMAL_TRIP_HOT)
  261. *hot_trip = trip_temp;
  262. if (type == THERMAL_TRIP_CRITICAL)
  263. *crit_trip = trip_temp;
  264. }
  265. /* clamp hardware trips to the calibration limits */
  266. *hot_trip = clamp(*hot_trip, 25000, 90000);
  267. /*
  268. * Kernel will perform a normal system shut down if it will
  269. * see that critical temperature is breached, hence set the
  270. * hardware limit by 5C higher in order to allow system to
  271. * shut down gracefully before sending signal to the Power
  272. * Management controller.
  273. */
  274. *crit_trip = clamp(*crit_trip + 5000, 25000, 90000);
  275. }
  276. static int tegra_tsensor_enable_hw_channel(const struct tegra_tsensor *ts,
  277. unsigned int id)
  278. {
  279. const struct tegra_tsensor_channel *tsc = &ts->ch[id];
  280. struct thermal_zone_device *tzd = tsc->tzd;
  281. int err, hot_trip = 0, crit_trip = 0;
  282. u32 val;
  283. if (!tzd) {
  284. val = readl_relaxed(tsc->regs + TSENSOR_SENSOR0_CONFIG0);
  285. val &= ~TSENSOR_SENSOR0_CONFIG0_SENSOR_STOP;
  286. writel_relaxed(val, tsc->regs + TSENSOR_SENSOR0_CONFIG0);
  287. return 0;
  288. }
  289. tegra_tsensor_get_hw_channel_trips(tzd, &hot_trip, &crit_trip);
  290. /* prevent potential racing with tegra_tsensor_set_trips() */
  291. mutex_lock(&tzd->lock);
  292. dev_info_once(ts->dev, "ch%u: PMC emergency shutdown trip set to %dC\n",
  293. id, DIV_ROUND_CLOSEST(crit_trip, 1000));
  294. hot_trip = tegra_tsensor_temp_to_counter(ts, hot_trip);
  295. crit_trip = tegra_tsensor_temp_to_counter(ts, crit_trip);
  296. /* program LEVEL2 counter threshold */
  297. val = readl_relaxed(tsc->regs + TSENSOR_SENSOR0_CONFIG1);
  298. val &= ~TSENSOR_SENSOR0_CONFIG1_TH2;
  299. val |= FIELD_PREP(TSENSOR_SENSOR0_CONFIG1_TH2, hot_trip);
  300. writel_relaxed(val, tsc->regs + TSENSOR_SENSOR0_CONFIG1);
  301. /* program LEVEL3 counter threshold */
  302. val = readl_relaxed(tsc->regs + TSENSOR_SENSOR0_CONFIG2);
  303. val &= ~TSENSOR_SENSOR0_CONFIG2_TH3;
  304. val |= FIELD_PREP(TSENSOR_SENSOR0_CONFIG2_TH3, crit_trip);
  305. writel_relaxed(val, tsc->regs + TSENSOR_SENSOR0_CONFIG2);
  306. /*
  307. * Enable sensor, emergency shutdown, interrupts for level 1/2/3
  308. * breaches and counter overflow condition.
  309. *
  310. * Disable DIV2 throttle for now since we need to figure out how
  311. * to integrate it properly with the thermal framework.
  312. *
  313. * Thermal levels supported by hardware:
  314. *
  315. * Level 0 = cold
  316. * Level 1 = passive cooling (cpufreq DVFS)
  317. * Level 2 = passive cooling assisted by hardware (DIV2)
  318. * Level 3 = emergency shutdown assisted by hardware (PMC)
  319. */
  320. val = readl_relaxed(tsc->regs + TSENSOR_SENSOR0_CONFIG0);
  321. val &= ~TSENSOR_SENSOR0_CONFIG0_SENSOR_STOP;
  322. val |= FIELD_PREP(TSENSOR_SENSOR0_CONFIG0_DVFS_EN, 1);
  323. val |= FIELD_PREP(TSENSOR_SENSOR0_CONFIG0_HW_FREQ_DIV_EN, 0);
  324. val |= FIELD_PREP(TSENSOR_SENSOR0_CONFIG0_THERMAL_RST_EN, 1);
  325. val |= FIELD_PREP(TSENSOR_SENSOR0_CONFIG0_INTR_OVERFLOW_EN, 1);
  326. val |= FIELD_PREP(TSENSOR_SENSOR0_CONFIG0_INTR_HW_FREQ_DIV_EN, 1);
  327. val |= FIELD_PREP(TSENSOR_SENSOR0_CONFIG0_INTR_THERMAL_RST_EN, 1);
  328. writel_relaxed(val, tsc->regs + TSENSOR_SENSOR0_CONFIG0);
  329. mutex_unlock(&tzd->lock);
  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. if (devm_thermal_add_hwmon_sysfs(tsc->tzd))
  428. dev_warn(ts->dev, "failed to add hwmon sysfs attributes\n");
  429. return 0;
  430. }
  431. static int tegra_tsensor_probe(struct platform_device *pdev)
  432. {
  433. struct tegra_tsensor *ts;
  434. unsigned int i;
  435. int err, irq;
  436. ts = devm_kzalloc(&pdev->dev, sizeof(*ts), GFP_KERNEL);
  437. if (!ts)
  438. return -ENOMEM;
  439. irq = platform_get_irq(pdev, 0);
  440. if (irq < 0)
  441. return irq;
  442. ts->dev = &pdev->dev;
  443. platform_set_drvdata(pdev, ts);
  444. ts->regs = devm_platform_ioremap_resource(pdev, 0);
  445. if (IS_ERR(ts->regs))
  446. return PTR_ERR(ts->regs);
  447. ts->clk = devm_clk_get(&pdev->dev, NULL);
  448. if (IS_ERR(ts->clk))
  449. return dev_err_probe(&pdev->dev, PTR_ERR(ts->clk),
  450. "failed to get clock\n");
  451. ts->rst = devm_reset_control_get_exclusive(&pdev->dev, NULL);
  452. if (IS_ERR(ts->rst))
  453. return dev_err_probe(&pdev->dev, PTR_ERR(ts->rst),
  454. "failed to get reset control\n");
  455. err = tegra_tsensor_nvmem_setup(ts);
  456. if (err)
  457. return err;
  458. err = tegra_tsensor_hw_enable(ts);
  459. if (err)
  460. return err;
  461. err = devm_add_action_or_reset(&pdev->dev,
  462. devm_tegra_tsensor_hw_disable,
  463. ts);
  464. if (err)
  465. return err;
  466. for (i = 0; i < ARRAY_SIZE(ts->ch); i++) {
  467. err = tegra_tsensor_register_channel(ts, i);
  468. if (err)
  469. return err;
  470. }
  471. err = devm_request_threaded_irq(&pdev->dev, irq, NULL,
  472. tegra_tsensor_isr, IRQF_ONESHOT,
  473. "tegra_tsensor", ts);
  474. if (err)
  475. return dev_err_probe(&pdev->dev, err,
  476. "failed to request interrupt\n");
  477. for (i = 0; i < ARRAY_SIZE(ts->ch); i++) {
  478. err = tegra_tsensor_enable_hw_channel(ts, i);
  479. if (err)
  480. return err;
  481. }
  482. return 0;
  483. }
  484. static int __maybe_unused tegra_tsensor_suspend(struct device *dev)
  485. {
  486. struct tegra_tsensor *ts = dev_get_drvdata(dev);
  487. unsigned int i;
  488. int err;
  489. for (i = 0; i < ARRAY_SIZE(ts->ch); i++) {
  490. err = tegra_tsensor_disable_hw_channel(ts, i);
  491. if (err)
  492. goto enable_channel;
  493. }
  494. err = tegra_tsensor_hw_disable(ts);
  495. if (err)
  496. goto enable_channel;
  497. return 0;
  498. enable_channel:
  499. while (i--)
  500. tegra_tsensor_enable_hw_channel(ts, i);
  501. return err;
  502. }
  503. static int __maybe_unused tegra_tsensor_resume(struct device *dev)
  504. {
  505. struct tegra_tsensor *ts = dev_get_drvdata(dev);
  506. unsigned int i;
  507. int err;
  508. err = tegra_tsensor_hw_enable(ts);
  509. if (err)
  510. return err;
  511. for (i = 0; i < ARRAY_SIZE(ts->ch); i++) {
  512. err = tegra_tsensor_enable_hw_channel(ts, i);
  513. if (err)
  514. return err;
  515. }
  516. return 0;
  517. }
  518. static const struct dev_pm_ops tegra_tsensor_pm_ops = {
  519. SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(tegra_tsensor_suspend,
  520. tegra_tsensor_resume)
  521. };
  522. static const struct of_device_id tegra_tsensor_of_match[] = {
  523. { .compatible = "nvidia,tegra30-tsensor", },
  524. {},
  525. };
  526. MODULE_DEVICE_TABLE(of, tegra_tsensor_of_match);
  527. static struct platform_driver tegra_tsensor_driver = {
  528. .probe = tegra_tsensor_probe,
  529. .driver = {
  530. .name = "tegra30-tsensor",
  531. .of_match_table = tegra_tsensor_of_match,
  532. .pm = &tegra_tsensor_pm_ops,
  533. },
  534. };
  535. module_platform_driver(tegra_tsensor_driver);
  536. MODULE_DESCRIPTION("NVIDIA Tegra30 Thermal Sensor driver");
  537. MODULE_AUTHOR("Dmitry Osipenko <[email protected]>");
  538. MODULE_LICENSE("GPL");