rohm-bd79124.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * ROHM ADC driver for BD79124 ADC/GPO device
  4. * https://fscdn.rohm.com/en/products/databook/datasheet/ic/data_converter/dac/bd79124muf-c-e.pdf
  5. *
  6. * Copyright (c) 2025, ROHM Semiconductor.
  7. */
  8. #include <linux/array_size.h>
  9. #include <linux/bitfield.h>
  10. #include <linux/bitmap.h>
  11. #include <linux/bits.h>
  12. #include <linux/device.h>
  13. #include <linux/delay.h>
  14. #include <linux/devm-helpers.h>
  15. #include <linux/err.h>
  16. #include <linux/gpio/driver.h>
  17. #include <linux/i2c.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/irqreturn.h>
  20. #include <linux/module.h>
  21. #include <linux/mod_devicetable.h>
  22. #include <linux/regmap.h>
  23. #include <linux/types.h>
  24. #include <asm/byteorder.h>
  25. #include <linux/iio/events.h>
  26. #include <linux/iio/iio.h>
  27. #include <linux/iio/adc-helpers.h>
  28. #define BD79124_I2C_MULTI_READ 0x30
  29. #define BD79124_I2C_MULTI_WRITE 0x28
  30. #define BD79124_REG_MAX 0xaf
  31. #define BD79124_REG_SYSTEM_STATUS 0x00
  32. #define BD79124_REG_GEN_CFG 0x01
  33. #define BD79124_REG_OPMODE_CFG 0x04
  34. #define BD79124_REG_PINCFG 0x05
  35. #define BD79124_REG_GPO_VAL 0x0B
  36. #define BD79124_REG_SEQ_CFG 0x10
  37. #define BD79124_REG_MANUAL_CHANNELS 0x11
  38. #define BD79124_REG_AUTO_CHANNELS 0x12
  39. #define BD79124_REG_ALERT_CH_SEL 0x14
  40. #define BD79124_REG_EVENT_FLAG 0x18
  41. #define BD79124_REG_EVENT_FLAG_HI 0x1a
  42. #define BD79124_REG_EVENT_FLAG_LO 0x1c
  43. #define BD79124_REG_HYSTERESIS_CH0 0x20
  44. #define BD79124_REG_EVENTCOUNT_CH0 0x22
  45. #define BD79124_REG_RECENT_CH0_LSB 0xa0
  46. #define BD79124_REG_RECENT_CH7_MSB 0xaf
  47. #define BD79124_ADC_BITS 12
  48. /* Masks for the BD79124_REG_OPMODE_CFG */
  49. #define BD79124_MSK_CONV_MODE GENMASK(6, 5)
  50. #define BD79124_CONV_MODE_MANSEQ 0
  51. #define BD79124_CONV_MODE_AUTO 1
  52. #define BD79124_MSK_AUTO_INTERVAL GENMASK(1, 0)
  53. #define BD79124_INTERVAL_750_US 0
  54. /* Masks for the BD79124_REG_GEN_CFG */
  55. #define BD79124_MSK_DWC_EN BIT(4)
  56. #define BD79124_MSK_STATS_EN BIT(5)
  57. /* Masks for the BD79124_REG_SEQ_CFG */
  58. #define BD79124_MSK_SEQ_START BIT(4)
  59. #define BD79124_MSK_SEQ_MODE GENMASK(1, 0)
  60. #define BD79124_MSK_SEQ_MANUAL 0
  61. #define BD79124_MSK_SEQ_SEQ 1
  62. #define BD79124_MSK_HYSTERESIS GENMASK(3, 0)
  63. #define BD79124_LOW_LIMIT_MIN 0
  64. #define BD79124_HIGH_LIMIT_MAX GENMASK(11, 0)
  65. /*
  66. * The high limit, low limit and last measurement result are each stored in
  67. * 2 consequtive registers. 4 bits are in the high bits of the first register
  68. * and 8 bits in the next register.
  69. *
  70. * These macros return the address of the first reg for the given channel.
  71. */
  72. #define BD79124_GET_HIGH_LIMIT_REG(ch) (BD79124_REG_HYSTERESIS_CH0 + (ch) * 4)
  73. #define BD79124_GET_LOW_LIMIT_REG(ch) (BD79124_REG_EVENTCOUNT_CH0 + (ch) * 4)
  74. #define BD79124_GET_LIMIT_REG(ch, dir) ((dir) == IIO_EV_DIR_RISING ? \
  75. BD79124_GET_HIGH_LIMIT_REG(ch) : BD79124_GET_LOW_LIMIT_REG(ch))
  76. #define BD79124_GET_RECENT_RES_REG(ch) (BD79124_REG_RECENT_CH0_LSB + (ch) * 2)
  77. /*
  78. * The hysteresis for a channel is stored in the same register where the
  79. * 4 bits of high limit reside.
  80. */
  81. #define BD79124_GET_HYSTERESIS_REG(ch) BD79124_GET_HIGH_LIMIT_REG(ch)
  82. #define BD79124_MAX_NUM_CHANNELS 8
  83. struct bd79124_data {
  84. s64 timestamp;
  85. struct regmap *map;
  86. struct device *dev;
  87. int vmax;
  88. /*
  89. * Keep measurement status so read_raw() knows if the measurement needs
  90. * to be started.
  91. */
  92. int alarm_monitored[BD79124_MAX_NUM_CHANNELS];
  93. /*
  94. * The BD79124 does not allow disabling/enabling limit separately for
  95. * one direction only. Hence, we do the disabling by changing the limit
  96. * to maximum/minimum measurable value. This means we need to cache
  97. * the limit in order to maintain it over the time limit is disabled.
  98. */
  99. u16 alarm_r_limit[BD79124_MAX_NUM_CHANNELS];
  100. u16 alarm_f_limit[BD79124_MAX_NUM_CHANNELS];
  101. /* Bitmask of disabled events (for rate limiting) for each channel. */
  102. int alarm_suppressed[BD79124_MAX_NUM_CHANNELS];
  103. /*
  104. * The BD79124 is configured to run the measurements in the background.
  105. * This is done for the event monitoring as well as for the read_raw().
  106. * Protect the measurement starting/stopping using a mutex.
  107. */
  108. struct mutex mutex;
  109. struct delayed_work alm_enable_work;
  110. struct gpio_chip gc;
  111. u8 gpio_valid_mask;
  112. };
  113. static const struct regmap_range bd79124_ro_ranges[] = {
  114. regmap_reg_range(BD79124_REG_EVENT_FLAG, BD79124_REG_EVENT_FLAG),
  115. regmap_reg_range(BD79124_REG_RECENT_CH0_LSB, BD79124_REG_RECENT_CH7_MSB),
  116. };
  117. static const struct regmap_access_table bd79124_ro_regs = {
  118. .no_ranges = &bd79124_ro_ranges[0],
  119. .n_no_ranges = ARRAY_SIZE(bd79124_ro_ranges),
  120. };
  121. static const struct regmap_range bd79124_volatile_ranges[] = {
  122. regmap_reg_range(BD79124_REG_RECENT_CH0_LSB, BD79124_REG_RECENT_CH7_MSB),
  123. regmap_reg_range(BD79124_REG_EVENT_FLAG, BD79124_REG_EVENT_FLAG),
  124. regmap_reg_range(BD79124_REG_EVENT_FLAG_HI, BD79124_REG_EVENT_FLAG_HI),
  125. regmap_reg_range(BD79124_REG_EVENT_FLAG_LO, BD79124_REG_EVENT_FLAG_LO),
  126. regmap_reg_range(BD79124_REG_SYSTEM_STATUS, BD79124_REG_SYSTEM_STATUS),
  127. };
  128. static const struct regmap_access_table bd79124_volatile_regs = {
  129. .yes_ranges = &bd79124_volatile_ranges[0],
  130. .n_yes_ranges = ARRAY_SIZE(bd79124_volatile_ranges),
  131. };
  132. static const struct regmap_range bd79124_precious_ranges[] = {
  133. regmap_reg_range(BD79124_REG_EVENT_FLAG_HI, BD79124_REG_EVENT_FLAG_HI),
  134. regmap_reg_range(BD79124_REG_EVENT_FLAG_LO, BD79124_REG_EVENT_FLAG_LO),
  135. };
  136. static const struct regmap_access_table bd79124_precious_regs = {
  137. .yes_ranges = &bd79124_precious_ranges[0],
  138. .n_yes_ranges = ARRAY_SIZE(bd79124_precious_ranges),
  139. };
  140. static const struct regmap_config bd79124_regmap = {
  141. .reg_bits = 16,
  142. .val_bits = 8,
  143. .read_flag_mask = BD79124_I2C_MULTI_READ,
  144. .write_flag_mask = BD79124_I2C_MULTI_WRITE,
  145. .max_register = BD79124_REG_MAX,
  146. .cache_type = REGCACHE_MAPLE,
  147. .volatile_table = &bd79124_volatile_regs,
  148. .wr_table = &bd79124_ro_regs,
  149. .precious_table = &bd79124_precious_regs,
  150. };
  151. static int bd79124gpo_direction_get(struct gpio_chip *gc, unsigned int offset)
  152. {
  153. return GPIO_LINE_DIRECTION_OUT;
  154. }
  155. static int bd79124gpo_set(struct gpio_chip *gc, unsigned int offset, int value)
  156. {
  157. struct bd79124_data *data = gpiochip_get_data(gc);
  158. return regmap_assign_bits(data->map, BD79124_REG_GPO_VAL, BIT(offset),
  159. value);
  160. }
  161. static int bd79124gpo_set_multiple(struct gpio_chip *gc, unsigned long *mask,
  162. unsigned long *bits)
  163. {
  164. unsigned int all_gpos;
  165. int ret;
  166. struct bd79124_data *data = gpiochip_get_data(gc);
  167. /*
  168. * Ensure all GPIOs in 'mask' are set to be GPIOs
  169. * The valid_mask was not obeyed by the gpiolib in all cases prior the
  170. * https://lore.kernel.org/all/cd5e067b80e1bb590027bc3bfa817e7f794f21c3.1741180097.git.mazziesaccount@gmail.com/
  171. *
  172. * Keep this check here for a couple of cycles.
  173. */
  174. ret = regmap_read(data->map, BD79124_REG_PINCFG, &all_gpos);
  175. if (ret)
  176. return ret;
  177. if (all_gpos ^ *mask) {
  178. dev_dbg(data->dev, "Invalid mux config. Can't set value.\n");
  179. return -EINVAL;
  180. }
  181. return regmap_update_bits(data->map, BD79124_REG_GPO_VAL, *mask, *bits);
  182. }
  183. static int bd79124_init_valid_mask(struct gpio_chip *gc,
  184. unsigned long *valid_mask,
  185. unsigned int ngpios)
  186. {
  187. struct bd79124_data *data = gpiochip_get_data(gc);
  188. *valid_mask = data->gpio_valid_mask;
  189. return 0;
  190. }
  191. /* Template for GPIO chip */
  192. static const struct gpio_chip bd79124gpo_chip = {
  193. .label = "bd79124-gpo",
  194. .get_direction = bd79124gpo_direction_get,
  195. .set = bd79124gpo_set,
  196. .set_multiple = bd79124gpo_set_multiple,
  197. .init_valid_mask = bd79124_init_valid_mask,
  198. .can_sleep = true,
  199. .ngpio = 8,
  200. .base = -1,
  201. };
  202. struct bd79124_raw {
  203. u8 val_bit3_0; /* Is set in high bits of the byte */
  204. u8 val_bit11_4;
  205. };
  206. #define BD79124_RAW_TO_INT(r) ((r.val_bit11_4 << 4) | (r.val_bit3_0 >> 4))
  207. #define BD79124_INT_TO_RAW(val) { \
  208. .val_bit11_4 = (val) >> 4, \
  209. .val_bit3_0 = (val) << 4, \
  210. }
  211. /*
  212. * The high and low limits as well as the recent result values are stored in
  213. * the same way in 2 consequent registers. The first register contains 4 bits
  214. * of the value. These bits are stored in the high bits [7:4] of register, but
  215. * they represent the low bits [3:0] of the value.
  216. * The value bits [11:4] are stored in the next register.
  217. *
  218. * Read data from register and convert to integer.
  219. */
  220. static int bd79124_read_reg_to_int(struct bd79124_data *data, int reg,
  221. unsigned int *val)
  222. {
  223. int ret;
  224. struct bd79124_raw raw;
  225. ret = regmap_bulk_read(data->map, reg, &raw, sizeof(raw));
  226. if (ret) {
  227. dev_dbg(data->dev, "bulk_read failed %d\n", ret);
  228. return ret;
  229. }
  230. *val = BD79124_RAW_TO_INT(raw);
  231. return 0;
  232. }
  233. /*
  234. * The high and low limits as well as the recent result values are stored in
  235. * the same way in 2 consequent registers. The first register contains 4 bits
  236. * of the value. These bits are stored in the high bits [7:4] of register, but
  237. * they represent the low bits [3:0] of the value.
  238. * The value bits [11:4] are stored in the next register.
  239. *
  240. * Convert the integer to register format and write it using rmw cycle.
  241. */
  242. static int bd79124_write_int_to_reg(struct bd79124_data *data, int reg,
  243. unsigned int val)
  244. {
  245. struct bd79124_raw raw = BD79124_INT_TO_RAW(val);
  246. unsigned int tmp;
  247. int ret;
  248. ret = regmap_read(data->map, reg, &tmp);
  249. if (ret)
  250. return ret;
  251. raw.val_bit3_0 |= (tmp & 0xf);
  252. return regmap_bulk_write(data->map, reg, &raw, sizeof(raw));
  253. }
  254. static const struct iio_event_spec bd79124_events[] = {
  255. {
  256. .type = IIO_EV_TYPE_THRESH,
  257. .dir = IIO_EV_DIR_RISING,
  258. .mask_separate = BIT(IIO_EV_INFO_VALUE) |
  259. BIT(IIO_EV_INFO_ENABLE),
  260. },
  261. {
  262. .type = IIO_EV_TYPE_THRESH,
  263. .dir = IIO_EV_DIR_FALLING,
  264. .mask_separate = BIT(IIO_EV_INFO_VALUE) |
  265. BIT(IIO_EV_INFO_ENABLE),
  266. },
  267. {
  268. .type = IIO_EV_TYPE_THRESH,
  269. .dir = IIO_EV_DIR_EITHER,
  270. .mask_separate = BIT(IIO_EV_INFO_HYSTERESIS),
  271. },
  272. };
  273. static const struct iio_chan_spec bd79124_chan_template_noirq = {
  274. .type = IIO_VOLTAGE,
  275. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  276. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
  277. .indexed = 1,
  278. };
  279. static const struct iio_chan_spec bd79124_chan_template = {
  280. .type = IIO_VOLTAGE,
  281. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  282. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
  283. .indexed = 1,
  284. .event_spec = bd79124_events,
  285. .num_event_specs = ARRAY_SIZE(bd79124_events),
  286. };
  287. static int bd79124_read_event_value(struct iio_dev *iio_dev,
  288. const struct iio_chan_spec *chan,
  289. enum iio_event_type type,
  290. enum iio_event_direction dir,
  291. enum iio_event_info info, int *val,
  292. int *val2)
  293. {
  294. struct bd79124_data *data = iio_priv(iio_dev);
  295. int ret, reg;
  296. if (chan->channel >= BD79124_MAX_NUM_CHANNELS)
  297. return -EINVAL;
  298. switch (info) {
  299. case IIO_EV_INFO_VALUE:
  300. if (dir == IIO_EV_DIR_RISING)
  301. *val = data->alarm_r_limit[chan->channel];
  302. else if (dir == IIO_EV_DIR_FALLING)
  303. *val = data->alarm_f_limit[chan->channel];
  304. else
  305. return -EINVAL;
  306. return IIO_VAL_INT;
  307. case IIO_EV_INFO_HYSTERESIS:
  308. reg = BD79124_GET_HYSTERESIS_REG(chan->channel);
  309. ret = regmap_read(data->map, reg, val);
  310. if (ret)
  311. return ret;
  312. *val &= BD79124_MSK_HYSTERESIS;
  313. /*
  314. * The data-sheet says the hysteresis register value needs to be
  315. * shifted left by 3.
  316. */
  317. *val <<= 3;
  318. return IIO_VAL_INT;
  319. default:
  320. return -EINVAL;
  321. }
  322. }
  323. static int bd79124_start_measurement(struct bd79124_data *data, int chan)
  324. {
  325. unsigned int val, regval;
  326. int ret;
  327. /* See if already started */
  328. ret = regmap_read(data->map, BD79124_REG_AUTO_CHANNELS, &val);
  329. if (val & BIT(chan))
  330. return 0;
  331. /*
  332. * The sequencer must be stopped when channels are added/removed from
  333. * the list of the measured channels to ensure the new channel
  334. * configuration is used.
  335. */
  336. ret = regmap_clear_bits(data->map, BD79124_REG_SEQ_CFG,
  337. BD79124_MSK_SEQ_START);
  338. if (ret)
  339. return ret;
  340. ret = regmap_write(data->map, BD79124_REG_AUTO_CHANNELS, val | BIT(chan));
  341. if (ret)
  342. return ret;
  343. ret = regmap_set_bits(data->map, BD79124_REG_SEQ_CFG,
  344. BD79124_MSK_SEQ_START);
  345. if (ret)
  346. return ret;
  347. /*
  348. * Start the measurement at the background. Don't bother checking if
  349. * it was started, regmap has cache.
  350. */
  351. regval = FIELD_PREP(BD79124_MSK_CONV_MODE, BD79124_CONV_MODE_AUTO);
  352. return regmap_update_bits(data->map, BD79124_REG_OPMODE_CFG,
  353. BD79124_MSK_CONV_MODE, regval);
  354. }
  355. static int bd79124_stop_measurement(struct bd79124_data *data, int chan)
  356. {
  357. unsigned int enabled_chans;
  358. int ret;
  359. /* See if already stopped */
  360. ret = regmap_read(data->map, BD79124_REG_AUTO_CHANNELS, &enabled_chans);
  361. if (!(enabled_chans & BIT(chan)))
  362. return 0;
  363. ret = regmap_clear_bits(data->map, BD79124_REG_SEQ_CFG,
  364. BD79124_MSK_SEQ_START);
  365. /* Clear the channel from the measured channels */
  366. enabled_chans &= ~BIT(chan);
  367. ret = regmap_write(data->map, BD79124_REG_AUTO_CHANNELS,
  368. enabled_chans);
  369. if (ret)
  370. return ret;
  371. /*
  372. * Stop background conversion for power saving if it was the last
  373. * channel.
  374. */
  375. if (!enabled_chans) {
  376. int regval = FIELD_PREP(BD79124_MSK_CONV_MODE,
  377. BD79124_CONV_MODE_MANSEQ);
  378. ret = regmap_update_bits(data->map, BD79124_REG_OPMODE_CFG,
  379. BD79124_MSK_CONV_MODE, regval);
  380. if (ret)
  381. return ret;
  382. }
  383. return regmap_set_bits(data->map, BD79124_REG_SEQ_CFG,
  384. BD79124_MSK_SEQ_START);
  385. }
  386. static int bd79124_read_event_config(struct iio_dev *iio_dev,
  387. const struct iio_chan_spec *chan,
  388. enum iio_event_type type,
  389. enum iio_event_direction dir)
  390. {
  391. struct bd79124_data *data = iio_priv(iio_dev);
  392. if (chan->channel >= BD79124_MAX_NUM_CHANNELS)
  393. return -EINVAL;
  394. return !!(data->alarm_monitored[chan->channel] & BIT(dir));
  395. }
  396. static int bd79124_disable_event(struct bd79124_data *data,
  397. enum iio_event_direction dir, int channel)
  398. {
  399. int dir_bit = BIT(dir);
  400. int reg;
  401. unsigned int limit;
  402. guard(mutex)(&data->mutex);
  403. /*
  404. * Set thresholds either to 0 or to 2^12 - 1 as appropriate to prevent
  405. * alerts and thus disable event generation.
  406. */
  407. if (dir == IIO_EV_DIR_RISING) {
  408. reg = BD79124_GET_HIGH_LIMIT_REG(channel);
  409. limit = BD79124_HIGH_LIMIT_MAX;
  410. } else if (dir == IIO_EV_DIR_FALLING) {
  411. reg = BD79124_GET_LOW_LIMIT_REG(channel);
  412. limit = BD79124_LOW_LIMIT_MIN;
  413. } else {
  414. return -EINVAL;
  415. }
  416. data->alarm_monitored[channel] &= ~dir_bit;
  417. /*
  418. * Stop measurement if there is no more events to monitor.
  419. * We don't bother checking the retval because the limit
  420. * setting should in any case effectively disable the alarm.
  421. */
  422. if (!data->alarm_monitored[channel]) {
  423. bd79124_stop_measurement(data, channel);
  424. regmap_clear_bits(data->map, BD79124_REG_ALERT_CH_SEL,
  425. BIT(channel));
  426. }
  427. return bd79124_write_int_to_reg(data, reg, limit);
  428. }
  429. static int bd79124_enable_event(struct bd79124_data *data,
  430. enum iio_event_direction dir,
  431. unsigned int channel)
  432. {
  433. int dir_bit = BIT(dir);
  434. int reg, ret;
  435. u16 *limit;
  436. guard(mutex)(&data->mutex);
  437. ret = bd79124_start_measurement(data, channel);
  438. if (ret)
  439. return ret;
  440. data->alarm_monitored[channel] |= dir_bit;
  441. /* Add the channel to the list of monitored channels */
  442. ret = regmap_set_bits(data->map, BD79124_REG_ALERT_CH_SEL, BIT(channel));
  443. if (ret)
  444. return ret;
  445. if (dir == IIO_EV_DIR_RISING) {
  446. limit = &data->alarm_f_limit[channel];
  447. reg = BD79124_GET_HIGH_LIMIT_REG(channel);
  448. } else {
  449. limit = &data->alarm_f_limit[channel];
  450. reg = BD79124_GET_LOW_LIMIT_REG(channel);
  451. }
  452. /*
  453. * Don't write the new limit to the hardware if we are in the
  454. * rate-limit period. The timer which re-enables the event will set
  455. * the limit.
  456. */
  457. if (!(data->alarm_suppressed[channel] & dir_bit)) {
  458. ret = bd79124_write_int_to_reg(data, reg, *limit);
  459. if (ret)
  460. return ret;
  461. }
  462. /*
  463. * Enable comparator. Trust the regmap cache, no need to check
  464. * if it was already enabled.
  465. *
  466. * We could do this in the hw-init, but there may be users who
  467. * never enable alarms and for them it makes sense to not
  468. * enable the comparator at probe.
  469. */
  470. return regmap_set_bits(data->map, BD79124_REG_GEN_CFG,
  471. BD79124_MSK_DWC_EN);
  472. }
  473. static int bd79124_write_event_config(struct iio_dev *iio_dev,
  474. const struct iio_chan_spec *chan,
  475. enum iio_event_type type,
  476. enum iio_event_direction dir, bool state)
  477. {
  478. struct bd79124_data *data = iio_priv(iio_dev);
  479. if (chan->channel >= BD79124_MAX_NUM_CHANNELS)
  480. return -EINVAL;
  481. if (state)
  482. return bd79124_enable_event(data, dir, chan->channel);
  483. return bd79124_disable_event(data, dir, chan->channel);
  484. }
  485. static int bd79124_write_event_value(struct iio_dev *iio_dev,
  486. const struct iio_chan_spec *chan,
  487. enum iio_event_type type,
  488. enum iio_event_direction dir,
  489. enum iio_event_info info, int val,
  490. int val2)
  491. {
  492. struct bd79124_data *data = iio_priv(iio_dev);
  493. int reg;
  494. if (chan->channel >= BD79124_MAX_NUM_CHANNELS)
  495. return -EINVAL;
  496. switch (info) {
  497. case IIO_EV_INFO_VALUE:
  498. {
  499. guard(mutex)(&data->mutex);
  500. if (dir == IIO_EV_DIR_RISING) {
  501. data->alarm_r_limit[chan->channel] = val;
  502. reg = BD79124_GET_HIGH_LIMIT_REG(chan->channel);
  503. } else if (dir == IIO_EV_DIR_FALLING) {
  504. data->alarm_f_limit[chan->channel] = val;
  505. reg = BD79124_GET_LOW_LIMIT_REG(chan->channel);
  506. } else {
  507. return -EINVAL;
  508. }
  509. /*
  510. * We don't want to enable the alarm if it is not enabled or
  511. * if it is suppressed. In that case skip writing to the
  512. * register.
  513. */
  514. if (!(data->alarm_monitored[chan->channel] & BIT(dir)) ||
  515. data->alarm_suppressed[chan->channel] & BIT(dir))
  516. return 0;
  517. return bd79124_write_int_to_reg(data, reg, val);
  518. }
  519. case IIO_EV_INFO_HYSTERESIS:
  520. reg = BD79124_GET_HYSTERESIS_REG(chan->channel);
  521. val >>= 3;
  522. return regmap_update_bits(data->map, reg, BD79124_MSK_HYSTERESIS,
  523. val);
  524. default:
  525. return -EINVAL;
  526. }
  527. }
  528. static int bd79124_single_chan_seq(struct bd79124_data *data, int chan, unsigned int *old)
  529. {
  530. int ret;
  531. ret = regmap_clear_bits(data->map, BD79124_REG_SEQ_CFG,
  532. BD79124_MSK_SEQ_START);
  533. if (ret)
  534. return ret;
  535. /*
  536. * It may be we have some channels monitored for alarms so we want to
  537. * cache the old config and return it when the single channel
  538. * measurement has been completed.
  539. */
  540. ret = regmap_read(data->map, BD79124_REG_AUTO_CHANNELS, old);
  541. if (ret)
  542. return ret;
  543. ret = regmap_write(data->map, BD79124_REG_AUTO_CHANNELS, BIT(chan));
  544. if (ret)
  545. return ret;
  546. /* Restart the sequencer */
  547. return regmap_set_bits(data->map, BD79124_REG_SEQ_CFG,
  548. BD79124_MSK_SEQ_START);
  549. }
  550. static int bd79124_single_chan_seq_end(struct bd79124_data *data, unsigned int old)
  551. {
  552. int ret;
  553. ret = regmap_clear_bits(data->map, BD79124_REG_SEQ_CFG,
  554. BD79124_MSK_SEQ_START);
  555. if (ret)
  556. return ret;
  557. ret = regmap_write(data->map, BD79124_REG_AUTO_CHANNELS, old);
  558. if (ret)
  559. return ret;
  560. return regmap_set_bits(data->map, BD79124_REG_SEQ_CFG,
  561. BD79124_MSK_SEQ_START);
  562. }
  563. static int bd79124_read_raw(struct iio_dev *iio_dev,
  564. struct iio_chan_spec const *chan,
  565. int *val, int *val2, long m)
  566. {
  567. struct bd79124_data *data = iio_priv(iio_dev);
  568. int ret;
  569. if (chan->channel >= BD79124_MAX_NUM_CHANNELS)
  570. return -EINVAL;
  571. switch (m) {
  572. case IIO_CHAN_INFO_RAW:
  573. {
  574. unsigned int old_chan_cfg, regval;
  575. int tmp;
  576. guard(mutex)(&data->mutex);
  577. /*
  578. * Start the automatic conversion. This is needed here if no
  579. * events have been enabled.
  580. */
  581. regval = FIELD_PREP(BD79124_MSK_CONV_MODE,
  582. BD79124_CONV_MODE_AUTO);
  583. ret = regmap_update_bits(data->map, BD79124_REG_OPMODE_CFG,
  584. BD79124_MSK_CONV_MODE, regval);
  585. if (ret)
  586. return ret;
  587. ret = bd79124_single_chan_seq(data, chan->channel, &old_chan_cfg);
  588. if (ret)
  589. return ret;
  590. /* The maximum conversion time is 6 uS. */
  591. udelay(6);
  592. ret = bd79124_read_reg_to_int(data,
  593. BD79124_GET_RECENT_RES_REG(chan->channel), val);
  594. /*
  595. * Return the old chan config even if data reading failed in
  596. * order to re-enable the event monitoring.
  597. */
  598. tmp = bd79124_single_chan_seq_end(data, old_chan_cfg);
  599. if (tmp)
  600. dev_err(data->dev,
  601. "Failed to return config. Alarms may be disabled\n");
  602. if (ret)
  603. return ret;
  604. return IIO_VAL_INT;
  605. }
  606. case IIO_CHAN_INFO_SCALE:
  607. *val = data->vmax / 1000;
  608. *val2 = BD79124_ADC_BITS;
  609. return IIO_VAL_FRACTIONAL_LOG2;
  610. default:
  611. return -EINVAL;
  612. }
  613. }
  614. static const struct iio_info bd79124_info = {
  615. .read_raw = bd79124_read_raw,
  616. .read_event_config = &bd79124_read_event_config,
  617. .write_event_config = &bd79124_write_event_config,
  618. .read_event_value = &bd79124_read_event_value,
  619. .write_event_value = &bd79124_write_event_value,
  620. };
  621. static void bd79124_re_enable_lo(struct bd79124_data *data, unsigned int channel)
  622. {
  623. int ret, evbit = BIT(IIO_EV_DIR_FALLING);
  624. /*
  625. * We should not re-enable the event if user has disabled it while
  626. * rate-limiting was enabled.
  627. */
  628. if (!(data->alarm_suppressed[channel] & evbit))
  629. return;
  630. data->alarm_suppressed[channel] &= ~evbit;
  631. if (!(data->alarm_monitored[channel] & evbit))
  632. return;
  633. ret = bd79124_write_int_to_reg(data, BD79124_GET_LOW_LIMIT_REG(channel),
  634. data->alarm_f_limit[channel]);
  635. if (ret)
  636. dev_warn(data->dev, "Low limit enabling failed for channel%d\n",
  637. channel);
  638. }
  639. static void bd79124_re_enable_hi(struct bd79124_data *data, unsigned int channel)
  640. {
  641. int ret, evbit = BIT(IIO_EV_DIR_RISING);
  642. /*
  643. * We should not re-enable the event if user has disabled it while
  644. * rate-limiting was enabled.
  645. */
  646. if (!(data->alarm_suppressed[channel] & evbit))
  647. return;
  648. data->alarm_suppressed[channel] &= ~evbit;
  649. if (!(data->alarm_monitored[channel] & evbit))
  650. return;
  651. ret = bd79124_write_int_to_reg(data, BD79124_GET_HIGH_LIMIT_REG(channel),
  652. data->alarm_r_limit[channel]);
  653. if (ret)
  654. dev_warn(data->dev, "High limit enabling failed for channel%d\n",
  655. channel);
  656. }
  657. static void bd79124_alm_enable_worker(struct work_struct *work)
  658. {
  659. int i;
  660. struct bd79124_data *data = container_of(work, struct bd79124_data,
  661. alm_enable_work.work);
  662. /* Take the mutex so there is no race with user disabling the alarm */
  663. guard(mutex)(&data->mutex);
  664. for (i = 0; i < BD79124_MAX_NUM_CHANNELS; i++) {
  665. bd79124_re_enable_hi(data, i);
  666. bd79124_re_enable_lo(data, i);
  667. }
  668. }
  669. static int __bd79124_event_ratelimit(struct bd79124_data *data, int reg,
  670. unsigned int limit)
  671. {
  672. int ret;
  673. if (limit > BD79124_HIGH_LIMIT_MAX)
  674. return -EINVAL;
  675. ret = bd79124_write_int_to_reg(data, reg, limit);
  676. if (ret)
  677. return ret;
  678. /*
  679. * We use 1 sec 'grace period'. At the moment I see no reason to make
  680. * this user configurable. We need an ABI for this if configuration is
  681. * needed.
  682. */
  683. schedule_delayed_work(&data->alm_enable_work, msecs_to_jiffies(1000));
  684. return 0;
  685. }
  686. static int bd79124_event_ratelimit_hi(struct bd79124_data *data,
  687. unsigned int channel)
  688. {
  689. guard(mutex)(&data->mutex);
  690. data->alarm_suppressed[channel] |= BIT(IIO_EV_DIR_RISING);
  691. return __bd79124_event_ratelimit(data,
  692. BD79124_GET_HIGH_LIMIT_REG(channel),
  693. BD79124_HIGH_LIMIT_MAX);
  694. }
  695. static int bd79124_event_ratelimit_lo(struct bd79124_data *data,
  696. unsigned int channel)
  697. {
  698. guard(mutex)(&data->mutex);
  699. data->alarm_suppressed[channel] |= BIT(IIO_EV_DIR_FALLING);
  700. return __bd79124_event_ratelimit(data,
  701. BD79124_GET_LOW_LIMIT_REG(channel),
  702. BD79124_LOW_LIMIT_MIN);
  703. }
  704. static irqreturn_t bd79124_event_handler(int irq, void *priv)
  705. {
  706. unsigned int i_hi, i_lo;
  707. int i, ret;
  708. struct iio_dev *iio_dev = priv;
  709. struct bd79124_data *data = iio_priv(iio_dev);
  710. /*
  711. * Return IRQ_NONE if bailing-out without acking. This allows the IRQ
  712. * subsystem to disable the offending IRQ line if we get a hardware
  713. * problem. This behaviour has saved my poor bottom a few times in the
  714. * past as, instead of getting unusably unresponsive, the system has
  715. * spilled out the magic words "...nobody cared".
  716. */
  717. ret = regmap_read(data->map, BD79124_REG_EVENT_FLAG_HI, &i_hi);
  718. if (ret)
  719. return IRQ_NONE;
  720. ret = regmap_read(data->map, BD79124_REG_EVENT_FLAG_LO, &i_lo);
  721. if (ret)
  722. return IRQ_NONE;
  723. if (!i_lo && !i_hi)
  724. return IRQ_NONE;
  725. for (i = 0; i < BD79124_MAX_NUM_CHANNELS; i++) {
  726. u64 ecode;
  727. if (BIT(i) & i_hi) {
  728. ecode = IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, i,
  729. IIO_EV_TYPE_THRESH,
  730. IIO_EV_DIR_RISING);
  731. iio_push_event(iio_dev, ecode, data->timestamp);
  732. /*
  733. * The BD79124 keeps the IRQ asserted for as long as
  734. * the voltage exceeds the threshold. It causes the IRQ
  735. * to keep firing.
  736. *
  737. * Disable the event for the channel and schedule the
  738. * re-enabling the event later to prevent storm of
  739. * events.
  740. */
  741. ret = bd79124_event_ratelimit_hi(data, i);
  742. if (ret)
  743. return IRQ_NONE;
  744. }
  745. if (BIT(i) & i_lo) {
  746. ecode = IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, i,
  747. IIO_EV_TYPE_THRESH,
  748. IIO_EV_DIR_FALLING);
  749. iio_push_event(iio_dev, ecode, data->timestamp);
  750. ret = bd79124_event_ratelimit_lo(data, i);
  751. if (ret)
  752. return IRQ_NONE;
  753. }
  754. }
  755. ret = regmap_write(data->map, BD79124_REG_EVENT_FLAG_HI, i_hi);
  756. if (ret)
  757. return IRQ_NONE;
  758. ret = regmap_write(data->map, BD79124_REG_EVENT_FLAG_LO, i_lo);
  759. if (ret)
  760. return IRQ_NONE;
  761. return IRQ_HANDLED;
  762. }
  763. static irqreturn_t bd79124_irq_handler(int irq, void *priv)
  764. {
  765. struct iio_dev *iio_dev = priv;
  766. struct bd79124_data *data = iio_priv(iio_dev);
  767. data->timestamp = iio_get_time_ns(iio_dev);
  768. return IRQ_WAKE_THREAD;
  769. }
  770. static int bd79124_chan_init(struct bd79124_data *data, int channel)
  771. {
  772. int ret;
  773. ret = regmap_write(data->map, BD79124_GET_HIGH_LIMIT_REG(channel),
  774. BD79124_HIGH_LIMIT_MAX);
  775. if (ret)
  776. return ret;
  777. return regmap_write(data->map, BD79124_GET_LOW_LIMIT_REG(channel),
  778. BD79124_LOW_LIMIT_MIN);
  779. }
  780. static int bd79124_get_gpio_pins(const struct iio_chan_spec *cs, int num_channels)
  781. {
  782. int i, gpio_channels;
  783. /*
  784. * Let's initialize the mux config to say that all 8 channels are
  785. * GPIOs. Then we can just loop through the iio_chan_spec and clear the
  786. * bits for found ADC channels.
  787. */
  788. gpio_channels = GENMASK(7, 0);
  789. for (i = 0; i < num_channels; i++)
  790. gpio_channels &= ~BIT(cs[i].channel);
  791. return gpio_channels;
  792. }
  793. static int bd79124_hw_init(struct bd79124_data *data)
  794. {
  795. unsigned int regval;
  796. int ret, i;
  797. for (i = 0; i < BD79124_MAX_NUM_CHANNELS; i++) {
  798. ret = bd79124_chan_init(data, i);
  799. if (ret)
  800. return ret;
  801. data->alarm_r_limit[i] = BD79124_HIGH_LIMIT_MAX;
  802. }
  803. /* Stop auto sequencer */
  804. ret = regmap_clear_bits(data->map, BD79124_REG_SEQ_CFG,
  805. BD79124_MSK_SEQ_START);
  806. if (ret)
  807. return ret;
  808. /* Enable writing the measured values to the regsters */
  809. ret = regmap_set_bits(data->map, BD79124_REG_GEN_CFG,
  810. BD79124_MSK_STATS_EN);
  811. if (ret)
  812. return ret;
  813. /* Set no channels to be auto-measured */
  814. ret = regmap_write(data->map, BD79124_REG_AUTO_CHANNELS, 0x0);
  815. if (ret)
  816. return ret;
  817. /* Set no channels to be manually measured */
  818. ret = regmap_write(data->map, BD79124_REG_MANUAL_CHANNELS, 0x0);
  819. if (ret)
  820. return ret;
  821. regval = FIELD_PREP(BD79124_MSK_AUTO_INTERVAL, BD79124_INTERVAL_750_US);
  822. ret = regmap_update_bits(data->map, BD79124_REG_OPMODE_CFG,
  823. BD79124_MSK_AUTO_INTERVAL, regval);
  824. if (ret)
  825. return ret;
  826. /* Sequencer mode to auto */
  827. ret = regmap_set_bits(data->map, BD79124_REG_SEQ_CFG,
  828. BD79124_MSK_SEQ_SEQ);
  829. if (ret)
  830. return ret;
  831. /* Don't start the measurement */
  832. regval = FIELD_PREP(BD79124_MSK_CONV_MODE, BD79124_CONV_MODE_MANSEQ);
  833. return regmap_update_bits(data->map, BD79124_REG_OPMODE_CFG,
  834. BD79124_MSK_CONV_MODE, regval);
  835. }
  836. static int bd79124_probe(struct i2c_client *i2c)
  837. {
  838. struct bd79124_data *data;
  839. struct iio_dev *iio_dev;
  840. const struct iio_chan_spec *template;
  841. struct iio_chan_spec *cs;
  842. struct device *dev = &i2c->dev;
  843. unsigned int gpio_pins;
  844. int ret;
  845. iio_dev = devm_iio_device_alloc(dev, sizeof(*data));
  846. if (!iio_dev)
  847. return -ENOMEM;
  848. data = iio_priv(iio_dev);
  849. data->dev = dev;
  850. data->map = devm_regmap_init_i2c(i2c, &bd79124_regmap);
  851. if (IS_ERR(data->map))
  852. return dev_err_probe(dev, PTR_ERR(data->map),
  853. "Failed to initialize Regmap\n");
  854. ret = devm_regulator_get_enable_read_voltage(dev, "vdd");
  855. if (ret < 0)
  856. return dev_err_probe(dev, ret, "Failed to get the Vdd\n");
  857. data->vmax = ret;
  858. ret = devm_regulator_get_enable(dev, "iovdd");
  859. if (ret < 0)
  860. return dev_err_probe(dev, ret, "Failed to enable I/O voltage\n");
  861. ret = devm_delayed_work_autocancel(dev, &data->alm_enable_work,
  862. bd79124_alm_enable_worker);
  863. if (ret)
  864. return ret;
  865. if (i2c->irq) {
  866. template = &bd79124_chan_template;
  867. } else {
  868. template = &bd79124_chan_template_noirq;
  869. dev_dbg(dev, "No IRQ found, events disabled\n");
  870. }
  871. ret = devm_mutex_init(dev, &data->mutex);
  872. if (ret)
  873. return ret;
  874. ret = devm_iio_adc_device_alloc_chaninfo_se(dev, template,
  875. BD79124_MAX_NUM_CHANNELS - 1, &cs);
  876. if (ret < 0) {
  877. /* Register all pins as GPOs if there are no ADC channels */
  878. if (ret == -ENOENT)
  879. goto register_gpios;
  880. return ret;
  881. }
  882. iio_dev->channels = cs;
  883. iio_dev->num_channels = ret;
  884. iio_dev->info = &bd79124_info;
  885. iio_dev->name = "bd79124";
  886. iio_dev->modes = INDIO_DIRECT_MODE;
  887. ret = bd79124_hw_init(data);
  888. if (ret)
  889. return ret;
  890. if (i2c->irq > 0) {
  891. ret = devm_request_threaded_irq(dev, i2c->irq,
  892. bd79124_irq_handler, &bd79124_event_handler,
  893. IRQF_ONESHOT, "adc-thresh-alert", iio_dev);
  894. if (ret)
  895. return dev_err_probe(data->dev, ret,
  896. "Failed to register IRQ\n");
  897. }
  898. ret = devm_iio_device_register(data->dev, iio_dev);
  899. if (ret)
  900. return dev_err_probe(data->dev, ret, "Failed to register ADC\n");
  901. register_gpios:
  902. gpio_pins = bd79124_get_gpio_pins(iio_dev->channels,
  903. iio_dev->num_channels);
  904. /*
  905. * The mux should default to "all ADCs", but better to not trust it.
  906. * Thus we do set the mux even when we have only ADCs and no GPOs.
  907. */
  908. ret = regmap_write(data->map, BD79124_REG_PINCFG, gpio_pins);
  909. if (ret)
  910. return ret;
  911. /* No GPOs if all channels are reserved for ADC, so we're done. */
  912. if (!gpio_pins)
  913. return 0;
  914. data->gpio_valid_mask = gpio_pins;
  915. data->gc = bd79124gpo_chip;
  916. data->gc.parent = dev;
  917. return devm_gpiochip_add_data(dev, &data->gc, data);
  918. }
  919. static const struct of_device_id bd79124_of_match[] = {
  920. { .compatible = "rohm,bd79124" },
  921. { }
  922. };
  923. MODULE_DEVICE_TABLE(of, bd79124_of_match);
  924. static const struct i2c_device_id bd79124_id[] = {
  925. { "bd79124" },
  926. { }
  927. };
  928. MODULE_DEVICE_TABLE(i2c, bd79124_id);
  929. static struct i2c_driver bd79124_driver = {
  930. .driver = {
  931. .name = "bd79124",
  932. .of_match_table = bd79124_of_match,
  933. },
  934. .probe = bd79124_probe,
  935. .id_table = bd79124_id,
  936. };
  937. module_i2c_driver(bd79124_driver);
  938. MODULE_AUTHOR("Matti Vaittinen <mazziesaccount@gmail.com>");
  939. MODULE_DESCRIPTION("Driver for ROHM BD79124 ADC");
  940. MODULE_LICENSE("GPL");
  941. MODULE_IMPORT_NS("IIO_DRIVER");