bt1-pvt.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2020 BAIKAL ELECTRONICS, JSC
  4. *
  5. * Authors:
  6. * Maxim Kaurkin <[email protected]>
  7. * Serge Semin <[email protected]>
  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 *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 *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. struct resource *res;
  760. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  761. if (!res) {
  762. dev_err(pvt->dev, "Couldn't find PVT memresource\n");
  763. return -EINVAL;
  764. }
  765. pvt->regs = devm_ioremap_resource(pvt->dev, res);
  766. if (IS_ERR(pvt->regs))
  767. return PTR_ERR(pvt->regs);
  768. return 0;
  769. }
  770. static void pvt_disable_clks(void *data)
  771. {
  772. struct pvt_hwmon *pvt = data;
  773. clk_bulk_disable_unprepare(PVT_CLOCK_NUM, pvt->clks);
  774. }
  775. static int pvt_request_clks(struct pvt_hwmon *pvt)
  776. {
  777. int ret;
  778. pvt->clks[PVT_CLOCK_APB].id = "pclk";
  779. pvt->clks[PVT_CLOCK_REF].id = "ref";
  780. ret = devm_clk_bulk_get(pvt->dev, PVT_CLOCK_NUM, pvt->clks);
  781. if (ret) {
  782. dev_err(pvt->dev, "Couldn't get PVT clocks descriptors\n");
  783. return ret;
  784. }
  785. ret = clk_bulk_prepare_enable(PVT_CLOCK_NUM, pvt->clks);
  786. if (ret) {
  787. dev_err(pvt->dev, "Couldn't enable the PVT clocks\n");
  788. return ret;
  789. }
  790. ret = devm_add_action_or_reset(pvt->dev, pvt_disable_clks, pvt);
  791. if (ret) {
  792. dev_err(pvt->dev, "Can't add PVT clocks disable action\n");
  793. return ret;
  794. }
  795. return 0;
  796. }
  797. static int pvt_check_pwr(struct pvt_hwmon *pvt)
  798. {
  799. unsigned long tout;
  800. int ret = 0;
  801. u32 data;
  802. /*
  803. * Test out the sensor conversion functionality. If it is not done on
  804. * time then the domain must have been unpowered and we won't be able
  805. * to use the device later in this driver.
  806. * Note If the power source is lost during the normal driver work the
  807. * data read procedure will either return -ETIMEDOUT (for the
  808. * alarm-less driver configuration) or just stop the repeated
  809. * conversion. In the later case alas we won't be able to detect the
  810. * problem.
  811. */
  812. pvt_update(pvt->regs + PVT_INTR_MASK, PVT_INTR_ALL, PVT_INTR_ALL);
  813. pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, PVT_CTRL_EN);
  814. pvt_set_tout(pvt, 0);
  815. readl(pvt->regs + PVT_DATA);
  816. tout = PVT_TOUT_MIN / NSEC_PER_USEC;
  817. usleep_range(tout, 2 * tout);
  818. data = readl(pvt->regs + PVT_DATA);
  819. if (!(data & PVT_DATA_VALID)) {
  820. ret = -ENODEV;
  821. dev_err(pvt->dev, "Sensor is powered down\n");
  822. }
  823. pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, 0);
  824. return ret;
  825. }
  826. static int pvt_init_iface(struct pvt_hwmon *pvt)
  827. {
  828. unsigned long rate;
  829. u32 trim, temp;
  830. rate = clk_get_rate(pvt->clks[PVT_CLOCK_REF].clk);
  831. if (!rate) {
  832. dev_err(pvt->dev, "Invalid reference clock rate\n");
  833. return -ENODEV;
  834. }
  835. /*
  836. * Make sure all interrupts and controller are disabled so not to
  837. * accidentally have ISR executed before the driver data is fully
  838. * initialized. Clear the IRQ status as well.
  839. */
  840. pvt_update(pvt->regs + PVT_INTR_MASK, PVT_INTR_ALL, PVT_INTR_ALL);
  841. pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, 0);
  842. readl(pvt->regs + PVT_CLR_INTR);
  843. readl(pvt->regs + PVT_DATA);
  844. /* Setup default sensor mode, timeout and temperature trim. */
  845. pvt_set_mode(pvt, pvt_info[pvt->sensor].mode);
  846. pvt_set_tout(pvt, PVT_TOUT_DEF);
  847. /*
  848. * Preserve the current ref-clock based delay (Ttotal) between the
  849. * sensors data samples in the driver data so not to recalculate it
  850. * each time on the data requests and timeout reads. It consists of the
  851. * delay introduced by the internal ref-clock timer (N / Fclk) and the
  852. * constant timeout caused by each conversion latency (Tmin):
  853. * Ttotal = N / Fclk + Tmin
  854. * If alarms are enabled the sensors are polled one after another and
  855. * in order to get the next measurement of a particular sensor the
  856. * caller will have to wait for at most until all the others are
  857. * polled. In that case the formulae will look a bit different:
  858. * Ttotal = 5 * (N / Fclk + Tmin)
  859. */
  860. #if defined(CONFIG_SENSORS_BT1_PVT_ALARMS)
  861. pvt->timeout = ktime_set(PVT_SENSORS_NUM * PVT_TOUT_DEF, 0);
  862. pvt->timeout = ktime_divns(pvt->timeout, rate);
  863. pvt->timeout = ktime_add_ns(pvt->timeout, PVT_SENSORS_NUM * PVT_TOUT_MIN);
  864. #else
  865. pvt->timeout = ktime_set(PVT_TOUT_DEF, 0);
  866. pvt->timeout = ktime_divns(pvt->timeout, rate);
  867. pvt->timeout = ktime_add_ns(pvt->timeout, PVT_TOUT_MIN);
  868. #endif
  869. trim = PVT_TRIM_DEF;
  870. if (!of_property_read_u32(pvt->dev->of_node,
  871. "baikal,pvt-temp-offset-millicelsius", &temp))
  872. trim = pvt_calc_trim(temp);
  873. pvt_set_trim(pvt, trim);
  874. return 0;
  875. }
  876. static int pvt_request_irq(struct pvt_hwmon *pvt)
  877. {
  878. struct platform_device *pdev = to_platform_device(pvt->dev);
  879. int ret;
  880. pvt->irq = platform_get_irq(pdev, 0);
  881. if (pvt->irq < 0)
  882. return pvt->irq;
  883. ret = devm_request_threaded_irq(pvt->dev, pvt->irq,
  884. pvt_hard_isr, pvt_soft_isr,
  885. #if defined(CONFIG_SENSORS_BT1_PVT_ALARMS)
  886. IRQF_SHARED | IRQF_TRIGGER_HIGH |
  887. IRQF_ONESHOT,
  888. #else
  889. IRQF_SHARED | IRQF_TRIGGER_HIGH,
  890. #endif
  891. "pvt", pvt);
  892. if (ret) {
  893. dev_err(pvt->dev, "Couldn't request PVT IRQ\n");
  894. return ret;
  895. }
  896. return 0;
  897. }
  898. static int pvt_create_hwmon(struct pvt_hwmon *pvt)
  899. {
  900. pvt->hwmon = devm_hwmon_device_register_with_info(pvt->dev, "pvt", pvt,
  901. &pvt_hwmon_info, NULL);
  902. if (IS_ERR(pvt->hwmon)) {
  903. dev_err(pvt->dev, "Couldn't create hwmon device\n");
  904. return PTR_ERR(pvt->hwmon);
  905. }
  906. return 0;
  907. }
  908. #if defined(CONFIG_SENSORS_BT1_PVT_ALARMS)
  909. static void pvt_disable_iface(void *data)
  910. {
  911. struct pvt_hwmon *pvt = data;
  912. mutex_lock(&pvt->iface_mtx);
  913. pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, 0);
  914. pvt_update(pvt->regs + PVT_INTR_MASK, PVT_INTR_DVALID,
  915. PVT_INTR_DVALID);
  916. mutex_unlock(&pvt->iface_mtx);
  917. }
  918. static int pvt_enable_iface(struct pvt_hwmon *pvt)
  919. {
  920. int ret;
  921. ret = devm_add_action(pvt->dev, pvt_disable_iface, pvt);
  922. if (ret) {
  923. dev_err(pvt->dev, "Can't add PVT disable interface action\n");
  924. return ret;
  925. }
  926. /*
  927. * Enable sensors data conversion and IRQ. We need to lock the
  928. * interface mutex since hwmon has just been created and the
  929. * corresponding sysfs files are accessible from user-space,
  930. * which theoretically may cause races.
  931. */
  932. mutex_lock(&pvt->iface_mtx);
  933. pvt_update(pvt->regs + PVT_INTR_MASK, PVT_INTR_DVALID, 0);
  934. pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, PVT_CTRL_EN);
  935. mutex_unlock(&pvt->iface_mtx);
  936. return 0;
  937. }
  938. #else /* !CONFIG_SENSORS_BT1_PVT_ALARMS */
  939. static int pvt_enable_iface(struct pvt_hwmon *pvt)
  940. {
  941. return 0;
  942. }
  943. #endif /* !CONFIG_SENSORS_BT1_PVT_ALARMS */
  944. static int pvt_probe(struct platform_device *pdev)
  945. {
  946. struct pvt_hwmon *pvt;
  947. int ret;
  948. pvt = pvt_create_data(pdev);
  949. if (IS_ERR(pvt))
  950. return PTR_ERR(pvt);
  951. ret = pvt_request_regs(pvt);
  952. if (ret)
  953. return ret;
  954. ret = pvt_request_clks(pvt);
  955. if (ret)
  956. return ret;
  957. ret = pvt_check_pwr(pvt);
  958. if (ret)
  959. return ret;
  960. ret = pvt_init_iface(pvt);
  961. if (ret)
  962. return ret;
  963. ret = pvt_request_irq(pvt);
  964. if (ret)
  965. return ret;
  966. ret = pvt_create_hwmon(pvt);
  967. if (ret)
  968. return ret;
  969. ret = pvt_enable_iface(pvt);
  970. if (ret)
  971. return ret;
  972. return 0;
  973. }
  974. static const struct of_device_id pvt_of_match[] = {
  975. { .compatible = "baikal,bt1-pvt" },
  976. { }
  977. };
  978. MODULE_DEVICE_TABLE(of, pvt_of_match);
  979. static struct platform_driver pvt_driver = {
  980. .probe = pvt_probe,
  981. .driver = {
  982. .name = "bt1-pvt",
  983. .of_match_table = pvt_of_match
  984. }
  985. };
  986. module_platform_driver(pvt_driver);
  987. MODULE_AUTHOR("Maxim Kaurkin <[email protected]>");
  988. MODULE_DESCRIPTION("Baikal-T1 PVT driver");
  989. MODULE_LICENSE("GPL v2");