rohm-bd79112.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * ROHM ADC driver for BD79112 signal monitoring hub.
  4. * Copyright (C) 2025, ROHM Semiconductor.
  5. *
  6. * SPI communication derived from ad7923.c and ti-ads7950.c
  7. */
  8. #include <linux/array_size.h>
  9. #include <linux/bitfield.h>
  10. #include <linux/bitops.h>
  11. #include <linux/bits.h>
  12. #include <linux/dev_printk.h>
  13. #include <linux/err.h>
  14. #include <linux/errno.h>
  15. #include <linux/gpio/driver.h>
  16. #include <linux/mod_devicetable.h>
  17. #include <linux/module.h>
  18. #include <linux/regmap.h>
  19. #include <linux/regulator/consumer.h>
  20. #include <linux/spi/spi.h>
  21. #include <linux/types.h>
  22. #include <asm/byteorder.h>
  23. #include <linux/iio/adc-helpers.h>
  24. #include <linux/iio/iio.h>
  25. #define BD79112_MAX_NUM_CHANNELS 32
  26. struct bd79112_data {
  27. struct spi_device *spi;
  28. struct regmap *map;
  29. struct device *dev;
  30. struct gpio_chip gc;
  31. unsigned long gpio_valid_mask;
  32. unsigned int vref_mv;
  33. struct spi_transfer read_xfer[2];
  34. struct spi_transfer write_xfer;
  35. struct spi_message read_msg;
  36. struct spi_message write_msg;
  37. /* 16-bit TX, valid data in high byte */
  38. u8 read_tx[2] __aligned(IIO_DMA_MINALIGN);
  39. /* 8-bit address followed by 8-bit data */
  40. u8 reg_write_tx[2];
  41. /* 12-bit of ADC data or 8 bit of reg data */
  42. __be16 read_rx;
  43. };
  44. /*
  45. * The ADC data is read issuing SPI-command matching the channel number.
  46. * We treat this as a register address.
  47. */
  48. #define BD79112_REG_AGIO0A 0x00
  49. #define BD79112_REG_AGIO15B 0x1f
  50. /*
  51. * ADC STATUS_FLAG appended to ADC data will be set, if the ADC result is being
  52. * read for a channel, which input pin is muxed to be a GPIO.
  53. */
  54. #define BD79112_ADC_STATUS_FLAG BIT(14)
  55. /*
  56. * The BD79112 requires "R/W bit" to be set for SPI register (not ADC data)
  57. * reads and an "IOSET bit" to be set for read/write operations (which aren't
  58. * reading the ADC data).
  59. */
  60. #define BD79112_BIT_RW BIT(4)
  61. #define BD79112_BIT_IO BIT(5)
  62. #define BD79112_REG_GPI_VALUE_B8_15 (BD79112_BIT_IO | 0x0)
  63. #define BD79112_REG_GPI_VALUE_B0_B7 (BD79112_BIT_IO | 0x1)
  64. #define BD79112_REG_GPI_VALUE_A8_15 (BD79112_BIT_IO | 0x2)
  65. #define BD79112_REG_GPI_VALUE_A0_A7 (BD79112_BIT_IO | 0x3)
  66. #define BD79112_REG_GPI_EN_B7_B15 (BD79112_BIT_IO | 0x4)
  67. #define BD79112_REG_GPI_EN_B0_B7 (BD79112_BIT_IO | 0x5)
  68. #define BD79112_REG_GPI_EN_A8_A15 (BD79112_BIT_IO | 0x6)
  69. #define BD79112_REG_GPI_EN_A0_A7 (BD79112_BIT_IO | 0x7)
  70. #define BD79112_REG_GPO_EN_B7_B15 (BD79112_BIT_IO | 0x8)
  71. #define BD79112_REG_GPO_EN_B0_B7 (BD79112_BIT_IO | 0x9)
  72. #define BD79112_REG_GPO_EN_A8_A15 (BD79112_BIT_IO | 0xa)
  73. #define BD79112_REG_GPO_EN_A0_A7 (BD79112_BIT_IO | 0xb)
  74. #define BD79112_NUM_GPIO_EN_REGS 8
  75. #define BD79112_FIRST_GPIO_EN_REG BD79112_REG_GPI_EN_B7_B15
  76. #define BD79112_REG_GPO_VALUE_B8_15 (BD79112_BIT_IO | 0xc)
  77. #define BD79112_REG_GPO_VALUE_B0_B7 (BD79112_BIT_IO | 0xd)
  78. #define BD79112_REG_GPO_VALUE_A8_15 (BD79112_BIT_IO | 0xe)
  79. #define BD79112_REG_GPO_VALUE_A0_A7 (BD79112_BIT_IO | 0xf)
  80. #define BD79112_REG_MAX BD79112_REG_GPO_VALUE_A0_A7
  81. /*
  82. * Read transaction consists of two 16-bit sequences separated by CSB.
  83. * For register read, 'IOSET' bit must be set. For ADC read, IOSET is cleared
  84. * and ADDR equals the channel number (0 ... 31).
  85. *
  86. * First 16-bit sequence, MOSI as below, MISO data ignored:
  87. * - SCK: | 1 | 2 | 3 | 4 | 5 .. 8 | 9 .. 16 |
  88. * - MOSI:| 0 | 0 | IOSET | RW (1) | ADDR | 8'b0 |
  89. *
  90. * CSB released and re-acquired between these sequences
  91. *
  92. * Second 16-bit sequence, MISO as below, MOSI data ignored:
  93. * For Register read data is 8 bits:
  94. * - SCK: | 1 .. 8 | 9 .. 16 |
  95. * - MISO:| 8'b0 | 8-bit data |
  96. *
  97. * For ADC read data is 12 bits:
  98. * - SCK: | 1 | 2 | 3 4 | 4 .. 16 |
  99. * - MISO:| 0 | STATUS_FLAG | 2'b0 | 12-bit data |
  100. * The 'STATUS_FLAG' is set if the read input pin was configured as a GPIO.
  101. */
  102. static int bd79112_reg_read(void *context, unsigned int reg, unsigned int *val)
  103. {
  104. struct bd79112_data *data = context;
  105. int ret;
  106. if (reg & BD79112_BIT_IO)
  107. reg |= BD79112_BIT_RW;
  108. data->read_tx[0] = reg;
  109. ret = spi_sync(data->spi, &data->read_msg);
  110. if (!ret)
  111. *val = be16_to_cpu(data->read_rx);
  112. return ret;
  113. }
  114. /*
  115. * Write, single 16-bit sequence (broken down below):
  116. *
  117. * First 8-bit, MOSI as below, MISO data ignored:
  118. * - SCK: | 1 | 2 | 3 | 4 | 5 .. 8 |
  119. * - MOSI:| 0 | 0 |IOSET| RW(0) | ADDR |
  120. *
  121. * Last 8 SCK cycles (b8 ... b15), MISO contains register data, MOSI ignored.
  122. * - SCK: | 9 .. 16 |
  123. * - MISO:| data |
  124. */
  125. static int bd79112_reg_write(void *context, unsigned int reg, unsigned int val)
  126. {
  127. struct bd79112_data *data = context;
  128. data->reg_write_tx[0] = reg;
  129. data->reg_write_tx[1] = val;
  130. return spi_sync(data->spi, &data->write_msg);
  131. }
  132. static int _get_gpio_reg(unsigned int offset, unsigned int base)
  133. {
  134. int regoffset = offset / 8;
  135. if (offset > 31)
  136. return -EINVAL;
  137. return base - regoffset;
  138. }
  139. #define GET_GPIO_BIT(offset) BIT((offset) % 8)
  140. #define GET_GPO_EN_REG(offset) _get_gpio_reg((offset), BD79112_REG_GPO_EN_A0_A7)
  141. #define GET_GPI_EN_REG(offset) _get_gpio_reg((offset), BD79112_REG_GPI_EN_A0_A7)
  142. #define GET_GPO_VAL_REG(offset) _get_gpio_reg((offset), BD79112_REG_GPO_VALUE_A0_A7)
  143. #define GET_GPI_VAL_REG(offset) _get_gpio_reg((offset), BD79112_REG_GPI_VALUE_A0_A7)
  144. static const struct regmap_range bd71815_volatile_ro_ranges[] = {
  145. /* Read ADC data */
  146. regmap_reg_range(BD79112_REG_AGIO0A, BD79112_REG_AGIO15B),
  147. /* GPI state */
  148. regmap_reg_range(BD79112_REG_GPI_VALUE_B8_15, BD79112_REG_GPI_VALUE_A0_A7),
  149. };
  150. static const struct regmap_access_table bd79112_volatile_regs = {
  151. .yes_ranges = &bd71815_volatile_ro_ranges[0],
  152. .n_yes_ranges = ARRAY_SIZE(bd71815_volatile_ro_ranges),
  153. };
  154. static const struct regmap_access_table bd79112_ro_regs = {
  155. .no_ranges = &bd71815_volatile_ro_ranges[0],
  156. .n_no_ranges = ARRAY_SIZE(bd71815_volatile_ro_ranges),
  157. };
  158. static const struct regmap_config bd79112_regmap = {
  159. .reg_read = bd79112_reg_read,
  160. .reg_write = bd79112_reg_write,
  161. .volatile_table = &bd79112_volatile_regs,
  162. .wr_table = &bd79112_ro_regs,
  163. .cache_type = REGCACHE_MAPLE,
  164. .max_register = BD79112_REG_MAX,
  165. };
  166. static int bd79112_read_raw(struct iio_dev *indio_dev,
  167. struct iio_chan_spec const *chan, int *val,
  168. int *val2, long m)
  169. {
  170. struct bd79112_data *data = iio_priv(indio_dev);
  171. int ret;
  172. switch (m) {
  173. case IIO_CHAN_INFO_RAW:
  174. ret = regmap_read(data->map, chan->channel, val);
  175. if (ret < 0)
  176. return ret;
  177. return IIO_VAL_INT;
  178. case IIO_CHAN_INFO_SCALE:
  179. *val = data->vref_mv;
  180. *val2 = 12;
  181. return IIO_VAL_FRACTIONAL_LOG2;
  182. default:
  183. return -EINVAL;
  184. }
  185. }
  186. static const struct iio_info bd79112_info = {
  187. .read_raw = bd79112_read_raw,
  188. };
  189. static const struct iio_chan_spec bd79112_chan_template = {
  190. .type = IIO_VOLTAGE,
  191. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  192. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
  193. .indexed = 1,
  194. };
  195. static int bd79112_gpio_init_valid_mask(struct gpio_chip *gc,
  196. unsigned long *valid_mask,
  197. unsigned int ngpios)
  198. {
  199. struct bd79112_data *data = gpiochip_get_data(gc);
  200. *valid_mask = data->gpio_valid_mask;
  201. return 0;
  202. }
  203. static int bd79112_gpio_dir_get(struct gpio_chip *gc, unsigned int offset)
  204. {
  205. struct bd79112_data *data = gpiochip_get_data(gc);
  206. unsigned int reg, bit, val;
  207. int ret;
  208. bit = GET_GPIO_BIT(offset);
  209. reg = GET_GPO_EN_REG(offset);
  210. ret = regmap_read(data->map, reg, &val);
  211. if (ret)
  212. return ret;
  213. if (bit & val)
  214. return GPIO_LINE_DIRECTION_OUT;
  215. reg = GET_GPI_EN_REG(offset);
  216. ret = regmap_read(data->map, reg, &val);
  217. if (ret)
  218. return ret;
  219. if (bit & val)
  220. return GPIO_LINE_DIRECTION_IN;
  221. /*
  222. * Ouch. Seems the pin is ADC input - shouldn't happen as changing mux
  223. * at runtime is not supported and non GPIO pins should be invalidated
  224. * by the valid_mask at probe. Maybe someone wrote a register bypassing
  225. * the driver?
  226. */
  227. dev_err(data->dev, "Pin not a GPIO\n");
  228. return -EINVAL;
  229. }
  230. static int bd79112_gpio_get(struct gpio_chip *gc, unsigned int offset)
  231. {
  232. struct bd79112_data *data = gpiochip_get_data(gc);
  233. unsigned int reg, bit, val;
  234. int ret;
  235. bit = GET_GPIO_BIT(offset);
  236. reg = GET_GPI_VAL_REG(offset);
  237. ret = regmap_read(data->map, reg, &val);
  238. if (ret)
  239. return ret;
  240. return !!(val & bit);
  241. }
  242. static int bd79112_gpio_set(struct gpio_chip *gc, unsigned int offset,
  243. int value)
  244. {
  245. struct bd79112_data *data = gpiochip_get_data(gc);
  246. unsigned int reg, bit;
  247. bit = GET_GPIO_BIT(offset);
  248. reg = GET_GPO_VAL_REG(offset);
  249. return regmap_assign_bits(data->map, reg, bit, value);
  250. }
  251. static int bd79112_gpio_set_multiple(struct gpio_chip *gc, unsigned long *mask,
  252. unsigned long *bits)
  253. {
  254. struct bd79112_data *data = gpiochip_get_data(gc);
  255. unsigned long i, bank_mask;
  256. for_each_set_clump8(i, bank_mask, mask, gc->ngpio) {
  257. unsigned long bank_bits;
  258. unsigned int reg;
  259. int ret;
  260. bank_bits = bitmap_get_value8(bits, i);
  261. reg = BD79112_REG_GPO_VALUE_A0_A7 - i / 8;
  262. ret = regmap_update_bits(data->map, reg, bank_mask, bank_bits);
  263. if (ret)
  264. return ret;
  265. }
  266. return 0;
  267. }
  268. static int bd79112_gpio_dir_set(struct bd79112_data *data, unsigned int offset,
  269. int dir)
  270. {
  271. unsigned int gpi_reg, gpo_reg, bit;
  272. int ret;
  273. bit = GET_GPIO_BIT(offset);
  274. gpi_reg = GET_GPI_EN_REG(offset);
  275. gpo_reg = GET_GPO_EN_REG(offset);
  276. if (dir == GPIO_LINE_DIRECTION_OUT) {
  277. ret = regmap_clear_bits(data->map, gpi_reg, bit);
  278. if (ret)
  279. return ret;
  280. return regmap_set_bits(data->map, gpo_reg, bit);
  281. }
  282. ret = regmap_set_bits(data->map, gpi_reg, bit);
  283. if (ret)
  284. return ret;
  285. return regmap_clear_bits(data->map, gpo_reg, bit);
  286. }
  287. static int bd79112_gpio_input(struct gpio_chip *gc, unsigned int offset)
  288. {
  289. struct bd79112_data *data = gpiochip_get_data(gc);
  290. return bd79112_gpio_dir_set(data, offset, GPIO_LINE_DIRECTION_IN);
  291. }
  292. static int bd79112_gpio_output(struct gpio_chip *gc, unsigned int offset,
  293. int value)
  294. {
  295. struct bd79112_data *data = gpiochip_get_data(gc);
  296. int ret;
  297. ret = bd79112_gpio_set(gc, offset, value);
  298. if (ret)
  299. return ret;
  300. return bd79112_gpio_dir_set(data, offset, GPIO_LINE_DIRECTION_OUT);
  301. }
  302. static const struct gpio_chip bd79112_gpio_chip = {
  303. .label = "bd79112-gpio",
  304. .get_direction = bd79112_gpio_dir_get,
  305. .direction_input = bd79112_gpio_input,
  306. .direction_output = bd79112_gpio_output,
  307. .get = bd79112_gpio_get,
  308. .set = bd79112_gpio_set,
  309. .set_multiple = bd79112_gpio_set_multiple,
  310. .init_valid_mask = bd79112_gpio_init_valid_mask,
  311. .can_sleep = true,
  312. .ngpio = 32,
  313. .base = -1,
  314. };
  315. static unsigned int bd79112_get_gpio_pins(const struct iio_chan_spec *cs, int num_channels)
  316. {
  317. unsigned int i, gpio_channels;
  318. /*
  319. * Let's initialize the mux config to say that all 32 channels are
  320. * GPIOs. Then we can just loop through the iio_chan_spec and clear the
  321. * bits for found ADC channels.
  322. */
  323. gpio_channels = GENMASK(31, 0);
  324. for (i = 0; i < num_channels; i++)
  325. gpio_channels &= ~BIT(cs[i].channel);
  326. return gpio_channels;
  327. }
  328. /* ADC channels as named in the data-sheet */
  329. static const char * const bd79112_chan_names[] = {
  330. "AGIO0A", "AGIO1A", "AGIO2A", "AGIO3A", /* 0 - 3 */
  331. "AGIO4A", "AGIO5A", "AGIO6A", "AGIO7A", /* 4 - 7 */
  332. "AGIO8A", "AGIO9A", "AGIO10A", "AGIO11A", /* 8 - 11 */
  333. "AGIO12A", "AGIO13A", "AGIO14A", "AGIO15A", /* 12 - 15 */
  334. "AGIO0B", "AGIO1B", "AGIO2B", "AGIO3B", /* 16 - 19 */
  335. "AGIO4B", "AGIO5B", "AGIO6B", "AGIO7B", /* 20 - 23 */
  336. "AGIO8B", "AGIO9B", "AGIO10B", "AGIO11B", /* 24 - 27 */
  337. "AGIO12B", "AGIO13B", "AGIO14B", "AGIO15B", /* 28 - 31 */
  338. };
  339. static int bd79112_probe(struct spi_device *spi)
  340. {
  341. struct bd79112_data *data;
  342. struct iio_dev *iio_dev;
  343. struct iio_chan_spec *cs;
  344. struct device *dev = &spi->dev;
  345. unsigned long gpio_pins, pin;
  346. unsigned int i;
  347. int ret;
  348. iio_dev = devm_iio_device_alloc(dev, sizeof(*data));
  349. if (!iio_dev)
  350. return -ENOMEM;
  351. data = iio_priv(iio_dev);
  352. data->spi = spi;
  353. data->dev = dev;
  354. data->map = devm_regmap_init(dev, NULL, data, &bd79112_regmap);
  355. if (IS_ERR(data->map))
  356. return dev_err_probe(dev, PTR_ERR(data->map),
  357. "Failed to initialize Regmap\n");
  358. ret = devm_regulator_get_enable_read_voltage(dev, "vdd");
  359. if (ret < 0)
  360. return dev_err_probe(dev, ret, "Failed to get the Vdd\n");
  361. data->vref_mv = ret / 1000;
  362. ret = devm_regulator_get_enable(dev, "iovdd");
  363. if (ret < 0)
  364. return dev_err_probe(dev, ret, "Failed to enable I/O voltage\n");
  365. data->read_xfer[0].tx_buf = &data->read_tx[0];
  366. data->read_xfer[0].len = sizeof(data->read_tx);
  367. data->read_xfer[0].cs_change = 1;
  368. data->read_xfer[1].rx_buf = &data->read_rx;
  369. data->read_xfer[1].len = sizeof(data->read_rx);
  370. spi_message_init_with_transfers(&data->read_msg, data->read_xfer, 2);
  371. ret = devm_spi_optimize_message(dev, spi, &data->read_msg);
  372. if (ret < 0)
  373. return dev_err_probe(dev, ret,
  374. "Failed to optimize SPI read message\n");
  375. data->write_xfer.tx_buf = &data->reg_write_tx[0];
  376. data->write_xfer.len = sizeof(data->reg_write_tx);
  377. spi_message_init_with_transfers(&data->write_msg, &data->write_xfer, 1);
  378. ret = devm_spi_optimize_message(dev, spi, &data->write_msg);
  379. if (ret < 0)
  380. return dev_err_probe(dev, ret,
  381. "Failed to optimize SPI write message\n");
  382. ret = devm_iio_adc_device_alloc_chaninfo_se(dev, &bd79112_chan_template,
  383. BD79112_MAX_NUM_CHANNELS - 1,
  384. &cs);
  385. /* Register all pins as GPIOs if there are no ADC channels */
  386. if (ret == -ENOENT)
  387. goto register_gpios;
  388. if (ret < 0)
  389. return ret;
  390. iio_dev->num_channels = ret;
  391. iio_dev->channels = cs;
  392. for (i = 0; i < iio_dev->num_channels; i++)
  393. cs[i].datasheet_name = bd79112_chan_names[cs[i].channel];
  394. iio_dev->info = &bd79112_info;
  395. iio_dev->name = "bd79112";
  396. iio_dev->modes = INDIO_DIRECT_MODE;
  397. /*
  398. * Ensure all channels are ADCs. This allows us to register the IIO
  399. * device early (before checking which pins are to be used for GPIO)
  400. * without having to worry about some pins being initially used for
  401. * GPIO.
  402. */
  403. for (i = 0; i < BD79112_NUM_GPIO_EN_REGS; i++) {
  404. ret = regmap_write(data->map, BD79112_FIRST_GPIO_EN_REG + i, 0);
  405. if (ret)
  406. return dev_err_probe(dev, ret,
  407. "Failed to initialize channels\n");
  408. }
  409. ret = devm_iio_device_register(data->dev, iio_dev);
  410. if (ret)
  411. return dev_err_probe(data->dev, ret, "Failed to register ADC\n");
  412. register_gpios:
  413. gpio_pins = bd79112_get_gpio_pins(iio_dev->channels,
  414. iio_dev->num_channels);
  415. /* If all channels are reserved for ADC, then we're done. */
  416. if (!gpio_pins)
  417. return 0;
  418. /* Default all the GPIO pins to GPI */
  419. for_each_set_bit(pin, &gpio_pins, BD79112_MAX_NUM_CHANNELS) {
  420. ret = bd79112_gpio_dir_set(data, pin, GPIO_LINE_DIRECTION_IN);
  421. if (ret)
  422. return dev_err_probe(dev, ret,
  423. "Failed to mark pin as GPI\n");
  424. }
  425. data->gpio_valid_mask = gpio_pins;
  426. data->gc = bd79112_gpio_chip;
  427. data->gc.parent = dev;
  428. return devm_gpiochip_add_data(dev, &data->gc, data);
  429. }
  430. static const struct of_device_id bd79112_of_match[] = {
  431. { .compatible = "rohm,bd79112" },
  432. { }
  433. };
  434. MODULE_DEVICE_TABLE(of, bd79112_of_match);
  435. static const struct spi_device_id bd79112_id[] = {
  436. { "bd79112" },
  437. { }
  438. };
  439. MODULE_DEVICE_TABLE(spi, bd79112_id);
  440. static struct spi_driver bd79112_driver = {
  441. .driver = {
  442. .name = "bd79112",
  443. .of_match_table = bd79112_of_match,
  444. },
  445. .probe = bd79112_probe,
  446. .id_table = bd79112_id,
  447. };
  448. module_spi_driver(bd79112_driver);
  449. MODULE_AUTHOR("Matti Vaittinen <mazziesaccount@gmail.com>");
  450. MODULE_DESCRIPTION("Driver for ROHM BD79112 ADC/GPIO");
  451. MODULE_LICENSE("GPL");
  452. MODULE_IMPORT_NS("IIO_DRIVER");