bt1-pvt.c 31 KB

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