max22007.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * max22007.c - MAX22007 DAC driver
  4. *
  5. * Driver for Analog Devices MAX22007 Digital to Analog Converter.
  6. *
  7. * Copyright (c) 2026 Analog Devices Inc.
  8. */
  9. #include <linux/bitfield.h>
  10. #include <linux/bits.h>
  11. #include <linux/crc8.h>
  12. #include <linux/delay.h>
  13. #include <linux/dev_printk.h>
  14. #include <linux/device/devres.h>
  15. #include <linux/err.h>
  16. #include <linux/errno.h>
  17. #include <linux/gpio/consumer.h>
  18. #include <linux/iio/iio.h>
  19. #include <linux/kstrtox.h>
  20. #include <linux/minmax.h>
  21. #include <linux/mod_devicetable.h>
  22. #include <linux/module.h>
  23. #include <linux/property.h>
  24. #include <linux/regmap.h>
  25. #include <linux/regulator/consumer.h>
  26. #include <linux/slab.h>
  27. #include <linux/spi/spi.h>
  28. #include <linux/string.h>
  29. #include <linux/sysfs.h>
  30. #include <linux/types.h>
  31. #include <dt-bindings/iio/addac/adi,ad74413r.h>
  32. struct device;
  33. #define MAX22007_NUM_CHANNELS 4
  34. #define MAX22007_REV_ID_REG 0x00
  35. #define MAX22007_STAT_INTR_REG 0x01
  36. #define MAX22007_INTERRUPT_EN_REG 0x02
  37. #define MAX22007_CONFIG_REG 0x03
  38. #define MAX22007_CONTROL_REG 0x04
  39. #define MAX22007_CHANNEL_MODE_REG 0x05
  40. #define MAX22007_SOFT_RESET_REG 0x06
  41. #define MAX22007_DAC_CHANNEL_REG(ch) (0x07 + (ch))
  42. #define MAX22007_GPIO_CTRL_REG 0x0B
  43. #define MAX22007_GPIO_DATA_REG 0x0C
  44. #define MAX22007_GPI_EDGE_INT_CTRL_REG 0x0D
  45. #define MAX22007_GPI_INT_STATUS_REG 0x0E
  46. /* Channel mask definitions */
  47. #define MAX22007_CH_MODE_CH_MASK(ch) BIT(12 + (ch))
  48. #define MAX22007_CH_PWRON_CH_MASK(ch) BIT(8 + (ch))
  49. #define MAX22007_DAC_LATCH_MODE_MASK(ch) BIT(12 + (ch))
  50. #define MAX22007_LDAC_UPDATE_MASK(ch) BIT(12 + (ch))
  51. #define MAX22007_SW_RST_MASK BIT(8)
  52. #define MAX22007_SW_CLR_MASK BIT(12)
  53. #define MAX22007_SOFT_RESET_BITS_MASK (MAX22007_SW_RST_MASK | \
  54. MAX22007_SW_CLR_MASK)
  55. #define MAX22007_DAC_DATA_MASK GENMASK(15, 4)
  56. #define MAX22007_DAC_MAX_RAW GENMASK(11, 0)
  57. #define MAX22007_CRC8_POLYNOMIAL 0x8C
  58. #define MAX22007_CRC_EN_MASK BIT(0)
  59. #define MAX22007_RW_MASK BIT(0)
  60. #define MAX22007_CRC_OVERHEAD 1
  61. #define MAX22007_NUM_SUPPLIES 3
  62. #define MAX22007_REF_MV 2500
  63. /* Field value preparation macros with masking */
  64. #define MAX22007_CH_PWR_VAL(ch, val) (((val) & 0x1) << (8 + (ch)))
  65. #define MAX22007_CH_MODE_VAL(ch, val) (((val) & 0x1) << (12 + (ch)))
  66. #define MAX22007_DAC_LATCH_MODE_VAL(ch, val) (((val) & 0x1) << (12 + (ch)))
  67. static u8 max22007_crc8_table[CRC8_TABLE_SIZE];
  68. static const char * const max22007_supply_names[MAX22007_NUM_SUPPLIES] = {
  69. "vdd",
  70. "hvdd",
  71. "hvss",
  72. };
  73. struct max22007_state {
  74. struct spi_device *spi;
  75. struct regmap *regmap;
  76. struct iio_chan_spec *iio_chans;
  77. u8 tx_buf[4] __aligned(IIO_DMA_MINALIGN);
  78. u8 rx_buf[4];
  79. };
  80. static int max22007_spi_read(void *context, const void *reg, size_t reg_size,
  81. void *val, size_t val_size)
  82. {
  83. struct max22007_state *st = context;
  84. u8 calculated_crc, received_crc;
  85. u8 rx_buf[4];
  86. u8 reg_byte;
  87. int ret;
  88. if (reg_size != 1)
  89. return -EINVAL;
  90. if (val_size == 0 || val_size > 3)
  91. return -EINVAL;
  92. memcpy(&reg_byte, reg, 1);
  93. ret = spi_write_then_read(st->spi, &reg_byte, 1, rx_buf,
  94. val_size + MAX22007_CRC_OVERHEAD);
  95. if (ret) {
  96. dev_err(&st->spi->dev, "SPI transfer failed: %d\n", ret);
  97. return ret;
  98. }
  99. calculated_crc = crc8(max22007_crc8_table, &reg_byte, 1, 0x00);
  100. calculated_crc = crc8(max22007_crc8_table, rx_buf, 2, calculated_crc);
  101. received_crc = rx_buf[val_size];
  102. if (calculated_crc != received_crc) {
  103. dev_err(&st->spi->dev, "CRC mismatch on read register %02x\n", reg_byte);
  104. return -EIO;
  105. }
  106. memcpy(val, rx_buf, val_size);
  107. return 0;
  108. }
  109. static int max22007_spi_write(void *context, const void *data, size_t count)
  110. {
  111. struct max22007_state *st = context;
  112. struct spi_transfer xfer = {
  113. .tx_buf = st->tx_buf,
  114. .rx_buf = st->rx_buf,
  115. };
  116. if (count + MAX22007_CRC_OVERHEAD > sizeof(st->tx_buf))
  117. return -EINVAL;
  118. memset(st->tx_buf, 0, sizeof(st->tx_buf));
  119. xfer.len = count + MAX22007_CRC_OVERHEAD;
  120. memcpy(st->tx_buf, data, count);
  121. st->tx_buf[count] = crc8(max22007_crc8_table, st->tx_buf,
  122. sizeof(st->tx_buf) - 1, 0x00);
  123. return spi_sync_transfer(st->spi, &xfer, 1);
  124. }
  125. static bool max22007_reg_readable(struct device *dev, unsigned int reg)
  126. {
  127. switch (reg) {
  128. case MAX22007_REV_ID_REG:
  129. case MAX22007_STAT_INTR_REG:
  130. case MAX22007_CONFIG_REG:
  131. case MAX22007_CONTROL_REG:
  132. case MAX22007_CHANNEL_MODE_REG:
  133. case MAX22007_SOFT_RESET_REG:
  134. case MAX22007_GPIO_CTRL_REG:
  135. case MAX22007_GPIO_DATA_REG:
  136. case MAX22007_GPI_EDGE_INT_CTRL_REG:
  137. case MAX22007_GPI_INT_STATUS_REG:
  138. return true;
  139. case MAX22007_DAC_CHANNEL_REG(0) ... MAX22007_DAC_CHANNEL_REG(MAX22007_NUM_CHANNELS - 1):
  140. return true;
  141. default:
  142. return false;
  143. }
  144. }
  145. static bool max22007_reg_writable(struct device *dev, unsigned int reg)
  146. {
  147. switch (reg) {
  148. case MAX22007_CONFIG_REG:
  149. case MAX22007_CONTROL_REG:
  150. case MAX22007_CHANNEL_MODE_REG:
  151. case MAX22007_SOFT_RESET_REG:
  152. case MAX22007_GPIO_CTRL_REG:
  153. case MAX22007_GPIO_DATA_REG:
  154. case MAX22007_GPI_EDGE_INT_CTRL_REG:
  155. return true;
  156. case MAX22007_DAC_CHANNEL_REG(0) ... MAX22007_DAC_CHANNEL_REG(MAX22007_NUM_CHANNELS - 1):
  157. return true;
  158. default:
  159. return false;
  160. }
  161. }
  162. static const struct regmap_bus max22007_regmap_bus = {
  163. .read = max22007_spi_read,
  164. .write = max22007_spi_write,
  165. .read_flag_mask = MAX22007_RW_MASK,
  166. .reg_format_endian_default = REGMAP_ENDIAN_BIG,
  167. .val_format_endian_default = REGMAP_ENDIAN_BIG,
  168. };
  169. static const struct regmap_config max22007_regmap_config = {
  170. .reg_bits = 8,
  171. .val_bits = 16,
  172. .reg_shift = -1,
  173. .readable_reg = max22007_reg_readable,
  174. .writeable_reg = max22007_reg_writable,
  175. .max_register = 0x0E,
  176. };
  177. static int max22007_write_channel_data(struct max22007_state *st,
  178. unsigned int channel, int data)
  179. {
  180. unsigned int reg_val;
  181. if (data < 0 || data > MAX22007_DAC_MAX_RAW)
  182. return -EINVAL;
  183. reg_val = FIELD_PREP(MAX22007_DAC_DATA_MASK, data);
  184. return regmap_write(st->regmap, MAX22007_DAC_CHANNEL_REG(channel), reg_val);
  185. }
  186. static int max22007_read_channel_data(struct max22007_state *st,
  187. unsigned int channel, int *data)
  188. {
  189. unsigned int reg_val;
  190. int ret;
  191. ret = regmap_read(st->regmap, MAX22007_DAC_CHANNEL_REG(channel), &reg_val);
  192. if (ret)
  193. return ret;
  194. *data = FIELD_GET(MAX22007_DAC_DATA_MASK, reg_val);
  195. return 0;
  196. }
  197. static int max22007_read_raw(struct iio_dev *indio_dev,
  198. struct iio_chan_spec const *chan,
  199. int *val, int *val2, long mask)
  200. {
  201. struct max22007_state *st = iio_priv(indio_dev);
  202. int ret;
  203. switch (mask) {
  204. case IIO_CHAN_INFO_RAW:
  205. ret = max22007_read_channel_data(st, chan->channel, val);
  206. if (ret)
  207. return ret;
  208. return IIO_VAL_INT;
  209. case IIO_CHAN_INFO_SCALE:
  210. if (chan->type == IIO_VOLTAGE)
  211. *val = 5 * MAX22007_REF_MV; /* 5 * Vref in mV */
  212. else
  213. *val = 25; /* Vref / (2 * Rsense) = MAX22007_REF_MV / 100 */
  214. *val2 = 12; /* 12-bit DAC resolution */
  215. return IIO_VAL_FRACTIONAL_LOG2;
  216. default:
  217. return -EINVAL;
  218. }
  219. }
  220. static int max22007_write_raw(struct iio_dev *indio_dev,
  221. struct iio_chan_spec const *chan,
  222. int val, int val2, long mask)
  223. {
  224. struct max22007_state *st = iio_priv(indio_dev);
  225. switch (mask) {
  226. case IIO_CHAN_INFO_RAW:
  227. return max22007_write_channel_data(st, chan->channel, val);
  228. default:
  229. return -EINVAL;
  230. }
  231. }
  232. static const struct iio_info max22007_info = {
  233. .read_raw = max22007_read_raw,
  234. .write_raw = max22007_write_raw,
  235. };
  236. static ssize_t max22007_read_dac_powerdown(struct iio_dev *indio_dev,
  237. uintptr_t private,
  238. const struct iio_chan_spec *chan,
  239. char *buf)
  240. {
  241. struct max22007_state *st = iio_priv(indio_dev);
  242. unsigned int reg_val;
  243. bool powerdown;
  244. int ret;
  245. ret = regmap_read(st->regmap, MAX22007_CHANNEL_MODE_REG, &reg_val);
  246. if (ret)
  247. return ret;
  248. powerdown = !(reg_val & MAX22007_CH_PWRON_CH_MASK(chan->channel));
  249. return sysfs_emit(buf, "%d\n", powerdown);
  250. }
  251. static ssize_t max22007_write_dac_powerdown(struct iio_dev *indio_dev,
  252. uintptr_t private,
  253. const struct iio_chan_spec *chan,
  254. const char *buf, size_t len)
  255. {
  256. struct max22007_state *st = iio_priv(indio_dev);
  257. bool powerdown;
  258. int ret;
  259. ret = kstrtobool(buf, &powerdown);
  260. if (ret)
  261. return ret;
  262. ret = regmap_update_bits(st->regmap, MAX22007_CHANNEL_MODE_REG,
  263. MAX22007_CH_PWRON_CH_MASK(chan->channel),
  264. MAX22007_CH_PWR_VAL(chan->channel, powerdown ? 0 : 1));
  265. if (ret)
  266. return ret;
  267. return len;
  268. }
  269. static const struct iio_chan_spec_ext_info max22007_ext_info[] = {
  270. {
  271. .name = "powerdown",
  272. .read = max22007_read_dac_powerdown,
  273. .write = max22007_write_dac_powerdown,
  274. .shared = IIO_SEPARATE,
  275. },
  276. { }
  277. };
  278. static int max22007_parse_channel_cfg(struct max22007_state *st, u8 *num_channels)
  279. {
  280. struct device *dev = &st->spi->dev;
  281. int ret, num_chan;
  282. int i = 0;
  283. u32 reg;
  284. num_chan = device_get_child_node_count(dev);
  285. if (!num_chan)
  286. return dev_err_probe(dev, -ENODEV, "no channels configured\n");
  287. st->iio_chans = devm_kcalloc(dev, num_chan, sizeof(*st->iio_chans), GFP_KERNEL);
  288. if (!st->iio_chans)
  289. return -ENOMEM;
  290. device_for_each_child_node_scoped(dev, child) {
  291. u32 ch_func;
  292. enum iio_chan_type chan_type;
  293. ret = fwnode_property_read_u32(child, "reg", &reg);
  294. if (ret)
  295. return dev_err_probe(dev, ret,
  296. "failed to read reg property of %pfwP\n", child);
  297. if (reg >= MAX22007_NUM_CHANNELS)
  298. return dev_err_probe(dev, -EINVAL,
  299. "reg out of range in %pfwP\n", child);
  300. ret = fwnode_property_read_u32(child, "adi,ch-func", &ch_func);
  301. if (ret)
  302. return dev_err_probe(dev, ret,
  303. "missing adi,ch-func property for %pfwP\n", child);
  304. switch (ch_func) {
  305. case CH_FUNC_VOLTAGE_OUTPUT:
  306. chan_type = IIO_VOLTAGE;
  307. break;
  308. case CH_FUNC_CURRENT_OUTPUT:
  309. chan_type = IIO_CURRENT;
  310. break;
  311. default:
  312. return dev_err_probe(dev, -EINVAL,
  313. "invalid adi,ch-func %u for %pfwP\n",
  314. ch_func, child);
  315. }
  316. st->iio_chans[i++] = (struct iio_chan_spec) {
  317. .output = 1,
  318. .indexed = 1,
  319. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  320. BIT(IIO_CHAN_INFO_SCALE),
  321. .ext_info = max22007_ext_info,
  322. .channel = reg,
  323. .type = chan_type,
  324. };
  325. ret = regmap_update_bits(st->regmap, MAX22007_CHANNEL_MODE_REG,
  326. MAX22007_CH_MODE_CH_MASK(reg),
  327. MAX22007_CH_MODE_VAL(reg, ch_func - 1));
  328. if (ret)
  329. return ret;
  330. /* Set DAC to transparent mode (immediate update) */
  331. ret = regmap_update_bits(st->regmap, MAX22007_CONFIG_REG,
  332. MAX22007_DAC_LATCH_MODE_MASK(reg),
  333. MAX22007_DAC_LATCH_MODE_VAL(reg, 1));
  334. if (ret)
  335. return ret;
  336. }
  337. *num_channels = num_chan;
  338. return 0;
  339. }
  340. static int max22007_probe(struct spi_device *spi)
  341. {
  342. struct device *dev = &spi->dev;
  343. struct gpio_desc *reset_gpio;
  344. struct max22007_state *st;
  345. struct iio_dev *indio_dev;
  346. u8 num_channels;
  347. int ret;
  348. indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
  349. if (!indio_dev)
  350. return -ENOMEM;
  351. st = iio_priv(indio_dev);
  352. st->spi = spi;
  353. crc8_populate_lsb(max22007_crc8_table, MAX22007_CRC8_POLYNOMIAL);
  354. st->regmap = devm_regmap_init(dev, &max22007_regmap_bus, st,
  355. &max22007_regmap_config);
  356. if (IS_ERR(st->regmap))
  357. return dev_err_probe(dev, PTR_ERR(st->regmap),
  358. "Failed to initialize regmap\n");
  359. ret = devm_regulator_bulk_get_enable(dev, MAX22007_NUM_SUPPLIES,
  360. max22007_supply_names);
  361. if (ret)
  362. return dev_err_probe(dev, ret, "Failed to get and enable regulators\n");
  363. reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
  364. if (IS_ERR(reset_gpio))
  365. return dev_err_probe(dev, PTR_ERR(reset_gpio),
  366. "Failed to get reset GPIO\n");
  367. if (reset_gpio) {
  368. gpiod_set_value_cansleep(reset_gpio, 1);
  369. usleep_range(1000, 5000);
  370. gpiod_set_value_cansleep(reset_gpio, 0);
  371. usleep_range(1000, 5000);
  372. } else {
  373. ret = regmap_write(st->regmap, MAX22007_SOFT_RESET_REG,
  374. MAX22007_SOFT_RESET_BITS_MASK);
  375. if (ret)
  376. return ret;
  377. }
  378. ret = regmap_set_bits(st->regmap, MAX22007_CONFIG_REG,
  379. MAX22007_CRC_EN_MASK);
  380. if (ret)
  381. return ret;
  382. ret = max22007_parse_channel_cfg(st, &num_channels);
  383. if (ret)
  384. return ret;
  385. indio_dev->info = &max22007_info;
  386. indio_dev->modes = INDIO_DIRECT_MODE;
  387. indio_dev->channels = st->iio_chans;
  388. indio_dev->num_channels = num_channels;
  389. indio_dev->name = "max22007";
  390. return devm_iio_device_register(dev, indio_dev);
  391. }
  392. static const struct spi_device_id max22007_id[] = {
  393. { "max22007" },
  394. { }
  395. };
  396. MODULE_DEVICE_TABLE(spi, max22007_id);
  397. static const struct of_device_id max22007_of_match[] = {
  398. { .compatible = "adi,max22007" },
  399. { }
  400. };
  401. MODULE_DEVICE_TABLE(of, max22007_of_match);
  402. static struct spi_driver max22007_driver = {
  403. .driver = {
  404. .name = "max22007",
  405. .of_match_table = max22007_of_match,
  406. },
  407. .probe = max22007_probe,
  408. .id_table = max22007_id,
  409. };
  410. module_spi_driver(max22007_driver);
  411. MODULE_AUTHOR("Janani Sunil <janani.sunil@analog.com>");
  412. MODULE_DESCRIPTION("Analog Devices MAX22007 DAC");
  413. MODULE_LICENSE("GPL");