pac1921.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * IIO driver for PAC1921 High-Side Power/Current Monitor
  4. *
  5. * Copyright (C) 2024 Matteo Martelli <matteomartelli3@gmail.com>
  6. */
  7. #include <linux/unaligned.h>
  8. #include <linux/bitfield.h>
  9. #include <linux/cleanup.h>
  10. #include <linux/i2c.h>
  11. #include <linux/iio/events.h>
  12. #include <linux/iio/iio.h>
  13. #include <linux/iio/trigger_consumer.h>
  14. #include <linux/iio/triggered_buffer.h>
  15. #include <linux/limits.h>
  16. #include <linux/regmap.h>
  17. #include <linux/units.h>
  18. /* pac1921 registers */
  19. #define PAC1921_REG_GAIN_CFG 0x00
  20. #define PAC1921_REG_INT_CFG 0x01
  21. #define PAC1921_REG_CONTROL 0x02
  22. #define PAC1921_REG_VBUS 0x10
  23. #define PAC1921_REG_VSENSE 0x12
  24. #define PAC1921_REG_OVERFLOW_STS 0x1C
  25. #define PAC1921_REG_VPOWER 0x1D
  26. /* pac1921 gain configuration bits */
  27. #define PAC1921_GAIN_DI_GAIN_MASK GENMASK(5, 3)
  28. #define PAC1921_GAIN_DV_GAIN_MASK GENMASK(2, 0)
  29. /* pac1921 integration configuration bits */
  30. #define PAC1921_INT_CFG_SMPL_MASK GENMASK(7, 4)
  31. #define PAC1921_INT_CFG_VSFEN BIT(3)
  32. #define PAC1921_INT_CFG_VBFEN BIT(2)
  33. #define PAC1921_INT_CFG_RIOV BIT(1)
  34. #define PAC1921_INT_CFG_INTEN BIT(0)
  35. /* pac1921 control bits */
  36. #define PAC1921_CONTROL_MXSL_MASK GENMASK(7, 6)
  37. enum pac1921_mxsl {
  38. PAC1921_MXSL_VPOWER_PIN = 0,
  39. PAC1921_MXSL_VSENSE_FREE_RUN = 1,
  40. PAC1921_MXSL_VBUS_FREE_RUN = 2,
  41. PAC1921_MXSL_VPOWER_FREE_RUN = 3,
  42. };
  43. #define PAC1921_CONTROL_SLEEP BIT(2)
  44. /* pac1921 result registers mask and resolution */
  45. #define PAC1921_RES_MASK GENMASK(15, 6)
  46. #define PAC1921_RES_RESOLUTION 1023
  47. /* pac1921 overflow status bits */
  48. #define PAC1921_OVERFLOW_VSOV BIT(2)
  49. #define PAC1921_OVERFLOW_VBOV BIT(1)
  50. #define PAC1921_OVERFLOW_VPOV BIT(0)
  51. /* pac1921 constants */
  52. #define PAC1921_MAX_VSENSE_MV 100
  53. #define PAC1921_MAX_VBUS_V 32
  54. /* Time to first communication after power up (tINT_T) */
  55. #define PAC1921_POWERUP_TIME_MS 20
  56. /* Time from Sleep State to Start of Integration Period (tSLEEP_TO_INT) */
  57. #define PAC1921_SLEEP_TO_INT_TIME_US 86
  58. /* pac1921 defaults */
  59. #define PAC1921_DEFAULT_DV_GAIN 0 /* 2^(value): 1x gain (HW default) */
  60. #define PAC1921_DEFAULT_DI_GAIN 0 /* 2^(value): 1x gain (HW default) */
  61. #define PAC1921_DEFAULT_NUM_SAMPLES 0 /* 2^(value): 1 sample (HW default) */
  62. #define PAC1921_ACPI_GET_uOHMS_VALS 0
  63. #define PAC1921_ACPI_GET_LABEL 1
  64. /* f7bb9932-86ee-4516-a236-7a7a742e55cb */
  65. static const guid_t pac1921_guid =
  66. GUID_INIT(0xf7bb9932, 0x86ee, 0x4516, 0xa2,
  67. 0x36, 0x7a, 0x7a, 0x74, 0x2e, 0x55, 0xcb);
  68. /*
  69. * Pre-computed scale factors for BUS voltage
  70. * format: IIO_VAL_INT_PLUS_NANO
  71. * unit: mV
  72. *
  73. * Vbus scale (mV) = max_vbus (mV) / dv_gain / resolution
  74. */
  75. static const int pac1921_vbus_scales[][2] = {
  76. { 31, 280547409 }, /* dv_gain x1 */
  77. { 15, 640273704 }, /* dv_gain x2 */
  78. { 7, 820136852 }, /* dv_gain x4 */
  79. { 3, 910068426 }, /* dv_gain x8 */
  80. { 1, 955034213 }, /* dv_gain x16 */
  81. { 0, 977517106 }, /* dv_gain x32 */
  82. };
  83. /*
  84. * Pre-computed scales for SENSE voltage
  85. * format: IIO_VAL_INT_PLUS_NANO
  86. * unit: mV
  87. *
  88. * Vsense scale (mV) = max_vsense (mV) / di_gain / resolution
  89. */
  90. static const int pac1921_vsense_scales[][2] = {
  91. { 0, 97751710 }, /* di_gain x1 */
  92. { 0, 48875855 }, /* di_gain x2 */
  93. { 0, 24437927 }, /* di_gain x4 */
  94. { 0, 12218963 }, /* di_gain x8 */
  95. { 0, 6109481 }, /* di_gain x16 */
  96. { 0, 3054740 }, /* di_gain x32 */
  97. { 0, 1527370 }, /* di_gain x64 */
  98. { 0, 763685 }, /* di_gain x128 */
  99. };
  100. /*
  101. * Numbers of samples used to integrate measurements at the end of an
  102. * integration period.
  103. *
  104. * Changing the number of samples affects the integration period: higher the
  105. * number of samples, longer the integration period.
  106. *
  107. * These correspond to the oversampling ratios available exposed to userspace.
  108. */
  109. static const int pac1921_int_num_samples[] = {
  110. 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048
  111. };
  112. /*
  113. * The integration period depends on the configuration of number of integration
  114. * samples, measurement resolution and post filters. The following array
  115. * contains integration periods, in microsecs unit, based on table 4-5 from
  116. * datasheet considering power integration mode, 14-Bit resolution and post
  117. * filters on. Each index corresponds to a specific number of samples from 1
  118. * to 2048.
  119. */
  120. static const unsigned int pac1921_int_periods_usecs[] = {
  121. 2720, /* 1 sample */
  122. 4050, /* 2 samples */
  123. 6790, /* 4 samples */
  124. 12200, /* 8 samples */
  125. 23000, /* 16 samples */
  126. 46000, /* 32 samples */
  127. 92000, /* 64 samples */
  128. 184000, /* 128 samples */
  129. 368000, /* 256 samples */
  130. 736000, /* 512 samples */
  131. 1471000, /* 1024 samples */
  132. 2941000 /* 2048 samples */
  133. };
  134. /* pac1921 regmap configuration */
  135. static const struct regmap_range pac1921_regmap_wr_ranges[] = {
  136. regmap_reg_range(PAC1921_REG_GAIN_CFG, PAC1921_REG_CONTROL),
  137. };
  138. static const struct regmap_access_table pac1921_regmap_wr_table = {
  139. .yes_ranges = pac1921_regmap_wr_ranges,
  140. .n_yes_ranges = ARRAY_SIZE(pac1921_regmap_wr_ranges),
  141. };
  142. static const struct regmap_range pac1921_regmap_rd_ranges[] = {
  143. regmap_reg_range(PAC1921_REG_GAIN_CFG, PAC1921_REG_CONTROL),
  144. regmap_reg_range(PAC1921_REG_VBUS, PAC1921_REG_VPOWER + 1),
  145. };
  146. static const struct regmap_access_table pac1921_regmap_rd_table = {
  147. .yes_ranges = pac1921_regmap_rd_ranges,
  148. .n_yes_ranges = ARRAY_SIZE(pac1921_regmap_rd_ranges),
  149. };
  150. static const struct regmap_config pac1921_regmap_config = {
  151. .reg_bits = 8,
  152. .val_bits = 8,
  153. .rd_table = &pac1921_regmap_rd_table,
  154. .wr_table = &pac1921_regmap_wr_table,
  155. };
  156. enum pac1921_channels {
  157. PAC1921_CHAN_VBUS = 0,
  158. PAC1921_CHAN_VSENSE = 1,
  159. PAC1921_CHAN_CURRENT = 2,
  160. PAC1921_CHAN_POWER = 3,
  161. };
  162. #define PAC1921_NUM_MEAS_CHANS 4
  163. struct pac1921_priv {
  164. struct i2c_client *client;
  165. struct regmap *regmap;
  166. struct regulator *vdd;
  167. struct iio_info iio_info;
  168. /*
  169. * Synchronize access to private members, and ensure atomicity of
  170. * consecutive regmap operations.
  171. */
  172. struct mutex lock;
  173. u32 rshunt_uohm; /* uOhm */
  174. u8 dv_gain;
  175. u8 di_gain;
  176. u8 n_samples;
  177. u8 prev_ovf_flags;
  178. u8 ovf_enabled_events;
  179. bool first_integr_started;
  180. bool first_integr_done;
  181. unsigned long integr_started_time_jiffies;
  182. unsigned int integr_period_usecs;
  183. int current_scales[ARRAY_SIZE(pac1921_vsense_scales)][2];
  184. struct {
  185. u16 chan[PAC1921_NUM_MEAS_CHANS];
  186. aligned_s64 timestamp;
  187. } scan;
  188. };
  189. /*
  190. * Check if first integration after configuration update has completed.
  191. *
  192. * Must be called with lock held.
  193. */
  194. static bool pac1921_data_ready(struct pac1921_priv *priv)
  195. {
  196. if (!priv->first_integr_started)
  197. return false;
  198. if (!priv->first_integr_done) {
  199. unsigned long t_ready;
  200. /*
  201. * Data valid after the device entered into integration state,
  202. * considering worst case where the device was in sleep state,
  203. * and completed the first integration period.
  204. */
  205. t_ready = priv->integr_started_time_jiffies +
  206. usecs_to_jiffies(PAC1921_SLEEP_TO_INT_TIME_US) +
  207. usecs_to_jiffies(priv->integr_period_usecs);
  208. if (time_before(jiffies, t_ready))
  209. return false;
  210. priv->first_integr_done = true;
  211. }
  212. return true;
  213. }
  214. static inline void pac1921_calc_scale(int dividend, int divisor, int *val,
  215. int *val2)
  216. {
  217. s64 tmp;
  218. tmp = div_s64(dividend * (s64)NANO, divisor);
  219. *val = div_s64_rem(tmp, NANO, val2);
  220. }
  221. /*
  222. * Fill the table of scale factors for current
  223. * format: IIO_VAL_INT_PLUS_NANO
  224. * unit: mA
  225. *
  226. * Vsense LSB (nV) = max_vsense (nV) * di_gain / resolution
  227. * Current scale (mA) = Vsense LSB (nV) / shunt (uOhm)
  228. *
  229. * Must be called with held lock when updating after first initialization.
  230. */
  231. static void pac1921_calc_current_scales(struct pac1921_priv *priv)
  232. {
  233. for (unsigned int i = 0; i < ARRAY_SIZE(priv->current_scales); i++) {
  234. int max = (PAC1921_MAX_VSENSE_MV * MICRO) >> i;
  235. int vsense_lsb = DIV_ROUND_CLOSEST(max, PAC1921_RES_RESOLUTION);
  236. pac1921_calc_scale(vsense_lsb, priv->rshunt_uohm,
  237. &priv->current_scales[i][0],
  238. &priv->current_scales[i][1]);
  239. }
  240. }
  241. /*
  242. * Check if overflow occurred and if so, push the corresponding events.
  243. *
  244. * Must be called with lock held.
  245. */
  246. static int pac1921_check_push_overflow(struct iio_dev *indio_dev, s64 timestamp)
  247. {
  248. struct pac1921_priv *priv = iio_priv(indio_dev);
  249. unsigned int flags;
  250. int ret;
  251. ret = regmap_read(priv->regmap, PAC1921_REG_OVERFLOW_STS, &flags);
  252. if (ret)
  253. return ret;
  254. if (flags & PAC1921_OVERFLOW_VBOV &&
  255. !(priv->prev_ovf_flags & PAC1921_OVERFLOW_VBOV) &&
  256. priv->ovf_enabled_events & PAC1921_OVERFLOW_VBOV) {
  257. iio_push_event(indio_dev,
  258. IIO_UNMOD_EVENT_CODE(
  259. IIO_VOLTAGE, PAC1921_CHAN_VBUS,
  260. IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING),
  261. timestamp);
  262. }
  263. if (flags & PAC1921_OVERFLOW_VSOV &&
  264. !(priv->prev_ovf_flags & PAC1921_OVERFLOW_VSOV) &&
  265. priv->ovf_enabled_events & PAC1921_OVERFLOW_VSOV) {
  266. iio_push_event(indio_dev,
  267. IIO_UNMOD_EVENT_CODE(
  268. IIO_VOLTAGE, PAC1921_CHAN_VSENSE,
  269. IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING),
  270. timestamp);
  271. iio_push_event(indio_dev,
  272. IIO_UNMOD_EVENT_CODE(
  273. IIO_CURRENT, PAC1921_CHAN_CURRENT,
  274. IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING),
  275. timestamp);
  276. }
  277. if (flags & PAC1921_OVERFLOW_VPOV &&
  278. !(priv->prev_ovf_flags & PAC1921_OVERFLOW_VPOV) &&
  279. priv->ovf_enabled_events & PAC1921_OVERFLOW_VPOV) {
  280. iio_push_event(indio_dev,
  281. IIO_UNMOD_EVENT_CODE(
  282. IIO_POWER, PAC1921_CHAN_POWER,
  283. IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING),
  284. timestamp);
  285. }
  286. priv->prev_ovf_flags = flags;
  287. return 0;
  288. }
  289. /*
  290. * Read the value from a result register
  291. *
  292. * Result registers contain the most recent averaged values of Vbus, Vsense and
  293. * Vpower. Each value is 10 bits wide and spread across two consecutive 8 bit
  294. * registers, with 6 bit LSB zero padding.
  295. */
  296. static int pac1921_read_res(struct pac1921_priv *priv, unsigned long reg,
  297. u16 *val)
  298. {
  299. int ret = regmap_bulk_read(priv->regmap, reg, val, sizeof(*val));
  300. if (ret)
  301. return ret;
  302. *val = FIELD_GET(PAC1921_RES_MASK, get_unaligned_be16(val));
  303. return 0;
  304. }
  305. static int pac1921_read_raw(struct iio_dev *indio_dev,
  306. struct iio_chan_spec const *chan, int *val,
  307. int *val2, long mask)
  308. {
  309. struct pac1921_priv *priv = iio_priv(indio_dev);
  310. guard(mutex)(&priv->lock);
  311. switch (mask) {
  312. case IIO_CHAN_INFO_RAW: {
  313. s64 ts;
  314. u16 res_val;
  315. int ret;
  316. if (!pac1921_data_ready(priv))
  317. return -EBUSY;
  318. ts = iio_get_time_ns(indio_dev);
  319. ret = pac1921_check_push_overflow(indio_dev, ts);
  320. if (ret)
  321. return ret;
  322. ret = pac1921_read_res(priv, chan->address, &res_val);
  323. if (ret)
  324. return ret;
  325. *val = res_val;
  326. return IIO_VAL_INT;
  327. }
  328. case IIO_CHAN_INFO_SCALE:
  329. switch (chan->channel) {
  330. case PAC1921_CHAN_VBUS:
  331. *val = pac1921_vbus_scales[priv->dv_gain][0];
  332. *val2 = pac1921_vbus_scales[priv->dv_gain][1];
  333. return IIO_VAL_INT_PLUS_NANO;
  334. case PAC1921_CHAN_VSENSE:
  335. *val = pac1921_vsense_scales[priv->di_gain][0];
  336. *val2 = pac1921_vsense_scales[priv->di_gain][1];
  337. return IIO_VAL_INT_PLUS_NANO;
  338. case PAC1921_CHAN_CURRENT:
  339. *val = priv->current_scales[priv->di_gain][0];
  340. *val2 = priv->current_scales[priv->di_gain][1];
  341. return IIO_VAL_INT_PLUS_NANO;
  342. case PAC1921_CHAN_POWER: {
  343. /*
  344. * Power scale factor in mW:
  345. * Current scale (mA) * max_vbus (V) / dv_gain
  346. */
  347. /* Get current scale based on di_gain */
  348. int *curr_scale = priv->current_scales[priv->di_gain];
  349. /* Convert current_scale from INT_PLUS_NANO to INT */
  350. s64 tmp = curr_scale[0] * (s64)NANO + curr_scale[1];
  351. /* Multiply by max_vbus (V) / dv_gain */
  352. tmp *= PAC1921_MAX_VBUS_V >> priv->dv_gain;
  353. /* Convert back to INT_PLUS_NANO */
  354. *val = div_s64_rem(tmp, NANO, val2);
  355. return IIO_VAL_INT_PLUS_NANO;
  356. }
  357. default:
  358. return -EINVAL;
  359. }
  360. case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
  361. *val = pac1921_int_num_samples[priv->n_samples];
  362. return IIO_VAL_INT;
  363. case IIO_CHAN_INFO_SAMP_FREQ:
  364. /*
  365. * The sampling frequency (Hz) is read-only and corresponds to
  366. * how often the device provides integrated measurements into
  367. * the result registers, thus it's 1/integration_period.
  368. * The integration period depends on the number of integration
  369. * samples, measurement resolution and post filters.
  370. *
  371. * 1/(integr_period_usecs/MICRO) = MICRO/integr_period_usecs
  372. */
  373. *val = MICRO;
  374. *val2 = priv->integr_period_usecs;
  375. return IIO_VAL_FRACTIONAL;
  376. default:
  377. return -EINVAL;
  378. }
  379. }
  380. static int pac1921_read_avail(struct iio_dev *indio_dev,
  381. struct iio_chan_spec const *chan,
  382. const int **vals, int *type, int *length,
  383. long mask)
  384. {
  385. switch (mask) {
  386. case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
  387. *type = IIO_VAL_INT;
  388. *vals = pac1921_int_num_samples;
  389. *length = ARRAY_SIZE(pac1921_int_num_samples);
  390. return IIO_AVAIL_LIST;
  391. default:
  392. return -EINVAL;
  393. }
  394. }
  395. /*
  396. * Perform configuration update sequence: set the device into read state, then
  397. * write the config register and set the device back into integration state.
  398. * Also reset integration start time and mark first integration to be yet
  399. * completed.
  400. *
  401. * Must be called with lock held.
  402. */
  403. static int pac1921_update_cfg_reg(struct pac1921_priv *priv, unsigned int reg,
  404. unsigned int mask, unsigned int val)
  405. {
  406. /* Enter READ state before configuration */
  407. int ret = regmap_update_bits(priv->regmap, PAC1921_REG_INT_CFG,
  408. PAC1921_INT_CFG_INTEN, 0);
  409. if (ret)
  410. return ret;
  411. /* Update configuration value */
  412. ret = regmap_update_bits(priv->regmap, reg, mask, val);
  413. if (ret)
  414. return ret;
  415. /* Re-enable integration */
  416. ret = regmap_update_bits(priv->regmap, PAC1921_REG_INT_CFG,
  417. PAC1921_INT_CFG_INTEN, PAC1921_INT_CFG_INTEN);
  418. if (ret)
  419. return ret;
  420. /*
  421. * Reset integration started time and mark this integration period as
  422. * the first one so that new measurements will be considered as valid
  423. * only at the end of this integration period.
  424. */
  425. priv->integr_started_time_jiffies = jiffies;
  426. priv->first_integr_done = false;
  427. return 0;
  428. }
  429. /*
  430. * Retrieve the index of the given scale (represented by scale_val and
  431. * scale_val2) from scales_tbl. The returned index (if found) is the log2 of
  432. * the gain corresponding to the given scale.
  433. *
  434. * Must be called with lock held if the scales_tbl can change runtime (e.g. for
  435. * the current scales table)
  436. */
  437. static int pac1921_lookup_scale(const int (*const scales_tbl)[2], size_t size,
  438. int scale_val, int scale_val2)
  439. {
  440. for (unsigned int i = 0; i < size; i++)
  441. if (scales_tbl[i][0] == scale_val &&
  442. scales_tbl[i][1] == scale_val2)
  443. return i;
  444. return -EINVAL;
  445. }
  446. /*
  447. * Configure device with the given gain (only if changed)
  448. *
  449. * Must be called with lock held.
  450. */
  451. static int pac1921_update_gain(struct pac1921_priv *priv, u8 *priv_val, u8 gain,
  452. unsigned int mask)
  453. {
  454. unsigned int reg_val;
  455. int ret;
  456. if (*priv_val == gain)
  457. return 0;
  458. reg_val = (gain << __ffs(mask)) & mask;
  459. ret = pac1921_update_cfg_reg(priv, PAC1921_REG_GAIN_CFG, mask, reg_val);
  460. if (ret)
  461. return ret;
  462. *priv_val = gain;
  463. return 0;
  464. }
  465. /*
  466. * Given a scale factor represented by scale_val and scale_val2 with format
  467. * IIO_VAL_INT_PLUS_NANO, find the corresponding gain value and write it to the
  468. * device.
  469. *
  470. * Must be called with lock held.
  471. */
  472. static int pac1921_update_gain_from_scale(struct pac1921_priv *priv,
  473. struct iio_chan_spec const *chan,
  474. int scale_val, int scale_val2)
  475. {
  476. int ret;
  477. switch (chan->channel) {
  478. case PAC1921_CHAN_VBUS:
  479. ret = pac1921_lookup_scale(pac1921_vbus_scales,
  480. ARRAY_SIZE(pac1921_vbus_scales),
  481. scale_val, scale_val2);
  482. if (ret < 0)
  483. return ret;
  484. return pac1921_update_gain(priv, &priv->dv_gain, ret,
  485. PAC1921_GAIN_DV_GAIN_MASK);
  486. case PAC1921_CHAN_VSENSE:
  487. ret = pac1921_lookup_scale(pac1921_vsense_scales,
  488. ARRAY_SIZE(pac1921_vsense_scales),
  489. scale_val, scale_val2);
  490. if (ret < 0)
  491. return ret;
  492. return pac1921_update_gain(priv, &priv->di_gain, ret,
  493. PAC1921_GAIN_DI_GAIN_MASK);
  494. case PAC1921_CHAN_CURRENT:
  495. ret = pac1921_lookup_scale(priv->current_scales,
  496. ARRAY_SIZE(priv->current_scales),
  497. scale_val, scale_val2);
  498. if (ret < 0)
  499. return ret;
  500. return pac1921_update_gain(priv, &priv->di_gain, ret,
  501. PAC1921_GAIN_DI_GAIN_MASK);
  502. default:
  503. return -EINVAL;
  504. }
  505. }
  506. /*
  507. * Retrieve the index of the given number of samples from the constant table.
  508. * The returned index (if found) is the log2 of the given num_samples.
  509. */
  510. static int pac1921_lookup_int_num_samples(int num_samples)
  511. {
  512. for (unsigned int i = 0; i < ARRAY_SIZE(pac1921_int_num_samples); i++)
  513. if (pac1921_int_num_samples[i] == num_samples)
  514. return i;
  515. return -EINVAL;
  516. }
  517. /*
  518. * Update the device with the given number of integration samples.
  519. *
  520. * Must be called with lock held.
  521. */
  522. static int pac1921_update_int_num_samples(struct pac1921_priv *priv,
  523. int num_samples)
  524. {
  525. unsigned int reg_val;
  526. u8 n_samples;
  527. int ret;
  528. ret = pac1921_lookup_int_num_samples(num_samples);
  529. if (ret < 0)
  530. return ret;
  531. n_samples = ret;
  532. if (priv->n_samples == n_samples)
  533. return 0;
  534. reg_val = FIELD_PREP(PAC1921_INT_CFG_SMPL_MASK, n_samples);
  535. ret = pac1921_update_cfg_reg(priv, PAC1921_REG_INT_CFG,
  536. PAC1921_INT_CFG_SMPL_MASK, reg_val);
  537. if (ret)
  538. return ret;
  539. priv->n_samples = n_samples;
  540. priv->integr_period_usecs = pac1921_int_periods_usecs[priv->n_samples];
  541. return 0;
  542. }
  543. static int pac1921_write_raw_get_fmt(struct iio_dev *indio_dev,
  544. struct iio_chan_spec const *chan,
  545. long info)
  546. {
  547. switch (info) {
  548. case IIO_CHAN_INFO_SCALE:
  549. return IIO_VAL_INT_PLUS_NANO;
  550. case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
  551. return IIO_VAL_INT;
  552. default:
  553. return -EINVAL;
  554. }
  555. }
  556. static int pac1921_write_raw(struct iio_dev *indio_dev,
  557. struct iio_chan_spec const *chan, int val,
  558. int val2, long mask)
  559. {
  560. struct pac1921_priv *priv = iio_priv(indio_dev);
  561. guard(mutex)(&priv->lock);
  562. switch (mask) {
  563. case IIO_CHAN_INFO_SCALE:
  564. return pac1921_update_gain_from_scale(priv, chan, val, val2);
  565. case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
  566. return pac1921_update_int_num_samples(priv, val);
  567. default:
  568. return -EINVAL;
  569. }
  570. }
  571. static int pac1921_read_label(struct iio_dev *indio_dev,
  572. struct iio_chan_spec const *chan, char *label)
  573. {
  574. switch (chan->channel) {
  575. case PAC1921_CHAN_VBUS:
  576. return sysfs_emit(label, "vbus\n");
  577. case PAC1921_CHAN_VSENSE:
  578. return sysfs_emit(label, "vsense\n");
  579. case PAC1921_CHAN_CURRENT:
  580. return sysfs_emit(label, "current\n");
  581. case PAC1921_CHAN_POWER:
  582. return sysfs_emit(label, "power\n");
  583. default:
  584. return -EINVAL;
  585. }
  586. }
  587. static int pac1921_read_event_config(struct iio_dev *indio_dev,
  588. const struct iio_chan_spec *chan,
  589. enum iio_event_type type,
  590. enum iio_event_direction dir)
  591. {
  592. struct pac1921_priv *priv = iio_priv(indio_dev);
  593. guard(mutex)(&priv->lock);
  594. switch (chan->channel) {
  595. case PAC1921_CHAN_VBUS:
  596. return !!(priv->ovf_enabled_events & PAC1921_OVERFLOW_VBOV);
  597. case PAC1921_CHAN_VSENSE:
  598. case PAC1921_CHAN_CURRENT:
  599. return !!(priv->ovf_enabled_events & PAC1921_OVERFLOW_VSOV);
  600. case PAC1921_CHAN_POWER:
  601. return !!(priv->ovf_enabled_events & PAC1921_OVERFLOW_VPOV);
  602. default:
  603. return -EINVAL;
  604. }
  605. }
  606. static int pac1921_write_event_config(struct iio_dev *indio_dev,
  607. const struct iio_chan_spec *chan,
  608. enum iio_event_type type,
  609. enum iio_event_direction dir,
  610. bool state)
  611. {
  612. struct pac1921_priv *priv = iio_priv(indio_dev);
  613. u8 ovf_bit;
  614. guard(mutex)(&priv->lock);
  615. switch (chan->channel) {
  616. case PAC1921_CHAN_VBUS:
  617. ovf_bit = PAC1921_OVERFLOW_VBOV;
  618. break;
  619. case PAC1921_CHAN_VSENSE:
  620. case PAC1921_CHAN_CURRENT:
  621. ovf_bit = PAC1921_OVERFLOW_VSOV;
  622. break;
  623. case PAC1921_CHAN_POWER:
  624. ovf_bit = PAC1921_OVERFLOW_VPOV;
  625. break;
  626. default:
  627. return -EINVAL;
  628. }
  629. if (state)
  630. priv->ovf_enabled_events |= ovf_bit;
  631. else
  632. priv->ovf_enabled_events &= ~ovf_bit;
  633. return 0;
  634. }
  635. static int pac1921_read_event_value(struct iio_dev *indio_dev,
  636. const struct iio_chan_spec *chan,
  637. enum iio_event_type type,
  638. enum iio_event_direction dir,
  639. enum iio_event_info info, int *val,
  640. int *val2)
  641. {
  642. switch (info) {
  643. case IIO_EV_INFO_VALUE:
  644. *val = PAC1921_RES_RESOLUTION;
  645. return IIO_VAL_INT;
  646. default:
  647. return -EINVAL;
  648. }
  649. }
  650. static const struct iio_info pac1921_iio = {
  651. .read_raw = pac1921_read_raw,
  652. .read_avail = pac1921_read_avail,
  653. .write_raw = pac1921_write_raw,
  654. .write_raw_get_fmt = pac1921_write_raw_get_fmt,
  655. .read_label = pac1921_read_label,
  656. .read_event_config = pac1921_read_event_config,
  657. .write_event_config = pac1921_write_event_config,
  658. .read_event_value = pac1921_read_event_value,
  659. };
  660. static ssize_t pac1921_read_shunt_resistor(struct iio_dev *indio_dev,
  661. uintptr_t private,
  662. const struct iio_chan_spec *chan,
  663. char *buf)
  664. {
  665. struct pac1921_priv *priv = iio_priv(indio_dev);
  666. int vals[2];
  667. if (chan->channel != PAC1921_CHAN_CURRENT)
  668. return -EINVAL;
  669. guard(mutex)(&priv->lock);
  670. vals[0] = priv->rshunt_uohm;
  671. vals[1] = MICRO;
  672. return iio_format_value(buf, IIO_VAL_FRACTIONAL, 1, vals);
  673. }
  674. static ssize_t pac1921_write_shunt_resistor(struct iio_dev *indio_dev,
  675. uintptr_t private,
  676. const struct iio_chan_spec *chan,
  677. const char *buf, size_t len)
  678. {
  679. struct pac1921_priv *priv = iio_priv(indio_dev);
  680. u32 rshunt_uohm;
  681. int val, val_fract;
  682. int ret;
  683. if (chan->channel != PAC1921_CHAN_CURRENT)
  684. return -EINVAL;
  685. ret = iio_str_to_fixpoint(buf, 100000, &val, &val_fract);
  686. if (ret)
  687. return ret;
  688. /*
  689. * This check validates the shunt is not zero and does not surpass
  690. * INT_MAX. The check is done before calculating in order to avoid
  691. * val * MICRO overflowing.
  692. */
  693. if ((!val && !val_fract) || val > INT_MAX / MICRO ||
  694. (val == INT_MAX / MICRO && val_fract > INT_MAX % MICRO))
  695. return -EINVAL;
  696. rshunt_uohm = val * MICRO + val_fract;
  697. guard(mutex)(&priv->lock);
  698. priv->rshunt_uohm = rshunt_uohm;
  699. pac1921_calc_current_scales(priv);
  700. return len;
  701. }
  702. /*
  703. * Emit on sysfs the list of available scales contained in scales_tbl
  704. *
  705. * TODO:: this function can be replaced with iio_format_avail_list() if the
  706. * latter will ever be exported.
  707. *
  708. * Must be called with lock held if the scales_tbl can change runtime (e.g. for
  709. * the current scales table)
  710. */
  711. static ssize_t pac1921_format_scale_avail(const int (*const scales_tbl)[2],
  712. size_t size, char *buf)
  713. {
  714. ssize_t len = 0;
  715. for (unsigned int i = 0; i < size; i++) {
  716. if (i != 0) {
  717. len += sysfs_emit_at(buf, len, " ");
  718. if (len >= PAGE_SIZE)
  719. return -EFBIG;
  720. }
  721. len += sysfs_emit_at(buf, len, "%d.%09d", scales_tbl[i][0],
  722. scales_tbl[i][1]);
  723. if (len >= PAGE_SIZE)
  724. return -EFBIG;
  725. }
  726. len += sysfs_emit_at(buf, len, "\n");
  727. return len;
  728. }
  729. /*
  730. * Read available scales for a specific channel
  731. *
  732. * NOTE: using extended info insted of iio.read_avail() because access to
  733. * current scales must be locked as they depend on shunt resistor which may
  734. * change runtime. Caller of iio.read_avail() would access the table unlocked
  735. * instead.
  736. */
  737. static ssize_t pac1921_read_scale_avail(struct iio_dev *indio_dev,
  738. uintptr_t private,
  739. const struct iio_chan_spec *chan,
  740. char *buf)
  741. {
  742. struct pac1921_priv *priv = iio_priv(indio_dev);
  743. const int (*scales_tbl)[2];
  744. size_t size;
  745. switch (chan->channel) {
  746. case PAC1921_CHAN_VBUS:
  747. scales_tbl = pac1921_vbus_scales;
  748. size = ARRAY_SIZE(pac1921_vbus_scales);
  749. return pac1921_format_scale_avail(scales_tbl, size, buf);
  750. case PAC1921_CHAN_VSENSE:
  751. scales_tbl = pac1921_vsense_scales;
  752. size = ARRAY_SIZE(pac1921_vsense_scales);
  753. return pac1921_format_scale_avail(scales_tbl, size, buf);
  754. case PAC1921_CHAN_CURRENT: {
  755. guard(mutex)(&priv->lock);
  756. scales_tbl = priv->current_scales;
  757. size = ARRAY_SIZE(priv->current_scales);
  758. return pac1921_format_scale_avail(scales_tbl, size, buf);
  759. }
  760. default:
  761. return -EINVAL;
  762. }
  763. }
  764. #define PAC1921_EXT_INFO_SCALE_AVAIL { \
  765. .name = "scale_available", \
  766. .read = pac1921_read_scale_avail, \
  767. .shared = IIO_SEPARATE, \
  768. }
  769. static const struct iio_chan_spec_ext_info pac1921_ext_info_voltage[] = {
  770. PAC1921_EXT_INFO_SCALE_AVAIL,
  771. { }
  772. };
  773. static const struct iio_chan_spec_ext_info pac1921_ext_info_current[] = {
  774. PAC1921_EXT_INFO_SCALE_AVAIL,
  775. {
  776. .name = "shunt_resistor",
  777. .read = pac1921_read_shunt_resistor,
  778. .write = pac1921_write_shunt_resistor,
  779. .shared = IIO_SEPARATE,
  780. },
  781. { }
  782. };
  783. static const struct iio_event_spec pac1921_overflow_event[] = {
  784. {
  785. .type = IIO_EV_TYPE_THRESH,
  786. .dir = IIO_EV_DIR_RISING,
  787. .mask_shared_by_all = BIT(IIO_EV_INFO_VALUE),
  788. .mask_separate = BIT(IIO_EV_INFO_ENABLE),
  789. },
  790. };
  791. static const struct iio_chan_spec pac1921_channels[] = {
  792. {
  793. .type = IIO_VOLTAGE,
  794. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  795. BIT(IIO_CHAN_INFO_SCALE),
  796. .info_mask_shared_by_all =
  797. BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO) |
  798. BIT(IIO_CHAN_INFO_SAMP_FREQ),
  799. .info_mask_shared_by_all_available =
  800. BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
  801. .channel = PAC1921_CHAN_VBUS,
  802. .address = PAC1921_REG_VBUS,
  803. .scan_index = PAC1921_CHAN_VBUS,
  804. .scan_type = {
  805. .sign = 'u',
  806. .realbits = 10,
  807. .storagebits = 16,
  808. .endianness = IIO_CPU
  809. },
  810. .indexed = 1,
  811. .event_spec = pac1921_overflow_event,
  812. .num_event_specs = ARRAY_SIZE(pac1921_overflow_event),
  813. .ext_info = pac1921_ext_info_voltage,
  814. },
  815. {
  816. .type = IIO_VOLTAGE,
  817. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  818. BIT(IIO_CHAN_INFO_SCALE),
  819. .info_mask_shared_by_all =
  820. BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO) |
  821. BIT(IIO_CHAN_INFO_SAMP_FREQ),
  822. .info_mask_shared_by_all_available =
  823. BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
  824. .channel = PAC1921_CHAN_VSENSE,
  825. .address = PAC1921_REG_VSENSE,
  826. .scan_index = PAC1921_CHAN_VSENSE,
  827. .scan_type = {
  828. .sign = 'u',
  829. .realbits = 10,
  830. .storagebits = 16,
  831. .endianness = IIO_CPU
  832. },
  833. .indexed = 1,
  834. .event_spec = pac1921_overflow_event,
  835. .num_event_specs = ARRAY_SIZE(pac1921_overflow_event),
  836. .ext_info = pac1921_ext_info_voltage,
  837. },
  838. {
  839. .type = IIO_CURRENT,
  840. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  841. BIT(IIO_CHAN_INFO_SCALE),
  842. .info_mask_shared_by_all =
  843. BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO) |
  844. BIT(IIO_CHAN_INFO_SAMP_FREQ),
  845. .info_mask_shared_by_all_available =
  846. BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
  847. .channel = PAC1921_CHAN_CURRENT,
  848. .address = PAC1921_REG_VSENSE,
  849. .scan_index = PAC1921_CHAN_CURRENT,
  850. .scan_type = {
  851. .sign = 'u',
  852. .realbits = 10,
  853. .storagebits = 16,
  854. .endianness = IIO_CPU
  855. },
  856. .event_spec = pac1921_overflow_event,
  857. .num_event_specs = ARRAY_SIZE(pac1921_overflow_event),
  858. .ext_info = pac1921_ext_info_current,
  859. },
  860. {
  861. .type = IIO_POWER,
  862. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  863. BIT(IIO_CHAN_INFO_SCALE),
  864. .info_mask_shared_by_all =
  865. BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO) |
  866. BIT(IIO_CHAN_INFO_SAMP_FREQ),
  867. .info_mask_shared_by_all_available =
  868. BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
  869. .channel = PAC1921_CHAN_POWER,
  870. .address = PAC1921_REG_VPOWER,
  871. .scan_index = PAC1921_CHAN_POWER,
  872. .scan_type = {
  873. .sign = 'u',
  874. .realbits = 10,
  875. .storagebits = 16,
  876. .endianness = IIO_CPU
  877. },
  878. .event_spec = pac1921_overflow_event,
  879. .num_event_specs = ARRAY_SIZE(pac1921_overflow_event),
  880. },
  881. IIO_CHAN_SOFT_TIMESTAMP(PAC1921_NUM_MEAS_CHANS),
  882. };
  883. static irqreturn_t pac1921_trigger_handler(int irq, void *p)
  884. {
  885. struct iio_poll_func *pf = p;
  886. struct iio_dev *idev = pf->indio_dev;
  887. struct pac1921_priv *priv = iio_priv(idev);
  888. int ret;
  889. int bit;
  890. int ch = 0;
  891. guard(mutex)(&priv->lock);
  892. if (!pac1921_data_ready(priv))
  893. goto done;
  894. ret = pac1921_check_push_overflow(idev, pf->timestamp);
  895. if (ret)
  896. goto done;
  897. iio_for_each_active_channel(idev, bit) {
  898. u16 val;
  899. ret = pac1921_read_res(priv, idev->channels[ch].address, &val);
  900. if (ret)
  901. goto done;
  902. priv->scan.chan[ch++] = val;
  903. }
  904. iio_push_to_buffers_with_ts(idev, &priv->scan, sizeof(priv->scan),
  905. pf->timestamp);
  906. done:
  907. iio_trigger_notify_done(idev->trig);
  908. return IRQ_HANDLED;
  909. }
  910. /*
  911. * Initialize device by writing initial configuration and putting it into
  912. * integration state.
  913. *
  914. * Must be called with lock held when called after first initialization
  915. * (e.g. from pm resume)
  916. */
  917. static int pac1921_init(struct pac1921_priv *priv)
  918. {
  919. unsigned int val;
  920. int ret;
  921. /* Enter READ state before configuration */
  922. ret = regmap_update_bits(priv->regmap, PAC1921_REG_INT_CFG,
  923. PAC1921_INT_CFG_INTEN, 0);
  924. if (ret)
  925. return ret;
  926. /* Configure gains, use 14-bits measurement resolution (HW default) */
  927. val = FIELD_PREP(PAC1921_GAIN_DI_GAIN_MASK, priv->di_gain) |
  928. FIELD_PREP(PAC1921_GAIN_DV_GAIN_MASK, priv->dv_gain);
  929. ret = regmap_write(priv->regmap, PAC1921_REG_GAIN_CFG, val);
  930. if (ret)
  931. return ret;
  932. /*
  933. * Configure integration:
  934. * - num of integration samples
  935. * - filters enabled (HW default)
  936. * - set READ/INT pin override (RIOV) to control operation mode via
  937. * register instead of pin
  938. */
  939. val = FIELD_PREP(PAC1921_INT_CFG_SMPL_MASK, priv->n_samples) |
  940. PAC1921_INT_CFG_VSFEN | PAC1921_INT_CFG_VBFEN |
  941. PAC1921_INT_CFG_RIOV;
  942. ret = regmap_write(priv->regmap, PAC1921_REG_INT_CFG, val);
  943. if (ret)
  944. return ret;
  945. /*
  946. * Init control register:
  947. * - VPower free run integration mode
  948. * - OUT pin full scale range: 3V (HW default)
  949. * - no timeout, no sleep, no sleep override, no recalc (HW defaults)
  950. */
  951. val = FIELD_PREP(PAC1921_CONTROL_MXSL_MASK,
  952. PAC1921_MXSL_VPOWER_FREE_RUN);
  953. ret = regmap_write(priv->regmap, PAC1921_REG_CONTROL, val);
  954. if (ret)
  955. return ret;
  956. /* Enable integration */
  957. ret = regmap_update_bits(priv->regmap, PAC1921_REG_INT_CFG,
  958. PAC1921_INT_CFG_INTEN, PAC1921_INT_CFG_INTEN);
  959. if (ret)
  960. return ret;
  961. priv->first_integr_started = true;
  962. priv->integr_started_time_jiffies = jiffies;
  963. priv->integr_period_usecs = pac1921_int_periods_usecs[priv->n_samples];
  964. return 0;
  965. }
  966. static int pac1921_suspend(struct device *dev)
  967. {
  968. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  969. struct pac1921_priv *priv = iio_priv(indio_dev);
  970. int ret;
  971. guard(mutex)(&priv->lock);
  972. priv->first_integr_started = false;
  973. priv->first_integr_done = false;
  974. ret = regmap_update_bits(priv->regmap, PAC1921_REG_INT_CFG,
  975. PAC1921_INT_CFG_INTEN, 0);
  976. if (ret)
  977. return ret;
  978. ret = regmap_update_bits(priv->regmap, PAC1921_REG_CONTROL,
  979. PAC1921_CONTROL_SLEEP, PAC1921_CONTROL_SLEEP);
  980. if (ret)
  981. return ret;
  982. return regulator_disable(priv->vdd);
  983. }
  984. static int pac1921_resume(struct device *dev)
  985. {
  986. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  987. struct pac1921_priv *priv = iio_priv(indio_dev);
  988. int ret;
  989. guard(mutex)(&priv->lock);
  990. ret = regulator_enable(priv->vdd);
  991. if (ret)
  992. return ret;
  993. msleep(PAC1921_POWERUP_TIME_MS);
  994. return pac1921_init(priv);
  995. }
  996. static DEFINE_SIMPLE_DEV_PM_OPS(pac1921_pm_ops, pac1921_suspend,
  997. pac1921_resume);
  998. static void pac1921_regulator_disable(void *data)
  999. {
  1000. struct regulator *regulator = data;
  1001. regulator_disable(regulator);
  1002. }
  1003. /*
  1004. * Documentation related to the ACPI device definition
  1005. * https://ww1.microchip.com/downloads/aemDocuments/documents/OTH/ApplicationNotes/ApplicationNotes/PAC193X-Integration-Notes-for-Microsoft-Windows-10-and-Windows-11-Driver-Support-DS00002534.pdf
  1006. */
  1007. static int pac1921_match_acpi_device(struct iio_dev *indio_dev)
  1008. {
  1009. acpi_handle handle;
  1010. union acpi_object *status;
  1011. char *label;
  1012. struct pac1921_priv *priv = iio_priv(indio_dev);
  1013. struct device *dev = &priv->client->dev;
  1014. handle = ACPI_HANDLE(dev);
  1015. status = acpi_evaluate_dsm(handle, &pac1921_guid, 1,
  1016. PAC1921_ACPI_GET_uOHMS_VALS, NULL);
  1017. if (!status)
  1018. return dev_err_probe(dev, -EINVAL,
  1019. "Could not read shunt from ACPI table\n");
  1020. priv->rshunt_uohm = status->package.elements[0].integer.value;
  1021. ACPI_FREE(status);
  1022. status = acpi_evaluate_dsm(handle, &pac1921_guid, 1,
  1023. PAC1921_ACPI_GET_LABEL, NULL);
  1024. if (!status)
  1025. return dev_err_probe(dev, -EINVAL,
  1026. "Could not read label from ACPI table\n");
  1027. label = devm_kstrdup(dev, status->package.elements[0].string.pointer,
  1028. GFP_KERNEL);
  1029. ACPI_FREE(status);
  1030. if (!label)
  1031. return -ENOMEM;
  1032. indio_dev->label = label;
  1033. return 0;
  1034. }
  1035. static int pac1921_parse_of_fw(struct iio_dev *indio_dev)
  1036. {
  1037. int ret;
  1038. struct pac1921_priv *priv = iio_priv(indio_dev);
  1039. struct device *dev = &priv->client->dev;
  1040. ret = device_property_read_u32(dev, "shunt-resistor-micro-ohms",
  1041. &priv->rshunt_uohm);
  1042. if (ret)
  1043. return dev_err_probe(dev, ret,
  1044. "Cannot read shunt resistor property\n");
  1045. return 0;
  1046. }
  1047. static int pac1921_probe(struct i2c_client *client)
  1048. {
  1049. struct device *dev = &client->dev;
  1050. struct pac1921_priv *priv;
  1051. struct iio_dev *indio_dev;
  1052. int ret;
  1053. indio_dev = devm_iio_device_alloc(dev, sizeof(*priv));
  1054. if (!indio_dev)
  1055. return -ENOMEM;
  1056. priv = iio_priv(indio_dev);
  1057. priv->client = client;
  1058. i2c_set_clientdata(client, indio_dev);
  1059. priv->regmap = devm_regmap_init_i2c(client, &pac1921_regmap_config);
  1060. if (IS_ERR(priv->regmap))
  1061. return dev_err_probe(dev, PTR_ERR(priv->regmap),
  1062. "Cannot initialize register map\n");
  1063. ret = devm_mutex_init(dev, &priv->lock);
  1064. if (ret)
  1065. return ret;
  1066. priv->dv_gain = PAC1921_DEFAULT_DV_GAIN;
  1067. priv->di_gain = PAC1921_DEFAULT_DI_GAIN;
  1068. priv->n_samples = PAC1921_DEFAULT_NUM_SAMPLES;
  1069. if (is_acpi_device_node(dev->fwnode))
  1070. ret = pac1921_match_acpi_device(indio_dev);
  1071. else
  1072. ret = pac1921_parse_of_fw(indio_dev);
  1073. if (ret)
  1074. return dev_err_probe(dev, ret,
  1075. "Parameter parsing error\n");
  1076. if (priv->rshunt_uohm == 0 || priv->rshunt_uohm > INT_MAX)
  1077. return dev_err_probe(dev, -EINVAL,
  1078. "Invalid shunt resistor: %u\n",
  1079. priv->rshunt_uohm);
  1080. pac1921_calc_current_scales(priv);
  1081. priv->vdd = devm_regulator_get(dev, "vdd");
  1082. if (IS_ERR(priv->vdd))
  1083. return dev_err_probe(dev, PTR_ERR(priv->vdd),
  1084. "Cannot get vdd regulator\n");
  1085. ret = regulator_enable(priv->vdd);
  1086. if (ret)
  1087. return dev_err_probe(dev, ret, "Cannot enable vdd regulator\n");
  1088. ret = devm_add_action_or_reset(dev, pac1921_regulator_disable,
  1089. priv->vdd);
  1090. if (ret)
  1091. return ret;
  1092. msleep(PAC1921_POWERUP_TIME_MS);
  1093. ret = pac1921_init(priv);
  1094. if (ret)
  1095. return dev_err_probe(dev, ret, "Cannot initialize device\n");
  1096. priv->iio_info = pac1921_iio;
  1097. indio_dev->name = "pac1921";
  1098. indio_dev->info = &priv->iio_info;
  1099. indio_dev->modes = INDIO_DIRECT_MODE;
  1100. indio_dev->channels = pac1921_channels;
  1101. indio_dev->num_channels = ARRAY_SIZE(pac1921_channels);
  1102. ret = devm_iio_triggered_buffer_setup(dev, indio_dev,
  1103. &iio_pollfunc_store_time,
  1104. &pac1921_trigger_handler, NULL);
  1105. if (ret)
  1106. return dev_err_probe(dev, ret,
  1107. "Cannot setup IIO triggered buffer\n");
  1108. ret = devm_iio_device_register(dev, indio_dev);
  1109. if (ret)
  1110. return dev_err_probe(dev, ret, "Cannot register IIO device\n");
  1111. return 0;
  1112. }
  1113. static const struct i2c_device_id pac1921_id[] = {
  1114. { .name = "pac1921", 0 },
  1115. { }
  1116. };
  1117. MODULE_DEVICE_TABLE(i2c, pac1921_id);
  1118. static const struct of_device_id pac1921_of_match[] = {
  1119. { .compatible = "microchip,pac1921" },
  1120. { }
  1121. };
  1122. MODULE_DEVICE_TABLE(of, pac1921_of_match);
  1123. static const struct acpi_device_id pac1921_acpi_match[] = {
  1124. { "MCHP1921" },
  1125. { }
  1126. };
  1127. MODULE_DEVICE_TABLE(acpi, pac1921_acpi_match);
  1128. static struct i2c_driver pac1921_driver = {
  1129. .driver = {
  1130. .name = "pac1921",
  1131. .pm = pm_sleep_ptr(&pac1921_pm_ops),
  1132. .of_match_table = pac1921_of_match,
  1133. .acpi_match_table = pac1921_acpi_match,
  1134. },
  1135. .probe = pac1921_probe,
  1136. .id_table = pac1921_id,
  1137. };
  1138. module_i2c_driver(pac1921_driver);
  1139. MODULE_AUTHOR("Matteo Martelli <matteomartelli3@gmail.com>");
  1140. MODULE_DESCRIPTION("IIO driver for PAC1921 High-Side Power/Current Monitor");
  1141. MODULE_LICENSE("GPL");