bt1-pvt.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2020 BAIKAL ELECTRONICS, JSC
  4. *
  5. * Authors:
  6. * Maxim Kaurkin <maxim.kaurkin@baikalelectronics.ru>
  7. * Serge Semin <Sergey.Semin@baikalelectronics.ru>
  8. *
  9. * Baikal-T1 Process, Voltage, Temperature sensor driver
  10. */
  11. #include <linux/bitfield.h>
  12. #include <linux/bitops.h>
  13. #include <linux/clk.h>
  14. #include <linux/completion.h>
  15. #include <linux/delay.h>
  16. #include <linux/device.h>
  17. #include <linux/hwmon-sysfs.h>
  18. #include <linux/hwmon.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/io.h>
  21. #include <linux/kernel.h>
  22. #include <linux/ktime.h>
  23. #include <linux/limits.h>
  24. #include <linux/module.h>
  25. #include <linux/mutex.h>
  26. #include <linux/of.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/polynomial.h>
  29. #include <linux/seqlock.h>
  30. #include <linux/sysfs.h>
  31. #include <linux/types.h>
  32. #include "bt1-pvt.h"
  33. /*
  34. * For the sake of the code simplification we created the sensors info table
  35. * with the sensor names, activation modes, threshold registers base address
  36. * and the thresholds bit fields.
  37. */
  38. static const struct pvt_sensor_info pvt_info[] = {
  39. PVT_SENSOR_INFO(0, "CPU Core Temperature", hwmon_temp, TEMP, TTHRES),
  40. PVT_SENSOR_INFO(0, "CPU Core Voltage", hwmon_in, VOLT, VTHRES),
  41. PVT_SENSOR_INFO(1, "CPU Core Low-Vt", hwmon_in, LVT, LTHRES),
  42. PVT_SENSOR_INFO(2, "CPU Core High-Vt", hwmon_in, HVT, HTHRES),
  43. PVT_SENSOR_INFO(3, "CPU Core Standard-Vt", hwmon_in, SVT, STHRES),
  44. };
  45. /*
  46. * The original translation formulae of the temperature (in degrees of Celsius)
  47. * to PVT data and vice-versa are following:
  48. * N = 1.8322e-8*(T^4) + 2.343e-5*(T^3) + 8.7018e-3*(T^2) + 3.9269*(T^1) +
  49. * 1.7204e2,
  50. * T = -1.6743e-11*(N^4) + 8.1542e-8*(N^3) + -1.8201e-4*(N^2) +
  51. * 3.1020e-1*(N^1) - 4.838e1,
  52. * where T = [-48.380, 147.438]C and N = [0, 1023].
  53. * They must be accordingly altered to be suitable for the integer arithmetics.
  54. * The technique is called 'factor redistribution', which just makes sure the
  55. * multiplications and divisions are made so to have a result of the operations
  56. * within the integer numbers limit. In addition we need to translate the
  57. * formulae to accept millidegrees of Celsius. Here what they look like after
  58. * the alterations:
  59. * N = (18322e-20*(T^4) + 2343e-13*(T^3) + 87018e-9*(T^2) + 39269e-3*T +
  60. * 17204e2) / 1e4,
  61. * T = -16743e-12*(D^4) + 81542e-9*(D^3) - 182010e-6*(D^2) + 310200e-3*D -
  62. * 48380,
  63. * where T = [-48380, 147438] mC and N = [0, 1023].
  64. */
  65. static const struct polynomial __maybe_unused poly_temp_to_N = {
  66. .total_divider = 10000,
  67. .terms = {
  68. {4, 18322, 10000, 10000},
  69. {3, 2343, 10000, 10},
  70. {2, 87018, 10000, 10},
  71. {1, 39269, 1000, 1},
  72. {0, 1720400, 1, 1}
  73. }
  74. };
  75. static const struct polynomial poly_N_to_temp = {
  76. .total_divider = 1,
  77. .terms = {
  78. {4, -16743, 1000, 1},
  79. {3, 81542, 1000, 1},
  80. {2, -182010, 1000, 1},
  81. {1, 310200, 1000, 1},
  82. {0, -48380, 1, 1}
  83. }
  84. };
  85. /*
  86. * Similar alterations are performed for the voltage conversion equations.
  87. * The original formulae are:
  88. * N = 1.8658e3*V - 1.1572e3,
  89. * V = (N + 1.1572e3) / 1.8658e3,
  90. * where V = [0.620, 1.168] V and N = [0, 1023].
  91. * After the optimization they looks as follows:
  92. * N = (18658e-3*V - 11572) / 10,
  93. * V = N * 10^5 / 18658 + 11572 * 10^4 / 18658.
  94. */
  95. static const struct polynomial __maybe_unused poly_volt_to_N = {
  96. .total_divider = 10,
  97. .terms = {
  98. {1, 18658, 1000, 1},
  99. {0, -11572, 1, 1}
  100. }
  101. };
  102. static const struct polynomial poly_N_to_volt = {
  103. .total_divider = 10,
  104. .terms = {
  105. {1, 100000, 18658, 1},
  106. {0, 115720000, 1, 18658}
  107. }
  108. };
  109. static inline u32 pvt_update(void __iomem *reg, u32 mask, u32 data)
  110. {
  111. u32 old;
  112. old = readl_relaxed(reg);
  113. writel((old & ~mask) | (data & mask), reg);
  114. return old & mask;
  115. }
  116. /*
  117. * Baikal-T1 PVT mode can be updated only when the controller is disabled.
  118. * So first we disable it, then set the new mode together with the controller
  119. * getting back enabled. The same concerns the temperature trim and
  120. * measurements timeout. If it is necessary the interface mutex is supposed
  121. * to be locked at the time the operations are performed.
  122. */
  123. static inline void pvt_set_mode(struct pvt_hwmon *pvt, u32 mode)
  124. {
  125. u32 old;
  126. mode = FIELD_PREP(PVT_CTRL_MODE_MASK, mode);
  127. old = pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, 0);
  128. pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_MODE_MASK | PVT_CTRL_EN,
  129. mode | old);
  130. }
  131. static inline u32 pvt_calc_trim(long temp)
  132. {
  133. temp = clamp_val(temp, 0, PVT_TRIM_TEMP);
  134. return DIV_ROUND_UP(temp, PVT_TRIM_STEP);
  135. }
  136. static inline void pvt_set_trim(struct pvt_hwmon *pvt, u32 trim)
  137. {
  138. u32 old;
  139. trim = FIELD_PREP(PVT_CTRL_TRIM_MASK, trim);
  140. old = pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, 0);
  141. pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_TRIM_MASK | PVT_CTRL_EN,
  142. trim | old);
  143. }
  144. static inline void pvt_set_tout(struct pvt_hwmon *pvt, u32 tout)
  145. {
  146. u32 old;
  147. old = pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, 0);
  148. writel(tout, pvt->regs + PVT_TTIMEOUT);
  149. pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, old);
  150. }
  151. /*
  152. * This driver can optionally provide the hwmon alarms for each sensor the PVT
  153. * controller supports. The alarms functionality is made compile-time
  154. * configurable due to the hardware interface implementation peculiarity
  155. * described further in this comment. So in case if alarms are unnecessary in
  156. * your system design it's recommended to have them disabled to prevent the PVT
  157. * IRQs being periodically raised to get the data cache/alarms status up to
  158. * date.
  159. *
  160. * Baikal-T1 PVT embedded controller is based on the Analog Bits PVT sensor,
  161. * but is equipped with a dedicated control wrapper. It exposes the PVT
  162. * sub-block registers space via the APB3 bus. In addition the wrapper provides
  163. * a common interrupt vector of the sensors conversion completion events and
  164. * threshold value alarms. Alas the wrapper interface hasn't been fully thought
  165. * through. There is only one sensor can be activated at a time, for which the
  166. * thresholds comparator is enabled right after the data conversion is
  167. * completed. Due to this if alarms need to be implemented for all available
  168. * sensors we can't just set the thresholds and enable the interrupts. We need
  169. * to enable the sensors one after another and let the controller to detect
  170. * the alarms by itself at each conversion. This also makes pointless to handle
  171. * the alarms interrupts, since in occasion they happen synchronously with
  172. * data conversion completion. The best driver design would be to have the
  173. * completion interrupts enabled only and keep the converted value in the
  174. * driver data cache. This solution is implemented if hwmon alarms are enabled
  175. * in this driver. In case if the alarms are disabled, the conversion is
  176. * performed on demand at the time a sensors input file is read.
  177. */
  178. #if defined(CONFIG_SENSORS_BT1_PVT_ALARMS)
  179. #define pvt_hard_isr NULL
  180. static irqreturn_t pvt_soft_isr(int irq, void *data)
  181. {
  182. const struct pvt_sensor_info *info;
  183. struct pvt_hwmon *pvt = data;
  184. struct pvt_cache *cache;
  185. u32 val, thres_sts, old;
  186. /*
  187. * DVALID bit will be cleared by reading the data. We need to save the
  188. * status before the next conversion happens. Threshold events will be
  189. * handled a bit later.
  190. */
  191. thres_sts = readl(pvt->regs + PVT_RAW_INTR_STAT);
  192. /*
  193. * Then lets recharge the PVT interface with the next sampling mode.
  194. * Lock the interface mutex to serialize trim, timeouts and alarm
  195. * thresholds settings.
  196. */
  197. cache = &pvt->cache[pvt->sensor];
  198. info = &pvt_info[pvt->sensor];
  199. pvt->sensor = (pvt->sensor == PVT_SENSOR_LAST) ?
  200. PVT_SENSOR_FIRST : (pvt->sensor + 1);
  201. /*
  202. * For some reason we have to mask the interrupt before changing the
  203. * mode, otherwise sometimes the temperature mode doesn't get
  204. * activated even though the actual mode in the ctrl register
  205. * corresponds to one. Then we read the data. By doing so we also
  206. * recharge the data conversion. After this the mode corresponding
  207. * to the next sensor in the row is set. Finally we enable the
  208. * interrupts back.
  209. */
  210. mutex_lock(&pvt->iface_mtx);
  211. old = pvt_update(pvt->regs + PVT_INTR_MASK, PVT_INTR_DVALID,
  212. PVT_INTR_DVALID);
  213. val = readl(pvt->regs + PVT_DATA);
  214. pvt_set_mode(pvt, pvt_info[pvt->sensor].mode);
  215. pvt_update(pvt->regs + PVT_INTR_MASK, PVT_INTR_DVALID, old);
  216. mutex_unlock(&pvt->iface_mtx);
  217. /*
  218. * We can now update the data cache with data just retrieved from the
  219. * sensor. Lock write-seqlock to make sure the reader has a coherent
  220. * data.
  221. */
  222. write_seqlock(&cache->data_seqlock);
  223. cache->data = FIELD_GET(PVT_DATA_DATA_MASK, val);
  224. write_sequnlock(&cache->data_seqlock);
  225. /*
  226. * While PVT core is doing the next mode data conversion, we'll check
  227. * whether the alarms were triggered for the current sensor. Note that
  228. * according to the documentation only one threshold IRQ status can be
  229. * set at a time, that's why if-else statement is utilized.
  230. */
  231. if ((thres_sts & info->thres_sts_lo) ^ cache->thres_sts_lo) {
  232. WRITE_ONCE(cache->thres_sts_lo, thres_sts & info->thres_sts_lo);
  233. hwmon_notify_event(pvt->hwmon, info->type, info->attr_min_alarm,
  234. info->channel);
  235. } else if ((thres_sts & info->thres_sts_hi) ^ cache->thres_sts_hi) {
  236. WRITE_ONCE(cache->thres_sts_hi, thres_sts & info->thres_sts_hi);
  237. hwmon_notify_event(pvt->hwmon, info->type, info->attr_max_alarm,
  238. info->channel);
  239. }
  240. return IRQ_HANDLED;
  241. }
  242. static inline umode_t pvt_limit_is_visible(enum pvt_sensor_type type)
  243. {
  244. return 0644;
  245. }
  246. static inline umode_t pvt_alarm_is_visible(enum pvt_sensor_type type)
  247. {
  248. return 0444;
  249. }
  250. static int pvt_read_data(struct pvt_hwmon *pvt, enum pvt_sensor_type type,
  251. long *val)
  252. {
  253. struct pvt_cache *cache = &pvt->cache[type];
  254. unsigned int seq;
  255. u32 data;
  256. do {
  257. seq = read_seqbegin(&cache->data_seqlock);
  258. data = cache->data;
  259. } while (read_seqretry(&cache->data_seqlock, seq));
  260. if (type == PVT_TEMP)
  261. *val = polynomial_calc(&poly_N_to_temp, data);
  262. else
  263. *val = polynomial_calc(&poly_N_to_volt, data);
  264. return 0;
  265. }
  266. static int pvt_read_limit(struct pvt_hwmon *pvt, enum pvt_sensor_type type,
  267. bool is_low, long *val)
  268. {
  269. u32 data;
  270. /* No need in serialization, since it is just read from MMIO. */
  271. data = readl(pvt->regs + pvt_info[type].thres_base);
  272. if (is_low)
  273. data = FIELD_GET(PVT_THRES_LO_MASK, data);
  274. else
  275. data = FIELD_GET(PVT_THRES_HI_MASK, data);
  276. if (type == PVT_TEMP)
  277. *val = polynomial_calc(&poly_N_to_temp, data);
  278. else
  279. *val = polynomial_calc(&poly_N_to_volt, data);
  280. return 0;
  281. }
  282. static int pvt_write_limit(struct pvt_hwmon *pvt, enum pvt_sensor_type type,
  283. bool is_low, long val)
  284. {
  285. u32 data, limit, mask;
  286. int ret;
  287. if (type == PVT_TEMP) {
  288. val = clamp(val, PVT_TEMP_MIN, PVT_TEMP_MAX);
  289. data = polynomial_calc(&poly_temp_to_N, val);
  290. } else {
  291. val = clamp(val, PVT_VOLT_MIN, PVT_VOLT_MAX);
  292. data = polynomial_calc(&poly_volt_to_N, val);
  293. }
  294. /* Serialize limit update, since a part of the register is changed. */
  295. ret = mutex_lock_interruptible(&pvt->iface_mtx);
  296. if (ret)
  297. return ret;
  298. /* Make sure the upper and lower ranges don't intersect. */
  299. limit = readl(pvt->regs + pvt_info[type].thres_base);
  300. if (is_low) {
  301. limit = FIELD_GET(PVT_THRES_HI_MASK, limit);
  302. data = clamp_val(data, PVT_DATA_MIN, limit);
  303. data = FIELD_PREP(PVT_THRES_LO_MASK, data);
  304. mask = PVT_THRES_LO_MASK;
  305. } else {
  306. limit = FIELD_GET(PVT_THRES_LO_MASK, limit);
  307. data = clamp_val(data, limit, PVT_DATA_MAX);
  308. data = FIELD_PREP(PVT_THRES_HI_MASK, data);
  309. mask = PVT_THRES_HI_MASK;
  310. }
  311. pvt_update(pvt->regs + pvt_info[type].thres_base, mask, data);
  312. mutex_unlock(&pvt->iface_mtx);
  313. return 0;
  314. }
  315. static int pvt_read_alarm(struct pvt_hwmon *pvt, enum pvt_sensor_type type,
  316. bool is_low, long *val)
  317. {
  318. if (is_low)
  319. *val = !!READ_ONCE(pvt->cache[type].thres_sts_lo);
  320. else
  321. *val = !!READ_ONCE(pvt->cache[type].thres_sts_hi);
  322. return 0;
  323. }
  324. static const struct hwmon_channel_info * const pvt_channel_info[] = {
  325. HWMON_CHANNEL_INFO(chip,
  326. HWMON_C_REGISTER_TZ | HWMON_C_UPDATE_INTERVAL),
  327. HWMON_CHANNEL_INFO(temp,
  328. HWMON_T_INPUT | HWMON_T_TYPE | HWMON_T_LABEL |
  329. HWMON_T_MIN | HWMON_T_MIN_ALARM |
  330. HWMON_T_MAX | HWMON_T_MAX_ALARM |
  331. HWMON_T_OFFSET),
  332. HWMON_CHANNEL_INFO(in,
  333. HWMON_I_INPUT | HWMON_I_LABEL |
  334. HWMON_I_MIN | HWMON_I_MIN_ALARM |
  335. HWMON_I_MAX | HWMON_I_MAX_ALARM,
  336. HWMON_I_INPUT | HWMON_I_LABEL |
  337. HWMON_I_MIN | HWMON_I_MIN_ALARM |
  338. HWMON_I_MAX | HWMON_I_MAX_ALARM,
  339. HWMON_I_INPUT | HWMON_I_LABEL |
  340. HWMON_I_MIN | HWMON_I_MIN_ALARM |
  341. HWMON_I_MAX | HWMON_I_MAX_ALARM,
  342. HWMON_I_INPUT | HWMON_I_LABEL |
  343. HWMON_I_MIN | HWMON_I_MIN_ALARM |
  344. HWMON_I_MAX | HWMON_I_MAX_ALARM),
  345. NULL
  346. };
  347. #else /* !CONFIG_SENSORS_BT1_PVT_ALARMS */
  348. static irqreturn_t pvt_hard_isr(int irq, void *data)
  349. {
  350. struct pvt_hwmon *pvt = data;
  351. struct pvt_cache *cache;
  352. u32 val;
  353. /*
  354. * Mask the DVALID interrupt so after exiting from the handler a
  355. * repeated conversion wouldn't happen.
  356. */
  357. pvt_update(pvt->regs + PVT_INTR_MASK, PVT_INTR_DVALID,
  358. PVT_INTR_DVALID);
  359. /*
  360. * Nothing special for alarm-less driver. Just read the data, update
  361. * the cache and notify a waiter of this event.
  362. */
  363. val = readl(pvt->regs + PVT_DATA);
  364. if (!(val & PVT_DATA_VALID)) {
  365. dev_err(pvt->dev, "Got IRQ when data isn't valid\n");
  366. return IRQ_HANDLED;
  367. }
  368. cache = &pvt->cache[pvt->sensor];
  369. WRITE_ONCE(cache->data, FIELD_GET(PVT_DATA_DATA_MASK, val));
  370. complete(&cache->conversion);
  371. return IRQ_HANDLED;
  372. }
  373. #define pvt_soft_isr NULL
  374. static inline umode_t pvt_limit_is_visible(enum pvt_sensor_type type)
  375. {
  376. return 0;
  377. }
  378. static inline umode_t pvt_alarm_is_visible(enum pvt_sensor_type type)
  379. {
  380. return 0;
  381. }
  382. static int pvt_read_data(struct pvt_hwmon *pvt, enum pvt_sensor_type type,
  383. long *val)
  384. {
  385. struct pvt_cache *cache = &pvt->cache[type];
  386. unsigned long timeout;
  387. u32 data;
  388. int ret;
  389. /*
  390. * Lock PVT conversion interface until data cache is updated. The
  391. * data read procedure is following: set the requested PVT sensor
  392. * mode, enable IRQ and conversion, wait until conversion is finished,
  393. * then disable conversion and IRQ, and read the cached data.
  394. */
  395. ret = mutex_lock_interruptible(&pvt->iface_mtx);
  396. if (ret)
  397. return ret;
  398. pvt->sensor = type;
  399. pvt_set_mode(pvt, pvt_info[type].mode);
  400. /*
  401. * Unmask the DVALID interrupt and enable the sensors conversions.
  402. * Do the reverse procedure when conversion is done.
  403. */
  404. pvt_update(pvt->regs + PVT_INTR_MASK, PVT_INTR_DVALID, 0);
  405. pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, PVT_CTRL_EN);
  406. /*
  407. * Wait with timeout since in case if the sensor is suddenly powered
  408. * down the request won't be completed and the caller will hang up on
  409. * this procedure until the power is back up again. Multiply the
  410. * timeout by the factor of two to prevent a false timeout.
  411. */
  412. timeout = 2 * usecs_to_jiffies(ktime_to_us(pvt->timeout));
  413. ret = wait_for_completion_timeout(&cache->conversion, timeout);
  414. pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, 0);
  415. pvt_update(pvt->regs + PVT_INTR_MASK, PVT_INTR_DVALID,
  416. PVT_INTR_DVALID);
  417. data = READ_ONCE(cache->data);
  418. mutex_unlock(&pvt->iface_mtx);
  419. if (!ret)
  420. return -ETIMEDOUT;
  421. if (type == PVT_TEMP)
  422. *val = polynomial_calc(&poly_N_to_temp, data);
  423. else
  424. *val = polynomial_calc(&poly_N_to_volt, data);
  425. return 0;
  426. }
  427. static int pvt_read_limit(struct pvt_hwmon *pvt, enum pvt_sensor_type type,
  428. bool is_low, long *val)
  429. {
  430. return -EOPNOTSUPP;
  431. }
  432. static int pvt_write_limit(struct pvt_hwmon *pvt, enum pvt_sensor_type type,
  433. bool is_low, long val)
  434. {
  435. return -EOPNOTSUPP;
  436. }
  437. static int pvt_read_alarm(struct pvt_hwmon *pvt, enum pvt_sensor_type type,
  438. bool is_low, long *val)
  439. {
  440. return -EOPNOTSUPP;
  441. }
  442. static const struct hwmon_channel_info * const pvt_channel_info[] = {
  443. HWMON_CHANNEL_INFO(chip,
  444. HWMON_C_REGISTER_TZ | HWMON_C_UPDATE_INTERVAL),
  445. HWMON_CHANNEL_INFO(temp,
  446. HWMON_T_INPUT | HWMON_T_TYPE | HWMON_T_LABEL |
  447. HWMON_T_OFFSET),
  448. HWMON_CHANNEL_INFO(in,
  449. HWMON_I_INPUT | HWMON_I_LABEL,
  450. HWMON_I_INPUT | HWMON_I_LABEL,
  451. HWMON_I_INPUT | HWMON_I_LABEL,
  452. HWMON_I_INPUT | HWMON_I_LABEL),
  453. NULL
  454. };
  455. #endif /* !CONFIG_SENSORS_BT1_PVT_ALARMS */
  456. static inline bool pvt_hwmon_channel_is_valid(enum hwmon_sensor_types type,
  457. int ch)
  458. {
  459. switch (type) {
  460. case hwmon_temp:
  461. if (ch < 0 || ch >= PVT_TEMP_CHS)
  462. return false;
  463. break;
  464. case hwmon_in:
  465. if (ch < 0 || ch >= PVT_VOLT_CHS)
  466. return false;
  467. break;
  468. default:
  469. break;
  470. }
  471. /* The rest of the types are independent from the channel number. */
  472. return true;
  473. }
  474. static umode_t pvt_hwmon_is_visible(const void *data,
  475. enum hwmon_sensor_types type,
  476. u32 attr, int ch)
  477. {
  478. if (!pvt_hwmon_channel_is_valid(type, ch))
  479. return 0;
  480. switch (type) {
  481. case hwmon_chip:
  482. switch (attr) {
  483. case hwmon_chip_update_interval:
  484. return 0644;
  485. }
  486. break;
  487. case hwmon_temp:
  488. switch (attr) {
  489. case hwmon_temp_input:
  490. case hwmon_temp_type:
  491. case hwmon_temp_label:
  492. return 0444;
  493. case hwmon_temp_min:
  494. case hwmon_temp_max:
  495. return pvt_limit_is_visible(ch);
  496. case hwmon_temp_min_alarm:
  497. case hwmon_temp_max_alarm:
  498. return pvt_alarm_is_visible(ch);
  499. case hwmon_temp_offset:
  500. return 0644;
  501. }
  502. break;
  503. case hwmon_in:
  504. switch (attr) {
  505. case hwmon_in_input:
  506. case hwmon_in_label:
  507. return 0444;
  508. case hwmon_in_min:
  509. case hwmon_in_max:
  510. return pvt_limit_is_visible(PVT_VOLT + ch);
  511. case hwmon_in_min_alarm:
  512. case hwmon_in_max_alarm:
  513. return pvt_alarm_is_visible(PVT_VOLT + ch);
  514. }
  515. break;
  516. default:
  517. break;
  518. }
  519. return 0;
  520. }
  521. static int pvt_read_trim(struct pvt_hwmon *pvt, long *val)
  522. {
  523. u32 data;
  524. data = readl(pvt->regs + PVT_CTRL);
  525. *val = FIELD_GET(PVT_CTRL_TRIM_MASK, data) * PVT_TRIM_STEP;
  526. return 0;
  527. }
  528. static int pvt_write_trim(struct pvt_hwmon *pvt, long val)
  529. {
  530. u32 trim;
  531. int ret;
  532. /*
  533. * Serialize trim update, since a part of the register is changed and
  534. * the controller is supposed to be disabled during this operation.
  535. */
  536. ret = mutex_lock_interruptible(&pvt->iface_mtx);
  537. if (ret)
  538. return ret;
  539. trim = pvt_calc_trim(val);
  540. pvt_set_trim(pvt, trim);
  541. mutex_unlock(&pvt->iface_mtx);
  542. return 0;
  543. }
  544. static int pvt_read_timeout(struct pvt_hwmon *pvt, long *val)
  545. {
  546. int ret;
  547. ret = mutex_lock_interruptible(&pvt->iface_mtx);
  548. if (ret)
  549. return ret;
  550. /* Return the result in msec as hwmon sysfs interface requires. */
  551. *val = ktime_to_ms(pvt->timeout);
  552. mutex_unlock(&pvt->iface_mtx);
  553. return 0;
  554. }
  555. static int pvt_write_timeout(struct pvt_hwmon *pvt, long val)
  556. {
  557. unsigned long rate;
  558. ktime_t kt, cache;
  559. u32 data;
  560. int ret;
  561. rate = clk_get_rate(pvt->clks[PVT_CLOCK_REF].clk);
  562. if (!rate)
  563. return -ENODEV;
  564. /*
  565. * If alarms are enabled, the requested timeout must be divided
  566. * between all available sensors to have the requested delay
  567. * applicable to each individual sensor.
  568. */
  569. cache = kt = ms_to_ktime(val);
  570. #if defined(CONFIG_SENSORS_BT1_PVT_ALARMS)
  571. kt = ktime_divns(kt, PVT_SENSORS_NUM);
  572. #endif
  573. /*
  574. * Subtract a constant lag, which always persists due to the limited
  575. * PVT sampling rate. Make sure the timeout is not negative.
  576. */
  577. kt = ktime_sub_ns(kt, PVT_TOUT_MIN);
  578. if (ktime_to_ns(kt) < 0)
  579. kt = ktime_set(0, 0);
  580. /*
  581. * Finally recalculate the timeout in terms of the reference clock
  582. * period.
  583. */
  584. data = ktime_divns(kt * rate, NSEC_PER_SEC);
  585. /*
  586. * Update the measurements delay, but lock the interface first, since
  587. * we have to disable PVT in order to have the new delay actually
  588. * updated.
  589. */
  590. ret = mutex_lock_interruptible(&pvt->iface_mtx);
  591. if (ret)
  592. return ret;
  593. pvt_set_tout(pvt, data);
  594. pvt->timeout = cache;
  595. mutex_unlock(&pvt->iface_mtx);
  596. return 0;
  597. }
  598. static int pvt_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
  599. u32 attr, int ch, long *val)
  600. {
  601. struct pvt_hwmon *pvt = dev_get_drvdata(dev);
  602. if (!pvt_hwmon_channel_is_valid(type, ch))
  603. return -EINVAL;
  604. switch (type) {
  605. case hwmon_chip:
  606. switch (attr) {
  607. case hwmon_chip_update_interval:
  608. return pvt_read_timeout(pvt, val);
  609. }
  610. break;
  611. case hwmon_temp:
  612. switch (attr) {
  613. case hwmon_temp_input:
  614. return pvt_read_data(pvt, ch, val);
  615. case hwmon_temp_type:
  616. *val = 1;
  617. return 0;
  618. case hwmon_temp_min:
  619. return pvt_read_limit(pvt, ch, true, val);
  620. case hwmon_temp_max:
  621. return pvt_read_limit(pvt, ch, false, val);
  622. case hwmon_temp_min_alarm:
  623. return pvt_read_alarm(pvt, ch, true, val);
  624. case hwmon_temp_max_alarm:
  625. return pvt_read_alarm(pvt, ch, false, val);
  626. case hwmon_temp_offset:
  627. return pvt_read_trim(pvt, val);
  628. }
  629. break;
  630. case hwmon_in:
  631. switch (attr) {
  632. case hwmon_in_input:
  633. return pvt_read_data(pvt, PVT_VOLT + ch, val);
  634. case hwmon_in_min:
  635. return pvt_read_limit(pvt, PVT_VOLT + ch, true, val);
  636. case hwmon_in_max:
  637. return pvt_read_limit(pvt, PVT_VOLT + ch, false, val);
  638. case hwmon_in_min_alarm:
  639. return pvt_read_alarm(pvt, PVT_VOLT + ch, true, val);
  640. case hwmon_in_max_alarm:
  641. return pvt_read_alarm(pvt, PVT_VOLT + ch, false, val);
  642. }
  643. break;
  644. default:
  645. break;
  646. }
  647. return -EOPNOTSUPP;
  648. }
  649. static int pvt_hwmon_read_string(struct device *dev,
  650. enum hwmon_sensor_types type,
  651. u32 attr, int ch, const char **str)
  652. {
  653. if (!pvt_hwmon_channel_is_valid(type, ch))
  654. return -EINVAL;
  655. switch (type) {
  656. case hwmon_temp:
  657. switch (attr) {
  658. case hwmon_temp_label:
  659. *str = pvt_info[ch].label;
  660. return 0;
  661. }
  662. break;
  663. case hwmon_in:
  664. switch (attr) {
  665. case hwmon_in_label:
  666. *str = pvt_info[PVT_VOLT + ch].label;
  667. return 0;
  668. }
  669. break;
  670. default:
  671. break;
  672. }
  673. return -EOPNOTSUPP;
  674. }
  675. static int pvt_hwmon_write(struct device *dev, enum hwmon_sensor_types type,
  676. u32 attr, int ch, long val)
  677. {
  678. struct pvt_hwmon *pvt = dev_get_drvdata(dev);
  679. if (!pvt_hwmon_channel_is_valid(type, ch))
  680. return -EINVAL;
  681. switch (type) {
  682. case hwmon_chip:
  683. switch (attr) {
  684. case hwmon_chip_update_interval:
  685. return pvt_write_timeout(pvt, val);
  686. }
  687. break;
  688. case hwmon_temp:
  689. switch (attr) {
  690. case hwmon_temp_min:
  691. return pvt_write_limit(pvt, ch, true, val);
  692. case hwmon_temp_max:
  693. return pvt_write_limit(pvt, ch, false, val);
  694. case hwmon_temp_offset:
  695. return pvt_write_trim(pvt, val);
  696. }
  697. break;
  698. case hwmon_in:
  699. switch (attr) {
  700. case hwmon_in_min:
  701. return pvt_write_limit(pvt, PVT_VOLT + ch, true, val);
  702. case hwmon_in_max:
  703. return pvt_write_limit(pvt, PVT_VOLT + ch, false, val);
  704. }
  705. break;
  706. default:
  707. break;
  708. }
  709. return -EOPNOTSUPP;
  710. }
  711. static const struct hwmon_ops pvt_hwmon_ops = {
  712. .is_visible = pvt_hwmon_is_visible,
  713. .read = pvt_hwmon_read,
  714. .read_string = pvt_hwmon_read_string,
  715. .write = pvt_hwmon_write
  716. };
  717. static const struct hwmon_chip_info pvt_hwmon_info = {
  718. .ops = &pvt_hwmon_ops,
  719. .info = pvt_channel_info
  720. };
  721. static void pvt_clear_data(void *data)
  722. {
  723. struct pvt_hwmon *pvt = data;
  724. #if !defined(CONFIG_SENSORS_BT1_PVT_ALARMS)
  725. int idx;
  726. for (idx = 0; idx < PVT_SENSORS_NUM; ++idx)
  727. complete_all(&pvt->cache[idx].conversion);
  728. #endif
  729. mutex_destroy(&pvt->iface_mtx);
  730. }
  731. static struct pvt_hwmon *pvt_create_data(struct platform_device *pdev)
  732. {
  733. struct device *dev = &pdev->dev;
  734. struct pvt_hwmon *pvt;
  735. int ret, idx;
  736. pvt = devm_kzalloc(dev, sizeof(*pvt), GFP_KERNEL);
  737. if (!pvt)
  738. return ERR_PTR(-ENOMEM);
  739. ret = devm_add_action(dev, pvt_clear_data, pvt);
  740. if (ret) {
  741. dev_err(dev, "Can't add PVT data clear action\n");
  742. return ERR_PTR(ret);
  743. }
  744. pvt->dev = dev;
  745. pvt->sensor = PVT_SENSOR_FIRST;
  746. mutex_init(&pvt->iface_mtx);
  747. #if defined(CONFIG_SENSORS_BT1_PVT_ALARMS)
  748. for (idx = 0; idx < PVT_SENSORS_NUM; ++idx)
  749. seqlock_init(&pvt->cache[idx].data_seqlock);
  750. #else
  751. for (idx = 0; idx < PVT_SENSORS_NUM; ++idx)
  752. init_completion(&pvt->cache[idx].conversion);
  753. #endif
  754. return pvt;
  755. }
  756. static int pvt_request_regs(struct pvt_hwmon *pvt)
  757. {
  758. struct platform_device *pdev = to_platform_device(pvt->dev);
  759. pvt->regs = devm_platform_ioremap_resource(pdev, 0);
  760. if (IS_ERR(pvt->regs))
  761. return PTR_ERR(pvt->regs);
  762. return 0;
  763. }
  764. static void pvt_disable_clks(void *data)
  765. {
  766. struct pvt_hwmon *pvt = data;
  767. clk_bulk_disable_unprepare(PVT_CLOCK_NUM, pvt->clks);
  768. }
  769. static int pvt_request_clks(struct pvt_hwmon *pvt)
  770. {
  771. int ret;
  772. pvt->clks[PVT_CLOCK_APB].id = "pclk";
  773. pvt->clks[PVT_CLOCK_REF].id = "ref";
  774. ret = devm_clk_bulk_get(pvt->dev, PVT_CLOCK_NUM, pvt->clks);
  775. if (ret) {
  776. dev_err(pvt->dev, "Couldn't get PVT clocks descriptors\n");
  777. return ret;
  778. }
  779. ret = clk_bulk_prepare_enable(PVT_CLOCK_NUM, pvt->clks);
  780. if (ret) {
  781. dev_err(pvt->dev, "Couldn't enable the PVT clocks\n");
  782. return ret;
  783. }
  784. ret = devm_add_action_or_reset(pvt->dev, pvt_disable_clks, pvt);
  785. if (ret) {
  786. dev_err(pvt->dev, "Can't add PVT clocks disable action\n");
  787. return ret;
  788. }
  789. return 0;
  790. }
  791. static int pvt_check_pwr(struct pvt_hwmon *pvt)
  792. {
  793. unsigned long tout;
  794. int ret = 0;
  795. u32 data;
  796. /*
  797. * Test out the sensor conversion functionality. If it is not done on
  798. * time then the domain must have been unpowered and we won't be able
  799. * to use the device later in this driver.
  800. * Note If the power source is lost during the normal driver work the
  801. * data read procedure will either return -ETIMEDOUT (for the
  802. * alarm-less driver configuration) or just stop the repeated
  803. * conversion. In the later case alas we won't be able to detect the
  804. * problem.
  805. */
  806. pvt_update(pvt->regs + PVT_INTR_MASK, PVT_INTR_ALL, PVT_INTR_ALL);
  807. pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, PVT_CTRL_EN);
  808. pvt_set_tout(pvt, 0);
  809. readl(pvt->regs + PVT_DATA);
  810. tout = PVT_TOUT_MIN / NSEC_PER_USEC;
  811. usleep_range(tout, 2 * tout);
  812. data = readl(pvt->regs + PVT_DATA);
  813. if (!(data & PVT_DATA_VALID)) {
  814. ret = -ENODEV;
  815. dev_err(pvt->dev, "Sensor is powered down\n");
  816. }
  817. pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, 0);
  818. return ret;
  819. }
  820. static int pvt_init_iface(struct pvt_hwmon *pvt)
  821. {
  822. unsigned long rate;
  823. u32 trim, temp;
  824. rate = clk_get_rate(pvt->clks[PVT_CLOCK_REF].clk);
  825. if (!rate) {
  826. dev_err(pvt->dev, "Invalid reference clock rate\n");
  827. return -ENODEV;
  828. }
  829. /*
  830. * Make sure all interrupts and controller are disabled so not to
  831. * accidentally have ISR executed before the driver data is fully
  832. * initialized. Clear the IRQ status as well.
  833. */
  834. pvt_update(pvt->regs + PVT_INTR_MASK, PVT_INTR_ALL, PVT_INTR_ALL);
  835. pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, 0);
  836. readl(pvt->regs + PVT_CLR_INTR);
  837. readl(pvt->regs + PVT_DATA);
  838. /* Setup default sensor mode, timeout and temperature trim. */
  839. pvt_set_mode(pvt, pvt_info[pvt->sensor].mode);
  840. pvt_set_tout(pvt, PVT_TOUT_DEF);
  841. /*
  842. * Preserve the current ref-clock based delay (Ttotal) between the
  843. * sensors data samples in the driver data so not to recalculate it
  844. * each time on the data requests and timeout reads. It consists of the
  845. * delay introduced by the internal ref-clock timer (N / Fclk) and the
  846. * constant timeout caused by each conversion latency (Tmin):
  847. * Ttotal = N / Fclk + Tmin
  848. * If alarms are enabled the sensors are polled one after another and
  849. * in order to get the next measurement of a particular sensor the
  850. * caller will have to wait for at most until all the others are
  851. * polled. In that case the formulae will look a bit different:
  852. * Ttotal = 5 * (N / Fclk + Tmin)
  853. */
  854. #if defined(CONFIG_SENSORS_BT1_PVT_ALARMS)
  855. pvt->timeout = ktime_set(PVT_SENSORS_NUM * PVT_TOUT_DEF, 0);
  856. pvt->timeout = ktime_divns(pvt->timeout, rate);
  857. pvt->timeout = ktime_add_ns(pvt->timeout, PVT_SENSORS_NUM * PVT_TOUT_MIN);
  858. #else
  859. pvt->timeout = ktime_set(PVT_TOUT_DEF, 0);
  860. pvt->timeout = ktime_divns(pvt->timeout, rate);
  861. pvt->timeout = ktime_add_ns(pvt->timeout, PVT_TOUT_MIN);
  862. #endif
  863. trim = PVT_TRIM_DEF;
  864. if (!of_property_read_u32(pvt->dev->of_node,
  865. "baikal,pvt-temp-offset-millicelsius", &temp))
  866. trim = pvt_calc_trim(temp);
  867. pvt_set_trim(pvt, trim);
  868. return 0;
  869. }
  870. static int pvt_request_irq(struct pvt_hwmon *pvt)
  871. {
  872. struct platform_device *pdev = to_platform_device(pvt->dev);
  873. int ret;
  874. pvt->irq = platform_get_irq(pdev, 0);
  875. if (pvt->irq < 0)
  876. return pvt->irq;
  877. ret = devm_request_threaded_irq(pvt->dev, pvt->irq,
  878. pvt_hard_isr, pvt_soft_isr,
  879. #if defined(CONFIG_SENSORS_BT1_PVT_ALARMS)
  880. IRQF_SHARED | IRQF_TRIGGER_HIGH |
  881. IRQF_ONESHOT,
  882. #else
  883. IRQF_SHARED | IRQF_TRIGGER_HIGH,
  884. #endif
  885. "pvt", pvt);
  886. if (ret) {
  887. dev_err(pvt->dev, "Couldn't request PVT IRQ\n");
  888. return ret;
  889. }
  890. return 0;
  891. }
  892. static int pvt_create_hwmon(struct pvt_hwmon *pvt)
  893. {
  894. pvt->hwmon = devm_hwmon_device_register_with_info(pvt->dev, "pvt", pvt,
  895. &pvt_hwmon_info, NULL);
  896. if (IS_ERR(pvt->hwmon)) {
  897. dev_err(pvt->dev, "Couldn't create hwmon device\n");
  898. return PTR_ERR(pvt->hwmon);
  899. }
  900. return 0;
  901. }
  902. #if defined(CONFIG_SENSORS_BT1_PVT_ALARMS)
  903. static void pvt_disable_iface(void *data)
  904. {
  905. struct pvt_hwmon *pvt = data;
  906. mutex_lock(&pvt->iface_mtx);
  907. pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, 0);
  908. pvt_update(pvt->regs + PVT_INTR_MASK, PVT_INTR_DVALID,
  909. PVT_INTR_DVALID);
  910. mutex_unlock(&pvt->iface_mtx);
  911. }
  912. static int pvt_enable_iface(struct pvt_hwmon *pvt)
  913. {
  914. int ret;
  915. ret = devm_add_action(pvt->dev, pvt_disable_iface, pvt);
  916. if (ret) {
  917. dev_err(pvt->dev, "Can't add PVT disable interface action\n");
  918. return ret;
  919. }
  920. /*
  921. * Enable sensors data conversion and IRQ. We need to lock the
  922. * interface mutex since hwmon has just been created and the
  923. * corresponding sysfs files are accessible from user-space,
  924. * which theoretically may cause races.
  925. */
  926. mutex_lock(&pvt->iface_mtx);
  927. pvt_update(pvt->regs + PVT_INTR_MASK, PVT_INTR_DVALID, 0);
  928. pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, PVT_CTRL_EN);
  929. mutex_unlock(&pvt->iface_mtx);
  930. return 0;
  931. }
  932. #else /* !CONFIG_SENSORS_BT1_PVT_ALARMS */
  933. static int pvt_enable_iface(struct pvt_hwmon *pvt)
  934. {
  935. return 0;
  936. }
  937. #endif /* !CONFIG_SENSORS_BT1_PVT_ALARMS */
  938. static int pvt_probe(struct platform_device *pdev)
  939. {
  940. struct pvt_hwmon *pvt;
  941. int ret;
  942. pvt = pvt_create_data(pdev);
  943. if (IS_ERR(pvt))
  944. return PTR_ERR(pvt);
  945. ret = pvt_request_regs(pvt);
  946. if (ret)
  947. return ret;
  948. ret = pvt_request_clks(pvt);
  949. if (ret)
  950. return ret;
  951. ret = pvt_check_pwr(pvt);
  952. if (ret)
  953. return ret;
  954. ret = pvt_init_iface(pvt);
  955. if (ret)
  956. return ret;
  957. ret = pvt_request_irq(pvt);
  958. if (ret)
  959. return ret;
  960. ret = pvt_create_hwmon(pvt);
  961. if (ret)
  962. return ret;
  963. ret = pvt_enable_iface(pvt);
  964. if (ret)
  965. return ret;
  966. return 0;
  967. }
  968. static const struct of_device_id pvt_of_match[] = {
  969. { .compatible = "baikal,bt1-pvt" },
  970. { }
  971. };
  972. MODULE_DEVICE_TABLE(of, pvt_of_match);
  973. static struct platform_driver pvt_driver = {
  974. .probe = pvt_probe,
  975. .driver = {
  976. .name = "bt1-pvt",
  977. .of_match_table = pvt_of_match
  978. }
  979. };
  980. module_platform_driver(pvt_driver);
  981. MODULE_AUTHOR("Maxim Kaurkin <maxim.kaurkin@baikalelectronics.ru>");
  982. MODULE_DESCRIPTION("Baikal-T1 PVT driver");
  983. MODULE_LICENSE("GPL v2");