ad5064.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * AD5024, AD5025, AD5044, AD5045, AD5064, AD5064-1, AD5065, AD5625, AD5625R,
  4. * AD5627, AD5627R, AD5628, AD5629R, AD5645R, AD5647R, AD5648, AD5665, AD5665R,
  5. * AD5666, AD5667, AD5667R, AD5668, AD5669R, LTC2606, LTC2607, LTC2609, LTC2616,
  6. * LTC2617, LTC2619, LTC2626, LTC2627, LTC2629, LTC2631, LTC2633, LTC2635
  7. * Digital to analog converters driver
  8. *
  9. * Copyright 2011 Analog Devices Inc.
  10. */
  11. #include <linux/device.h>
  12. #include <linux/err.h>
  13. #include <linux/module.h>
  14. #include <linux/kernel.h>
  15. #include <linux/spi/spi.h>
  16. #include <linux/i2c.h>
  17. #include <linux/slab.h>
  18. #include <linux/sysfs.h>
  19. #include <linux/regulator/consumer.h>
  20. #include <asm/unaligned.h>
  21. #include <linux/iio/iio.h>
  22. #include <linux/iio/sysfs.h>
  23. #define AD5064_MAX_DAC_CHANNELS 8
  24. #define AD5064_MAX_VREFS 4
  25. #define AD5064_ADDR(x) ((x) << 20)
  26. #define AD5064_CMD(x) ((x) << 24)
  27. #define AD5064_ADDR_ALL_DAC 0xF
  28. #define AD5064_CMD_WRITE_INPUT_N 0x0
  29. #define AD5064_CMD_UPDATE_DAC_N 0x1
  30. #define AD5064_CMD_WRITE_INPUT_N_UPDATE_ALL 0x2
  31. #define AD5064_CMD_WRITE_INPUT_N_UPDATE_N 0x3
  32. #define AD5064_CMD_POWERDOWN_DAC 0x4
  33. #define AD5064_CMD_CLEAR 0x5
  34. #define AD5064_CMD_LDAC_MASK 0x6
  35. #define AD5064_CMD_RESET 0x7
  36. #define AD5064_CMD_CONFIG 0x8
  37. #define AD5064_CMD_RESET_V2 0x5
  38. #define AD5064_CMD_CONFIG_V2 0x7
  39. #define AD5064_CONFIG_DAISY_CHAIN_ENABLE BIT(1)
  40. #define AD5064_CONFIG_INT_VREF_ENABLE BIT(0)
  41. #define AD5064_LDAC_PWRDN_NONE 0x0
  42. #define AD5064_LDAC_PWRDN_1K 0x1
  43. #define AD5064_LDAC_PWRDN_100K 0x2
  44. #define AD5064_LDAC_PWRDN_3STATE 0x3
  45. /**
  46. * enum ad5064_regmap_type - Register layout variant
  47. * @AD5064_REGMAP_ADI: Old Analog Devices register map layout
  48. * @AD5064_REGMAP_ADI2: New Analog Devices register map layout
  49. * @AD5064_REGMAP_LTC: LTC register map layout
  50. */
  51. enum ad5064_regmap_type {
  52. AD5064_REGMAP_ADI,
  53. AD5064_REGMAP_ADI2,
  54. AD5064_REGMAP_LTC,
  55. };
  56. /**
  57. * struct ad5064_chip_info - chip specific information
  58. * @shared_vref: whether the vref supply is shared between channels
  59. * @internal_vref: internal reference voltage. 0 if the chip has no
  60. * internal vref.
  61. * @channels: channel specification
  62. * @num_channels: number of channels
  63. * @regmap_type: register map layout variant
  64. */
  65. struct ad5064_chip_info {
  66. bool shared_vref;
  67. unsigned long internal_vref;
  68. const struct iio_chan_spec *channels;
  69. unsigned int num_channels;
  70. enum ad5064_regmap_type regmap_type;
  71. };
  72. struct ad5064_state;
  73. typedef int (*ad5064_write_func)(struct ad5064_state *st, unsigned int cmd,
  74. unsigned int addr, unsigned int val);
  75. /**
  76. * struct ad5064_state - driver instance specific data
  77. * @dev: the device for this driver instance
  78. * @chip_info: chip model specific constants, available modes etc
  79. * @vref_reg: vref supply regulators
  80. * @pwr_down: whether channel is powered down
  81. * @pwr_down_mode: channel's current power down mode
  82. * @dac_cache: current DAC raw value (chip does not support readback)
  83. * @use_internal_vref: set to true if the internal reference voltage should be
  84. * used.
  85. * @write: register write callback
  86. * @lock: maintain consistency between cached and dev state
  87. * @data: i2c/spi transfer buffers
  88. */
  89. struct ad5064_state {
  90. struct device *dev;
  91. const struct ad5064_chip_info *chip_info;
  92. struct regulator_bulk_data vref_reg[AD5064_MAX_VREFS];
  93. bool pwr_down[AD5064_MAX_DAC_CHANNELS];
  94. u8 pwr_down_mode[AD5064_MAX_DAC_CHANNELS];
  95. unsigned int dac_cache[AD5064_MAX_DAC_CHANNELS];
  96. bool use_internal_vref;
  97. ad5064_write_func write;
  98. struct mutex lock;
  99. /*
  100. * DMA (thus cache coherency maintenance) may require the
  101. * transfer buffers to live in their own cache lines.
  102. */
  103. union {
  104. u8 i2c[3];
  105. __be32 spi;
  106. } data __aligned(IIO_DMA_MINALIGN);
  107. };
  108. enum ad5064_type {
  109. ID_AD5024,
  110. ID_AD5025,
  111. ID_AD5044,
  112. ID_AD5045,
  113. ID_AD5064,
  114. ID_AD5064_1,
  115. ID_AD5065,
  116. ID_AD5625,
  117. ID_AD5625R_1V25,
  118. ID_AD5625R_2V5,
  119. ID_AD5627,
  120. ID_AD5627R_1V25,
  121. ID_AD5627R_2V5,
  122. ID_AD5628_1,
  123. ID_AD5628_2,
  124. ID_AD5629_1,
  125. ID_AD5629_2,
  126. ID_AD5645R_1V25,
  127. ID_AD5645R_2V5,
  128. ID_AD5647R_1V25,
  129. ID_AD5647R_2V5,
  130. ID_AD5648_1,
  131. ID_AD5648_2,
  132. ID_AD5665,
  133. ID_AD5665R_1V25,
  134. ID_AD5665R_2V5,
  135. ID_AD5666_1,
  136. ID_AD5666_2,
  137. ID_AD5667,
  138. ID_AD5667R_1V25,
  139. ID_AD5667R_2V5,
  140. ID_AD5668_1,
  141. ID_AD5668_2,
  142. ID_AD5669_1,
  143. ID_AD5669_2,
  144. ID_LTC2606,
  145. ID_LTC2607,
  146. ID_LTC2609,
  147. ID_LTC2616,
  148. ID_LTC2617,
  149. ID_LTC2619,
  150. ID_LTC2626,
  151. ID_LTC2627,
  152. ID_LTC2629,
  153. ID_LTC2631_L12,
  154. ID_LTC2631_H12,
  155. ID_LTC2631_L10,
  156. ID_LTC2631_H10,
  157. ID_LTC2631_L8,
  158. ID_LTC2631_H8,
  159. ID_LTC2633_L12,
  160. ID_LTC2633_H12,
  161. ID_LTC2633_L10,
  162. ID_LTC2633_H10,
  163. ID_LTC2633_L8,
  164. ID_LTC2633_H8,
  165. ID_LTC2635_L12,
  166. ID_LTC2635_H12,
  167. ID_LTC2635_L10,
  168. ID_LTC2635_H10,
  169. ID_LTC2635_L8,
  170. ID_LTC2635_H8,
  171. };
  172. static int ad5064_write(struct ad5064_state *st, unsigned int cmd,
  173. unsigned int addr, unsigned int val, unsigned int shift)
  174. {
  175. val <<= shift;
  176. return st->write(st, cmd, addr, val);
  177. }
  178. static int ad5064_sync_powerdown_mode(struct ad5064_state *st,
  179. const struct iio_chan_spec *chan)
  180. {
  181. unsigned int val, address;
  182. unsigned int shift;
  183. int ret;
  184. if (st->chip_info->regmap_type == AD5064_REGMAP_LTC) {
  185. val = 0;
  186. address = chan->address;
  187. } else {
  188. if (st->chip_info->regmap_type == AD5064_REGMAP_ADI2)
  189. shift = 4;
  190. else
  191. shift = 8;
  192. val = (0x1 << chan->address);
  193. address = 0;
  194. if (st->pwr_down[chan->channel])
  195. val |= st->pwr_down_mode[chan->channel] << shift;
  196. }
  197. ret = ad5064_write(st, AD5064_CMD_POWERDOWN_DAC, address, val, 0);
  198. return ret;
  199. }
  200. static const char * const ad5064_powerdown_modes[] = {
  201. "1kohm_to_gnd",
  202. "100kohm_to_gnd",
  203. "three_state",
  204. };
  205. static const char * const ltc2617_powerdown_modes[] = {
  206. "90kohm_to_gnd",
  207. };
  208. static int ad5064_get_powerdown_mode(struct iio_dev *indio_dev,
  209. const struct iio_chan_spec *chan)
  210. {
  211. struct ad5064_state *st = iio_priv(indio_dev);
  212. return st->pwr_down_mode[chan->channel] - 1;
  213. }
  214. static int ad5064_set_powerdown_mode(struct iio_dev *indio_dev,
  215. const struct iio_chan_spec *chan, unsigned int mode)
  216. {
  217. struct ad5064_state *st = iio_priv(indio_dev);
  218. int ret;
  219. mutex_lock(&st->lock);
  220. st->pwr_down_mode[chan->channel] = mode + 1;
  221. ret = ad5064_sync_powerdown_mode(st, chan);
  222. mutex_unlock(&st->lock);
  223. return ret;
  224. }
  225. static const struct iio_enum ad5064_powerdown_mode_enum = {
  226. .items = ad5064_powerdown_modes,
  227. .num_items = ARRAY_SIZE(ad5064_powerdown_modes),
  228. .get = ad5064_get_powerdown_mode,
  229. .set = ad5064_set_powerdown_mode,
  230. };
  231. static const struct iio_enum ltc2617_powerdown_mode_enum = {
  232. .items = ltc2617_powerdown_modes,
  233. .num_items = ARRAY_SIZE(ltc2617_powerdown_modes),
  234. .get = ad5064_get_powerdown_mode,
  235. .set = ad5064_set_powerdown_mode,
  236. };
  237. static ssize_t ad5064_read_dac_powerdown(struct iio_dev *indio_dev,
  238. uintptr_t private, const struct iio_chan_spec *chan, char *buf)
  239. {
  240. struct ad5064_state *st = iio_priv(indio_dev);
  241. return sysfs_emit(buf, "%d\n", st->pwr_down[chan->channel]);
  242. }
  243. static ssize_t ad5064_write_dac_powerdown(struct iio_dev *indio_dev,
  244. uintptr_t private, const struct iio_chan_spec *chan, const char *buf,
  245. size_t len)
  246. {
  247. struct ad5064_state *st = iio_priv(indio_dev);
  248. bool pwr_down;
  249. int ret;
  250. ret = kstrtobool(buf, &pwr_down);
  251. if (ret)
  252. return ret;
  253. mutex_lock(&st->lock);
  254. st->pwr_down[chan->channel] = pwr_down;
  255. ret = ad5064_sync_powerdown_mode(st, chan);
  256. mutex_unlock(&st->lock);
  257. return ret ? ret : len;
  258. }
  259. static int ad5064_get_vref(struct ad5064_state *st,
  260. struct iio_chan_spec const *chan)
  261. {
  262. unsigned int i;
  263. if (st->use_internal_vref)
  264. return st->chip_info->internal_vref;
  265. i = st->chip_info->shared_vref ? 0 : chan->channel;
  266. return regulator_get_voltage(st->vref_reg[i].consumer);
  267. }
  268. static int ad5064_read_raw(struct iio_dev *indio_dev,
  269. struct iio_chan_spec const *chan,
  270. int *val,
  271. int *val2,
  272. long m)
  273. {
  274. struct ad5064_state *st = iio_priv(indio_dev);
  275. int scale_uv;
  276. switch (m) {
  277. case IIO_CHAN_INFO_RAW:
  278. *val = st->dac_cache[chan->channel];
  279. return IIO_VAL_INT;
  280. case IIO_CHAN_INFO_SCALE:
  281. scale_uv = ad5064_get_vref(st, chan);
  282. if (scale_uv < 0)
  283. return scale_uv;
  284. *val = scale_uv / 1000;
  285. *val2 = chan->scan_type.realbits;
  286. return IIO_VAL_FRACTIONAL_LOG2;
  287. default:
  288. break;
  289. }
  290. return -EINVAL;
  291. }
  292. static int ad5064_write_raw(struct iio_dev *indio_dev,
  293. struct iio_chan_spec const *chan, int val, int val2, long mask)
  294. {
  295. struct ad5064_state *st = iio_priv(indio_dev);
  296. int ret;
  297. switch (mask) {
  298. case IIO_CHAN_INFO_RAW:
  299. if (val >= (1 << chan->scan_type.realbits) || val < 0)
  300. return -EINVAL;
  301. mutex_lock(&st->lock);
  302. ret = ad5064_write(st, AD5064_CMD_WRITE_INPUT_N_UPDATE_N,
  303. chan->address, val, chan->scan_type.shift);
  304. if (ret == 0)
  305. st->dac_cache[chan->channel] = val;
  306. mutex_unlock(&st->lock);
  307. break;
  308. default:
  309. ret = -EINVAL;
  310. }
  311. return ret;
  312. }
  313. static const struct iio_info ad5064_info = {
  314. .read_raw = ad5064_read_raw,
  315. .write_raw = ad5064_write_raw,
  316. };
  317. static const struct iio_chan_spec_ext_info ad5064_ext_info[] = {
  318. {
  319. .name = "powerdown",
  320. .read = ad5064_read_dac_powerdown,
  321. .write = ad5064_write_dac_powerdown,
  322. .shared = IIO_SEPARATE,
  323. },
  324. IIO_ENUM("powerdown_mode", IIO_SEPARATE, &ad5064_powerdown_mode_enum),
  325. IIO_ENUM_AVAILABLE("powerdown_mode", IIO_SHARED_BY_TYPE, &ad5064_powerdown_mode_enum),
  326. { },
  327. };
  328. static const struct iio_chan_spec_ext_info ltc2617_ext_info[] = {
  329. {
  330. .name = "powerdown",
  331. .read = ad5064_read_dac_powerdown,
  332. .write = ad5064_write_dac_powerdown,
  333. .shared = IIO_SEPARATE,
  334. },
  335. IIO_ENUM("powerdown_mode", IIO_SEPARATE, &ltc2617_powerdown_mode_enum),
  336. IIO_ENUM_AVAILABLE("powerdown_mode", IIO_SHARED_BY_TYPE, &ltc2617_powerdown_mode_enum),
  337. { },
  338. };
  339. #define AD5064_CHANNEL(chan, addr, bits, _shift, _ext_info) { \
  340. .type = IIO_VOLTAGE, \
  341. .indexed = 1, \
  342. .output = 1, \
  343. .channel = (chan), \
  344. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  345. BIT(IIO_CHAN_INFO_SCALE), \
  346. .address = addr, \
  347. .scan_type = { \
  348. .sign = 'u', \
  349. .realbits = (bits), \
  350. .storagebits = 16, \
  351. .shift = (_shift), \
  352. }, \
  353. .ext_info = (_ext_info), \
  354. }
  355. #define DECLARE_AD5064_CHANNELS(name, bits, shift, ext_info) \
  356. const struct iio_chan_spec name[] = { \
  357. AD5064_CHANNEL(0, 0, bits, shift, ext_info), \
  358. AD5064_CHANNEL(1, 1, bits, shift, ext_info), \
  359. AD5064_CHANNEL(2, 2, bits, shift, ext_info), \
  360. AD5064_CHANNEL(3, 3, bits, shift, ext_info), \
  361. AD5064_CHANNEL(4, 4, bits, shift, ext_info), \
  362. AD5064_CHANNEL(5, 5, bits, shift, ext_info), \
  363. AD5064_CHANNEL(6, 6, bits, shift, ext_info), \
  364. AD5064_CHANNEL(7, 7, bits, shift, ext_info), \
  365. }
  366. #define DECLARE_AD5065_CHANNELS(name, bits, shift, ext_info) \
  367. const struct iio_chan_spec name[] = { \
  368. AD5064_CHANNEL(0, 0, bits, shift, ext_info), \
  369. AD5064_CHANNEL(1, 3, bits, shift, ext_info), \
  370. }
  371. static DECLARE_AD5064_CHANNELS(ad5024_channels, 12, 8, ad5064_ext_info);
  372. static DECLARE_AD5064_CHANNELS(ad5044_channels, 14, 6, ad5064_ext_info);
  373. static DECLARE_AD5064_CHANNELS(ad5064_channels, 16, 4, ad5064_ext_info);
  374. static DECLARE_AD5065_CHANNELS(ad5025_channels, 12, 8, ad5064_ext_info);
  375. static DECLARE_AD5065_CHANNELS(ad5045_channels, 14, 6, ad5064_ext_info);
  376. static DECLARE_AD5065_CHANNELS(ad5065_channels, 16, 4, ad5064_ext_info);
  377. static DECLARE_AD5064_CHANNELS(ad5629_channels, 12, 4, ad5064_ext_info);
  378. static DECLARE_AD5064_CHANNELS(ad5645_channels, 14, 2, ad5064_ext_info);
  379. static DECLARE_AD5064_CHANNELS(ad5669_channels, 16, 0, ad5064_ext_info);
  380. static DECLARE_AD5064_CHANNELS(ltc2607_channels, 16, 0, ltc2617_ext_info);
  381. static DECLARE_AD5064_CHANNELS(ltc2617_channels, 14, 2, ltc2617_ext_info);
  382. static DECLARE_AD5064_CHANNELS(ltc2627_channels, 12, 4, ltc2617_ext_info);
  383. #define ltc2631_12_channels ltc2627_channels
  384. static DECLARE_AD5064_CHANNELS(ltc2631_10_channels, 10, 6, ltc2617_ext_info);
  385. static DECLARE_AD5064_CHANNELS(ltc2631_8_channels, 8, 8, ltc2617_ext_info);
  386. #define LTC2631_INFO(vref, pchannels, nchannels) \
  387. { \
  388. .shared_vref = true, \
  389. .internal_vref = vref, \
  390. .channels = pchannels, \
  391. .num_channels = nchannels, \
  392. .regmap_type = AD5064_REGMAP_LTC, \
  393. }
  394. static const struct ad5064_chip_info ad5064_chip_info_tbl[] = {
  395. [ID_AD5024] = {
  396. .shared_vref = false,
  397. .channels = ad5024_channels,
  398. .num_channels = 4,
  399. .regmap_type = AD5064_REGMAP_ADI,
  400. },
  401. [ID_AD5025] = {
  402. .shared_vref = false,
  403. .channels = ad5025_channels,
  404. .num_channels = 2,
  405. .regmap_type = AD5064_REGMAP_ADI,
  406. },
  407. [ID_AD5044] = {
  408. .shared_vref = false,
  409. .channels = ad5044_channels,
  410. .num_channels = 4,
  411. .regmap_type = AD5064_REGMAP_ADI,
  412. },
  413. [ID_AD5045] = {
  414. .shared_vref = false,
  415. .channels = ad5045_channels,
  416. .num_channels = 2,
  417. .regmap_type = AD5064_REGMAP_ADI,
  418. },
  419. [ID_AD5064] = {
  420. .shared_vref = false,
  421. .channels = ad5064_channels,
  422. .num_channels = 4,
  423. .regmap_type = AD5064_REGMAP_ADI,
  424. },
  425. [ID_AD5064_1] = {
  426. .shared_vref = true,
  427. .channels = ad5064_channels,
  428. .num_channels = 4,
  429. .regmap_type = AD5064_REGMAP_ADI,
  430. },
  431. [ID_AD5065] = {
  432. .shared_vref = false,
  433. .channels = ad5065_channels,
  434. .num_channels = 2,
  435. .regmap_type = AD5064_REGMAP_ADI,
  436. },
  437. [ID_AD5625] = {
  438. .shared_vref = true,
  439. .channels = ad5629_channels,
  440. .num_channels = 4,
  441. .regmap_type = AD5064_REGMAP_ADI2
  442. },
  443. [ID_AD5625R_1V25] = {
  444. .shared_vref = true,
  445. .internal_vref = 1250000,
  446. .channels = ad5629_channels,
  447. .num_channels = 4,
  448. .regmap_type = AD5064_REGMAP_ADI2
  449. },
  450. [ID_AD5625R_2V5] = {
  451. .shared_vref = true,
  452. .internal_vref = 2500000,
  453. .channels = ad5629_channels,
  454. .num_channels = 4,
  455. .regmap_type = AD5064_REGMAP_ADI2
  456. },
  457. [ID_AD5627] = {
  458. .shared_vref = true,
  459. .channels = ad5629_channels,
  460. .num_channels = 2,
  461. .regmap_type = AD5064_REGMAP_ADI2
  462. },
  463. [ID_AD5627R_1V25] = {
  464. .shared_vref = true,
  465. .internal_vref = 1250000,
  466. .channels = ad5629_channels,
  467. .num_channels = 2,
  468. .regmap_type = AD5064_REGMAP_ADI2
  469. },
  470. [ID_AD5627R_2V5] = {
  471. .shared_vref = true,
  472. .internal_vref = 2500000,
  473. .channels = ad5629_channels,
  474. .num_channels = 2,
  475. .regmap_type = AD5064_REGMAP_ADI2
  476. },
  477. [ID_AD5628_1] = {
  478. .shared_vref = true,
  479. .internal_vref = 2500000,
  480. .channels = ad5024_channels,
  481. .num_channels = 8,
  482. .regmap_type = AD5064_REGMAP_ADI,
  483. },
  484. [ID_AD5628_2] = {
  485. .shared_vref = true,
  486. .internal_vref = 5000000,
  487. .channels = ad5024_channels,
  488. .num_channels = 8,
  489. .regmap_type = AD5064_REGMAP_ADI,
  490. },
  491. [ID_AD5629_1] = {
  492. .shared_vref = true,
  493. .internal_vref = 2500000,
  494. .channels = ad5629_channels,
  495. .num_channels = 8,
  496. .regmap_type = AD5064_REGMAP_ADI,
  497. },
  498. [ID_AD5629_2] = {
  499. .shared_vref = true,
  500. .internal_vref = 5000000,
  501. .channels = ad5629_channels,
  502. .num_channels = 8,
  503. .regmap_type = AD5064_REGMAP_ADI,
  504. },
  505. [ID_AD5645R_1V25] = {
  506. .shared_vref = true,
  507. .internal_vref = 1250000,
  508. .channels = ad5645_channels,
  509. .num_channels = 4,
  510. .regmap_type = AD5064_REGMAP_ADI2
  511. },
  512. [ID_AD5645R_2V5] = {
  513. .shared_vref = true,
  514. .internal_vref = 2500000,
  515. .channels = ad5645_channels,
  516. .num_channels = 4,
  517. .regmap_type = AD5064_REGMAP_ADI2
  518. },
  519. [ID_AD5647R_1V25] = {
  520. .shared_vref = true,
  521. .internal_vref = 1250000,
  522. .channels = ad5645_channels,
  523. .num_channels = 2,
  524. .regmap_type = AD5064_REGMAP_ADI2
  525. },
  526. [ID_AD5647R_2V5] = {
  527. .shared_vref = true,
  528. .internal_vref = 2500000,
  529. .channels = ad5645_channels,
  530. .num_channels = 2,
  531. .regmap_type = AD5064_REGMAP_ADI2
  532. },
  533. [ID_AD5648_1] = {
  534. .shared_vref = true,
  535. .internal_vref = 2500000,
  536. .channels = ad5044_channels,
  537. .num_channels = 8,
  538. .regmap_type = AD5064_REGMAP_ADI,
  539. },
  540. [ID_AD5648_2] = {
  541. .shared_vref = true,
  542. .internal_vref = 5000000,
  543. .channels = ad5044_channels,
  544. .num_channels = 8,
  545. .regmap_type = AD5064_REGMAP_ADI,
  546. },
  547. [ID_AD5665] = {
  548. .shared_vref = true,
  549. .channels = ad5669_channels,
  550. .num_channels = 4,
  551. .regmap_type = AD5064_REGMAP_ADI2
  552. },
  553. [ID_AD5665R_1V25] = {
  554. .shared_vref = true,
  555. .internal_vref = 1250000,
  556. .channels = ad5669_channels,
  557. .num_channels = 4,
  558. .regmap_type = AD5064_REGMAP_ADI2
  559. },
  560. [ID_AD5665R_2V5] = {
  561. .shared_vref = true,
  562. .internal_vref = 2500000,
  563. .channels = ad5669_channels,
  564. .num_channels = 4,
  565. .regmap_type = AD5064_REGMAP_ADI2
  566. },
  567. [ID_AD5666_1] = {
  568. .shared_vref = true,
  569. .internal_vref = 2500000,
  570. .channels = ad5064_channels,
  571. .num_channels = 4,
  572. .regmap_type = AD5064_REGMAP_ADI,
  573. },
  574. [ID_AD5666_2] = {
  575. .shared_vref = true,
  576. .internal_vref = 5000000,
  577. .channels = ad5064_channels,
  578. .num_channels = 4,
  579. .regmap_type = AD5064_REGMAP_ADI,
  580. },
  581. [ID_AD5667] = {
  582. .shared_vref = true,
  583. .channels = ad5669_channels,
  584. .num_channels = 2,
  585. .regmap_type = AD5064_REGMAP_ADI2
  586. },
  587. [ID_AD5667R_1V25] = {
  588. .shared_vref = true,
  589. .internal_vref = 1250000,
  590. .channels = ad5669_channels,
  591. .num_channels = 2,
  592. .regmap_type = AD5064_REGMAP_ADI2
  593. },
  594. [ID_AD5667R_2V5] = {
  595. .shared_vref = true,
  596. .internal_vref = 2500000,
  597. .channels = ad5669_channels,
  598. .num_channels = 2,
  599. .regmap_type = AD5064_REGMAP_ADI2
  600. },
  601. [ID_AD5668_1] = {
  602. .shared_vref = true,
  603. .internal_vref = 2500000,
  604. .channels = ad5064_channels,
  605. .num_channels = 8,
  606. .regmap_type = AD5064_REGMAP_ADI,
  607. },
  608. [ID_AD5668_2] = {
  609. .shared_vref = true,
  610. .internal_vref = 5000000,
  611. .channels = ad5064_channels,
  612. .num_channels = 8,
  613. .regmap_type = AD5064_REGMAP_ADI,
  614. },
  615. [ID_AD5669_1] = {
  616. .shared_vref = true,
  617. .internal_vref = 2500000,
  618. .channels = ad5669_channels,
  619. .num_channels = 8,
  620. .regmap_type = AD5064_REGMAP_ADI,
  621. },
  622. [ID_AD5669_2] = {
  623. .shared_vref = true,
  624. .internal_vref = 5000000,
  625. .channels = ad5669_channels,
  626. .num_channels = 8,
  627. .regmap_type = AD5064_REGMAP_ADI,
  628. },
  629. [ID_LTC2606] = {
  630. .shared_vref = true,
  631. .internal_vref = 0,
  632. .channels = ltc2607_channels,
  633. .num_channels = 1,
  634. .regmap_type = AD5064_REGMAP_LTC,
  635. },
  636. [ID_LTC2607] = {
  637. .shared_vref = true,
  638. .internal_vref = 0,
  639. .channels = ltc2607_channels,
  640. .num_channels = 2,
  641. .regmap_type = AD5064_REGMAP_LTC,
  642. },
  643. [ID_LTC2609] = {
  644. .shared_vref = false,
  645. .internal_vref = 0,
  646. .channels = ltc2607_channels,
  647. .num_channels = 4,
  648. .regmap_type = AD5064_REGMAP_LTC,
  649. },
  650. [ID_LTC2616] = {
  651. .shared_vref = true,
  652. .internal_vref = 0,
  653. .channels = ltc2617_channels,
  654. .num_channels = 1,
  655. .regmap_type = AD5064_REGMAP_LTC,
  656. },
  657. [ID_LTC2617] = {
  658. .shared_vref = true,
  659. .internal_vref = 0,
  660. .channels = ltc2617_channels,
  661. .num_channels = 2,
  662. .regmap_type = AD5064_REGMAP_LTC,
  663. },
  664. [ID_LTC2619] = {
  665. .shared_vref = false,
  666. .internal_vref = 0,
  667. .channels = ltc2617_channels,
  668. .num_channels = 4,
  669. .regmap_type = AD5064_REGMAP_LTC,
  670. },
  671. [ID_LTC2626] = {
  672. .shared_vref = true,
  673. .internal_vref = 0,
  674. .channels = ltc2627_channels,
  675. .num_channels = 1,
  676. .regmap_type = AD5064_REGMAP_LTC,
  677. },
  678. [ID_LTC2627] = {
  679. .shared_vref = true,
  680. .internal_vref = 0,
  681. .channels = ltc2627_channels,
  682. .num_channels = 2,
  683. .regmap_type = AD5064_REGMAP_LTC,
  684. },
  685. [ID_LTC2629] = {
  686. .shared_vref = false,
  687. .internal_vref = 0,
  688. .channels = ltc2627_channels,
  689. .num_channels = 4,
  690. .regmap_type = AD5064_REGMAP_LTC,
  691. },
  692. [ID_LTC2631_L12] = LTC2631_INFO(2500000, ltc2631_12_channels, 1),
  693. [ID_LTC2631_H12] = LTC2631_INFO(4096000, ltc2631_12_channels, 1),
  694. [ID_LTC2631_L10] = LTC2631_INFO(2500000, ltc2631_10_channels, 1),
  695. [ID_LTC2631_H10] = LTC2631_INFO(4096000, ltc2631_10_channels, 1),
  696. [ID_LTC2631_L8] = LTC2631_INFO(2500000, ltc2631_8_channels, 1),
  697. [ID_LTC2631_H8] = LTC2631_INFO(4096000, ltc2631_8_channels, 1),
  698. [ID_LTC2633_L12] = LTC2631_INFO(2500000, ltc2631_12_channels, 2),
  699. [ID_LTC2633_H12] = LTC2631_INFO(4096000, ltc2631_12_channels, 2),
  700. [ID_LTC2633_L10] = LTC2631_INFO(2500000, ltc2631_10_channels, 2),
  701. [ID_LTC2633_H10] = LTC2631_INFO(4096000, ltc2631_10_channels, 2),
  702. [ID_LTC2633_L8] = LTC2631_INFO(2500000, ltc2631_8_channels, 2),
  703. [ID_LTC2633_H8] = LTC2631_INFO(4096000, ltc2631_8_channels, 2),
  704. [ID_LTC2635_L12] = LTC2631_INFO(2500000, ltc2631_12_channels, 4),
  705. [ID_LTC2635_H12] = LTC2631_INFO(4096000, ltc2631_12_channels, 4),
  706. [ID_LTC2635_L10] = LTC2631_INFO(2500000, ltc2631_10_channels, 4),
  707. [ID_LTC2635_H10] = LTC2631_INFO(4096000, ltc2631_10_channels, 4),
  708. [ID_LTC2635_L8] = LTC2631_INFO(2500000, ltc2631_8_channels, 4),
  709. [ID_LTC2635_H8] = LTC2631_INFO(4096000, ltc2631_8_channels, 4),
  710. };
  711. static inline unsigned int ad5064_num_vref(struct ad5064_state *st)
  712. {
  713. return st->chip_info->shared_vref ? 1 : st->chip_info->num_channels;
  714. }
  715. static const char * const ad5064_vref_names[] = {
  716. "vrefA",
  717. "vrefB",
  718. "vrefC",
  719. "vrefD",
  720. };
  721. static const char *ad5064_vref_name(struct ad5064_state *st,
  722. unsigned int vref)
  723. {
  724. return st->chip_info->shared_vref ? "vref" : ad5064_vref_names[vref];
  725. }
  726. static int ad5064_set_config(struct ad5064_state *st, unsigned int val)
  727. {
  728. unsigned int cmd;
  729. switch (st->chip_info->regmap_type) {
  730. case AD5064_REGMAP_ADI2:
  731. cmd = AD5064_CMD_CONFIG_V2;
  732. break;
  733. default:
  734. cmd = AD5064_CMD_CONFIG;
  735. break;
  736. }
  737. return ad5064_write(st, cmd, 0, val, 0);
  738. }
  739. static int ad5064_request_vref(struct ad5064_state *st, struct device *dev)
  740. {
  741. unsigned int i;
  742. int ret;
  743. for (i = 0; i < ad5064_num_vref(st); ++i)
  744. st->vref_reg[i].supply = ad5064_vref_name(st, i);
  745. if (!st->chip_info->internal_vref)
  746. return devm_regulator_bulk_get(dev, ad5064_num_vref(st),
  747. st->vref_reg);
  748. /*
  749. * This assumes that when the regulator has an internal VREF
  750. * there is only one external VREF connection, which is
  751. * currently the case for all supported devices.
  752. */
  753. st->vref_reg[0].consumer = devm_regulator_get_optional(dev, "vref");
  754. if (!IS_ERR(st->vref_reg[0].consumer))
  755. return 0;
  756. ret = PTR_ERR(st->vref_reg[0].consumer);
  757. if (ret != -ENODEV)
  758. return ret;
  759. /* If no external regulator was supplied use the internal VREF */
  760. st->use_internal_vref = true;
  761. ret = ad5064_set_config(st, AD5064_CONFIG_INT_VREF_ENABLE);
  762. if (ret)
  763. dev_err(dev, "Failed to enable internal vref: %d\n", ret);
  764. return ret;
  765. }
  766. static void ad5064_bulk_reg_disable(void *data)
  767. {
  768. struct ad5064_state *st = data;
  769. regulator_bulk_disable(ad5064_num_vref(st), st->vref_reg);
  770. }
  771. static int ad5064_probe(struct device *dev, enum ad5064_type type,
  772. const char *name, ad5064_write_func write)
  773. {
  774. struct iio_dev *indio_dev;
  775. struct ad5064_state *st;
  776. unsigned int midscale;
  777. unsigned int i;
  778. int ret;
  779. indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
  780. if (indio_dev == NULL)
  781. return -ENOMEM;
  782. st = iio_priv(indio_dev);
  783. mutex_init(&st->lock);
  784. st->chip_info = &ad5064_chip_info_tbl[type];
  785. st->dev = dev;
  786. st->write = write;
  787. ret = ad5064_request_vref(st, dev);
  788. if (ret)
  789. return ret;
  790. if (!st->use_internal_vref) {
  791. ret = regulator_bulk_enable(ad5064_num_vref(st), st->vref_reg);
  792. if (ret)
  793. return ret;
  794. ret = devm_add_action_or_reset(dev, ad5064_bulk_reg_disable, st);
  795. if (ret)
  796. return ret;
  797. }
  798. indio_dev->name = name;
  799. indio_dev->info = &ad5064_info;
  800. indio_dev->modes = INDIO_DIRECT_MODE;
  801. indio_dev->channels = st->chip_info->channels;
  802. indio_dev->num_channels = st->chip_info->num_channels;
  803. midscale = (1 << indio_dev->channels[0].scan_type.realbits) / 2;
  804. for (i = 0; i < st->chip_info->num_channels; ++i) {
  805. st->pwr_down_mode[i] = AD5064_LDAC_PWRDN_1K;
  806. st->dac_cache[i] = midscale;
  807. }
  808. return devm_iio_device_register(dev, indio_dev);
  809. }
  810. #if IS_ENABLED(CONFIG_SPI_MASTER)
  811. static int ad5064_spi_write(struct ad5064_state *st, unsigned int cmd,
  812. unsigned int addr, unsigned int val)
  813. {
  814. struct spi_device *spi = to_spi_device(st->dev);
  815. st->data.spi = cpu_to_be32(AD5064_CMD(cmd) | AD5064_ADDR(addr) | val);
  816. return spi_write(spi, &st->data.spi, sizeof(st->data.spi));
  817. }
  818. static int ad5064_spi_probe(struct spi_device *spi)
  819. {
  820. const struct spi_device_id *id = spi_get_device_id(spi);
  821. return ad5064_probe(&spi->dev, id->driver_data, id->name,
  822. ad5064_spi_write);
  823. }
  824. static const struct spi_device_id ad5064_spi_ids[] = {
  825. {"ad5024", ID_AD5024},
  826. {"ad5025", ID_AD5025},
  827. {"ad5044", ID_AD5044},
  828. {"ad5045", ID_AD5045},
  829. {"ad5064", ID_AD5064},
  830. {"ad5064-1", ID_AD5064_1},
  831. {"ad5065", ID_AD5065},
  832. {"ad5628-1", ID_AD5628_1},
  833. {"ad5628-2", ID_AD5628_2},
  834. {"ad5648-1", ID_AD5648_1},
  835. {"ad5648-2", ID_AD5648_2},
  836. {"ad5666-1", ID_AD5666_1},
  837. {"ad5666-2", ID_AD5666_2},
  838. {"ad5668-1", ID_AD5668_1},
  839. {"ad5668-2", ID_AD5668_2},
  840. {"ad5668-3", ID_AD5668_2}, /* similar enough to ad5668-2 */
  841. {}
  842. };
  843. MODULE_DEVICE_TABLE(spi, ad5064_spi_ids);
  844. static struct spi_driver ad5064_spi_driver = {
  845. .driver = {
  846. .name = "ad5064",
  847. },
  848. .probe = ad5064_spi_probe,
  849. .id_table = ad5064_spi_ids,
  850. };
  851. static int __init ad5064_spi_register_driver(void)
  852. {
  853. return spi_register_driver(&ad5064_spi_driver);
  854. }
  855. static void ad5064_spi_unregister_driver(void)
  856. {
  857. spi_unregister_driver(&ad5064_spi_driver);
  858. }
  859. #else
  860. static inline int ad5064_spi_register_driver(void) { return 0; }
  861. static inline void ad5064_spi_unregister_driver(void) { }
  862. #endif
  863. #if IS_ENABLED(CONFIG_I2C)
  864. static int ad5064_i2c_write(struct ad5064_state *st, unsigned int cmd,
  865. unsigned int addr, unsigned int val)
  866. {
  867. struct i2c_client *i2c = to_i2c_client(st->dev);
  868. unsigned int cmd_shift;
  869. int ret;
  870. switch (st->chip_info->regmap_type) {
  871. case AD5064_REGMAP_ADI2:
  872. cmd_shift = 3;
  873. break;
  874. default:
  875. cmd_shift = 4;
  876. break;
  877. }
  878. st->data.i2c[0] = (cmd << cmd_shift) | addr;
  879. put_unaligned_be16(val, &st->data.i2c[1]);
  880. ret = i2c_master_send(i2c, st->data.i2c, 3);
  881. if (ret < 0)
  882. return ret;
  883. return 0;
  884. }
  885. static int ad5064_i2c_probe(struct i2c_client *i2c,
  886. const struct i2c_device_id *id)
  887. {
  888. return ad5064_probe(&i2c->dev, id->driver_data, id->name,
  889. ad5064_i2c_write);
  890. }
  891. static const struct i2c_device_id ad5064_i2c_ids[] = {
  892. {"ad5625", ID_AD5625 },
  893. {"ad5625r-1v25", ID_AD5625R_1V25 },
  894. {"ad5625r-2v5", ID_AD5625R_2V5 },
  895. {"ad5627", ID_AD5627 },
  896. {"ad5627r-1v25", ID_AD5627R_1V25 },
  897. {"ad5627r-2v5", ID_AD5627R_2V5 },
  898. {"ad5629-1", ID_AD5629_1},
  899. {"ad5629-2", ID_AD5629_2},
  900. {"ad5629-3", ID_AD5629_2}, /* similar enough to ad5629-2 */
  901. {"ad5645r-1v25", ID_AD5645R_1V25 },
  902. {"ad5645r-2v5", ID_AD5645R_2V5 },
  903. {"ad5665", ID_AD5665 },
  904. {"ad5665r-1v25", ID_AD5665R_1V25 },
  905. {"ad5665r-2v5", ID_AD5665R_2V5 },
  906. {"ad5667", ID_AD5667 },
  907. {"ad5667r-1v25", ID_AD5667R_1V25 },
  908. {"ad5667r-2v5", ID_AD5667R_2V5 },
  909. {"ad5669-1", ID_AD5669_1},
  910. {"ad5669-2", ID_AD5669_2},
  911. {"ad5669-3", ID_AD5669_2}, /* similar enough to ad5669-2 */
  912. {"ltc2606", ID_LTC2606},
  913. {"ltc2607", ID_LTC2607},
  914. {"ltc2609", ID_LTC2609},
  915. {"ltc2616", ID_LTC2616},
  916. {"ltc2617", ID_LTC2617},
  917. {"ltc2619", ID_LTC2619},
  918. {"ltc2626", ID_LTC2626},
  919. {"ltc2627", ID_LTC2627},
  920. {"ltc2629", ID_LTC2629},
  921. {"ltc2631-l12", ID_LTC2631_L12},
  922. {"ltc2631-h12", ID_LTC2631_H12},
  923. {"ltc2631-l10", ID_LTC2631_L10},
  924. {"ltc2631-h10", ID_LTC2631_H10},
  925. {"ltc2631-l8", ID_LTC2631_L8},
  926. {"ltc2631-h8", ID_LTC2631_H8},
  927. {"ltc2633-l12", ID_LTC2633_L12},
  928. {"ltc2633-h12", ID_LTC2633_H12},
  929. {"ltc2633-l10", ID_LTC2633_L10},
  930. {"ltc2633-h10", ID_LTC2633_H10},
  931. {"ltc2633-l8", ID_LTC2633_L8},
  932. {"ltc2633-h8", ID_LTC2633_H8},
  933. {"ltc2635-l12", ID_LTC2635_L12},
  934. {"ltc2635-h12", ID_LTC2635_H12},
  935. {"ltc2635-l10", ID_LTC2635_L10},
  936. {"ltc2635-h10", ID_LTC2635_H10},
  937. {"ltc2635-l8", ID_LTC2635_L8},
  938. {"ltc2635-h8", ID_LTC2635_H8},
  939. {}
  940. };
  941. MODULE_DEVICE_TABLE(i2c, ad5064_i2c_ids);
  942. static struct i2c_driver ad5064_i2c_driver = {
  943. .driver = {
  944. .name = "ad5064",
  945. },
  946. .probe = ad5064_i2c_probe,
  947. .id_table = ad5064_i2c_ids,
  948. };
  949. static int __init ad5064_i2c_register_driver(void)
  950. {
  951. return i2c_add_driver(&ad5064_i2c_driver);
  952. }
  953. static void __exit ad5064_i2c_unregister_driver(void)
  954. {
  955. i2c_del_driver(&ad5064_i2c_driver);
  956. }
  957. #else
  958. static inline int ad5064_i2c_register_driver(void) { return 0; }
  959. static inline void ad5064_i2c_unregister_driver(void) { }
  960. #endif
  961. static int __init ad5064_init(void)
  962. {
  963. int ret;
  964. ret = ad5064_spi_register_driver();
  965. if (ret)
  966. return ret;
  967. ret = ad5064_i2c_register_driver();
  968. if (ret) {
  969. ad5064_spi_unregister_driver();
  970. return ret;
  971. }
  972. return 0;
  973. }
  974. module_init(ad5064_init);
  975. static void __exit ad5064_exit(void)
  976. {
  977. ad5064_i2c_unregister_driver();
  978. ad5064_spi_unregister_driver();
  979. }
  980. module_exit(ad5064_exit);
  981. MODULE_AUTHOR("Lars-Peter Clausen <[email protected]>");
  982. MODULE_DESCRIPTION("Analog Devices AD5024 and similar multi-channel DACs");
  983. MODULE_LICENSE("GPL v2");