ad5758.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * AD5758 Digital to analog converters driver
  4. *
  5. * Copyright 2018 Analog Devices Inc.
  6. *
  7. * TODO: Currently CRC is not supported in this driver
  8. */
  9. #include <linux/bsearch.h>
  10. #include <linux/delay.h>
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/property.h>
  14. #include <linux/spi/spi.h>
  15. #include <linux/iio/iio.h>
  16. #include <linux/iio/sysfs.h>
  17. /* AD5758 registers definition */
  18. #define AD5758_NOP 0x00
  19. #define AD5758_DAC_INPUT 0x01
  20. #define AD5758_DAC_OUTPUT 0x02
  21. #define AD5758_CLEAR_CODE 0x03
  22. #define AD5758_USER_GAIN 0x04
  23. #define AD5758_USER_OFFSET 0x05
  24. #define AD5758_DAC_CONFIG 0x06
  25. #define AD5758_SW_LDAC 0x07
  26. #define AD5758_KEY 0x08
  27. #define AD5758_GP_CONFIG1 0x09
  28. #define AD5758_GP_CONFIG2 0x0A
  29. #define AD5758_DCDC_CONFIG1 0x0B
  30. #define AD5758_DCDC_CONFIG2 0x0C
  31. #define AD5758_WDT_CONFIG 0x0F
  32. #define AD5758_DIGITAL_DIAG_CONFIG 0x10
  33. #define AD5758_ADC_CONFIG 0x11
  34. #define AD5758_FAULT_PIN_CONFIG 0x12
  35. #define AD5758_TWO_STAGE_READBACK_SELECT 0x13
  36. #define AD5758_DIGITAL_DIAG_RESULTS 0x14
  37. #define AD5758_ANALOG_DIAG_RESULTS 0x15
  38. #define AD5758_STATUS 0x16
  39. #define AD5758_CHIP_ID 0x17
  40. #define AD5758_FREQ_MONITOR 0x18
  41. #define AD5758_DEVICE_ID_0 0x19
  42. #define AD5758_DEVICE_ID_1 0x1A
  43. #define AD5758_DEVICE_ID_2 0x1B
  44. #define AD5758_DEVICE_ID_3 0x1C
  45. /* AD5758_DAC_CONFIG */
  46. #define AD5758_DAC_CONFIG_RANGE_MSK GENMASK(3, 0)
  47. #define AD5758_DAC_CONFIG_RANGE_MODE(x) (((x) & 0xF) << 0)
  48. #define AD5758_DAC_CONFIG_INT_EN_MSK BIT(5)
  49. #define AD5758_DAC_CONFIG_INT_EN_MODE(x) (((x) & 0x1) << 5)
  50. #define AD5758_DAC_CONFIG_OUT_EN_MSK BIT(6)
  51. #define AD5758_DAC_CONFIG_OUT_EN_MODE(x) (((x) & 0x1) << 6)
  52. #define AD5758_DAC_CONFIG_SR_EN_MSK BIT(8)
  53. #define AD5758_DAC_CONFIG_SR_EN_MODE(x) (((x) & 0x1) << 8)
  54. #define AD5758_DAC_CONFIG_SR_CLOCK_MSK GENMASK(12, 9)
  55. #define AD5758_DAC_CONFIG_SR_CLOCK_MODE(x) (((x) & 0xF) << 9)
  56. #define AD5758_DAC_CONFIG_SR_STEP_MSK GENMASK(15, 13)
  57. #define AD5758_DAC_CONFIG_SR_STEP_MODE(x) (((x) & 0x7) << 13)
  58. /* AD5758_KEY */
  59. #define AD5758_KEY_CODE_RESET_1 0x15FA
  60. #define AD5758_KEY_CODE_RESET_2 0xAF51
  61. #define AD5758_KEY_CODE_SINGLE_ADC_CONV 0x1ADC
  62. #define AD5758_KEY_CODE_RESET_WDT 0x0D06
  63. #define AD5758_KEY_CODE_CALIB_MEM_REFRESH 0xFCBA
  64. /* AD5758_DCDC_CONFIG1 */
  65. #define AD5758_DCDC_CONFIG1_DCDC_VPROG_MSK GENMASK(4, 0)
  66. #define AD5758_DCDC_CONFIG1_DCDC_VPROG_MODE(x) (((x) & 0x1F) << 0)
  67. #define AD5758_DCDC_CONFIG1_DCDC_MODE_MSK GENMASK(6, 5)
  68. #define AD5758_DCDC_CONFIG1_DCDC_MODE_MODE(x) (((x) & 0x3) << 5)
  69. #define AD5758_DCDC_CONFIG1_PROT_SW_EN_MSK BIT(7)
  70. #define AD5758_DCDC_CONFIG1_PROT_SW_EN_MODE(x) (((x) & 0x1) << 7)
  71. /* AD5758_DCDC_CONFIG2 */
  72. #define AD5758_DCDC_CONFIG2_ILIMIT_MSK GENMASK(3, 1)
  73. #define AD5758_DCDC_CONFIG2_ILIMIT_MODE(x) (((x) & 0x7) << 1)
  74. #define AD5758_DCDC_CONFIG2_INTR_SAT_3WI_MSK BIT(11)
  75. #define AD5758_DCDC_CONFIG2_BUSY_3WI_MSK BIT(12)
  76. /* AD5758_DIGITAL_DIAG_RESULTS */
  77. #define AD5758_CAL_MEM_UNREFRESHED_MSK BIT(15)
  78. #define AD5758_WR_FLAG_MSK(x) (0x80 | ((x) & 0x1F))
  79. #define AD5758_FULL_SCALE_MICRO 65535000000ULL
  80. /**
  81. * struct ad5758_state - driver instance specific data
  82. * @spi: spi_device
  83. * @lock: mutex lock
  84. * @out_range: struct which stores the output range
  85. * @dc_dc_mode: variable which stores the mode of operation
  86. * @dc_dc_ilim: variable which stores the dc-to-dc converter current limit
  87. * @slew_time: variable which stores the target slew time
  88. * @pwr_down: variable which contains whether a channel is powered down or not
  89. * @data: spi transfer buffers
  90. */
  91. struct ad5758_range {
  92. int reg;
  93. int min;
  94. int max;
  95. };
  96. struct ad5758_state {
  97. struct spi_device *spi;
  98. struct mutex lock;
  99. struct ad5758_range out_range;
  100. unsigned int dc_dc_mode;
  101. unsigned int dc_dc_ilim;
  102. unsigned int slew_time;
  103. bool pwr_down;
  104. __be32 d32[3];
  105. };
  106. /**
  107. * Output ranges corresponding to bits [3:0] from DAC_CONFIG register
  108. * 0000: 0 V to 5 V voltage range
  109. * 0001: 0 V to 10 V voltage range
  110. * 0010: ±5 V voltage range
  111. * 0011: ±10 V voltage range
  112. * 1000: 0 mA to 20 mA current range
  113. * 1001: 0 mA to 24 mA current range
  114. * 1010: 4 mA to 20 mA current range
  115. * 1011: ±20 mA current range
  116. * 1100: ±24 mA current range
  117. * 1101: -1 mA to +22 mA current range
  118. */
  119. enum ad5758_output_range {
  120. AD5758_RANGE_0V_5V,
  121. AD5758_RANGE_0V_10V,
  122. AD5758_RANGE_PLUSMINUS_5V,
  123. AD5758_RANGE_PLUSMINUS_10V,
  124. AD5758_RANGE_0mA_20mA = 8,
  125. AD5758_RANGE_0mA_24mA,
  126. AD5758_RANGE_4mA_24mA,
  127. AD5758_RANGE_PLUSMINUS_20mA,
  128. AD5758_RANGE_PLUSMINUS_24mA,
  129. AD5758_RANGE_MINUS_1mA_PLUS_22mA,
  130. };
  131. enum ad5758_dc_dc_mode {
  132. AD5758_DCDC_MODE_POWER_OFF,
  133. AD5758_DCDC_MODE_DPC_CURRENT,
  134. AD5758_DCDC_MODE_DPC_VOLTAGE,
  135. AD5758_DCDC_MODE_PPC_CURRENT,
  136. };
  137. static const struct ad5758_range ad5758_voltage_range[] = {
  138. { AD5758_RANGE_0V_5V, 0, 5000000 },
  139. { AD5758_RANGE_0V_10V, 0, 10000000 },
  140. { AD5758_RANGE_PLUSMINUS_5V, -5000000, 5000000 },
  141. { AD5758_RANGE_PLUSMINUS_10V, -10000000, 10000000 }
  142. };
  143. static const struct ad5758_range ad5758_current_range[] = {
  144. { AD5758_RANGE_0mA_20mA, 0, 20000},
  145. { AD5758_RANGE_0mA_24mA, 0, 24000 },
  146. { AD5758_RANGE_4mA_24mA, 4, 24000 },
  147. { AD5758_RANGE_PLUSMINUS_20mA, -20000, 20000 },
  148. { AD5758_RANGE_PLUSMINUS_24mA, -24000, 24000 },
  149. { AD5758_RANGE_MINUS_1mA_PLUS_22mA, -1000, 22000 },
  150. };
  151. static const int ad5758_sr_clk[16] = {
  152. 240000, 200000, 150000, 128000, 64000, 32000, 16000, 8000, 4000, 2000,
  153. 1000, 512, 256, 128, 64, 16
  154. };
  155. static const int ad5758_sr_step[8] = {
  156. 4, 12, 64, 120, 256, 500, 1820, 2048
  157. };
  158. static const int ad5758_dc_dc_ilim[6] = {
  159. 150000, 200000, 250000, 300000, 350000, 400000
  160. };
  161. static int ad5758_spi_reg_read(struct ad5758_state *st, unsigned int addr)
  162. {
  163. struct spi_transfer t[] = {
  164. {
  165. .tx_buf = &st->d32[0],
  166. .len = 4,
  167. .cs_change = 1,
  168. }, {
  169. .tx_buf = &st->d32[1],
  170. .rx_buf = &st->d32[2],
  171. .len = 4,
  172. },
  173. };
  174. int ret;
  175. st->d32[0] = cpu_to_be32(
  176. (AD5758_WR_FLAG_MSK(AD5758_TWO_STAGE_READBACK_SELECT) << 24) |
  177. (addr << 8));
  178. st->d32[1] = cpu_to_be32(AD5758_WR_FLAG_MSK(AD5758_NOP) << 24);
  179. ret = spi_sync_transfer(st->spi, t, ARRAY_SIZE(t));
  180. if (ret < 0)
  181. return ret;
  182. return (be32_to_cpu(st->d32[2]) >> 8) & 0xFFFF;
  183. }
  184. static int ad5758_spi_reg_write(struct ad5758_state *st,
  185. unsigned int addr,
  186. unsigned int val)
  187. {
  188. st->d32[0] = cpu_to_be32((AD5758_WR_FLAG_MSK(addr) << 24) |
  189. ((val & 0xFFFF) << 8));
  190. return spi_write(st->spi, &st->d32[0], sizeof(st->d32[0]));
  191. }
  192. static int ad5758_spi_write_mask(struct ad5758_state *st,
  193. unsigned int addr,
  194. unsigned long int mask,
  195. unsigned int val)
  196. {
  197. int regval;
  198. regval = ad5758_spi_reg_read(st, addr);
  199. if (regval < 0)
  200. return regval;
  201. regval &= ~mask;
  202. regval |= val;
  203. return ad5758_spi_reg_write(st, addr, regval);
  204. }
  205. static int cmpfunc(const void *a, const void *b)
  206. {
  207. return *(int *)a - *(int *)b;
  208. }
  209. static int ad5758_find_closest_match(const int *array,
  210. unsigned int size, int val)
  211. {
  212. int i;
  213. for (i = 0; i < size; i++) {
  214. if (val <= array[i])
  215. return i;
  216. }
  217. return size - 1;
  218. }
  219. static int ad5758_wait_for_task_complete(struct ad5758_state *st,
  220. unsigned int reg,
  221. unsigned int mask)
  222. {
  223. unsigned int timeout;
  224. int ret;
  225. timeout = 10;
  226. do {
  227. ret = ad5758_spi_reg_read(st, reg);
  228. if (ret < 0)
  229. return ret;
  230. if (!(ret & mask))
  231. return 0;
  232. usleep_range(100, 1000);
  233. } while (--timeout);
  234. dev_err(&st->spi->dev,
  235. "Error reading bit 0x%x in 0x%x register\n", mask, reg);
  236. return -EIO;
  237. }
  238. static int ad5758_calib_mem_refresh(struct ad5758_state *st)
  239. {
  240. int ret;
  241. ret = ad5758_spi_reg_write(st, AD5758_KEY,
  242. AD5758_KEY_CODE_CALIB_MEM_REFRESH);
  243. if (ret < 0) {
  244. dev_err(&st->spi->dev,
  245. "Failed to initiate a calibration memory refresh\n");
  246. return ret;
  247. }
  248. /* Wait to allow time for the internal calibrations to complete */
  249. return ad5758_wait_for_task_complete(st, AD5758_DIGITAL_DIAG_RESULTS,
  250. AD5758_CAL_MEM_UNREFRESHED_MSK);
  251. }
  252. static int ad5758_soft_reset(struct ad5758_state *st)
  253. {
  254. int ret;
  255. ret = ad5758_spi_reg_write(st, AD5758_KEY, AD5758_KEY_CODE_RESET_1);
  256. if (ret < 0)
  257. return ret;
  258. ret = ad5758_spi_reg_write(st, AD5758_KEY, AD5758_KEY_CODE_RESET_2);
  259. /* Perform a software reset and wait at least 100us */
  260. usleep_range(100, 1000);
  261. return ret;
  262. }
  263. static int ad5758_set_dc_dc_conv_mode(struct ad5758_state *st,
  264. enum ad5758_dc_dc_mode mode)
  265. {
  266. int ret;
  267. ret = ad5758_spi_write_mask(st, AD5758_DCDC_CONFIG1,
  268. AD5758_DCDC_CONFIG1_DCDC_MODE_MSK,
  269. AD5758_DCDC_CONFIG1_DCDC_MODE_MODE(mode));
  270. if (ret < 0)
  271. return ret;
  272. /*
  273. * Poll the BUSY_3WI bit in the DCDC_CONFIG2 register until it is 0.
  274. * This allows the 3-wire interface communication to complete.
  275. */
  276. ret = ad5758_wait_for_task_complete(st, AD5758_DCDC_CONFIG2,
  277. AD5758_DCDC_CONFIG2_BUSY_3WI_MSK);
  278. if (ret < 0)
  279. return ret;
  280. st->dc_dc_mode = mode;
  281. return ret;
  282. }
  283. static int ad5758_set_dc_dc_ilim(struct ad5758_state *st, unsigned int ilim)
  284. {
  285. int ret;
  286. ret = ad5758_spi_write_mask(st, AD5758_DCDC_CONFIG2,
  287. AD5758_DCDC_CONFIG2_ILIMIT_MSK,
  288. AD5758_DCDC_CONFIG2_ILIMIT_MODE(ilim));
  289. if (ret < 0)
  290. return ret;
  291. /*
  292. * Poll the BUSY_3WI bit in the DCDC_CONFIG2 register until it is 0.
  293. * This allows the 3-wire interface communication to complete.
  294. */
  295. return ad5758_wait_for_task_complete(st, AD5758_DCDC_CONFIG2,
  296. AD5758_DCDC_CONFIG2_BUSY_3WI_MSK);
  297. }
  298. static int ad5758_slew_rate_set(struct ad5758_state *st,
  299. unsigned int sr_clk_idx,
  300. unsigned int sr_step_idx)
  301. {
  302. unsigned int mode;
  303. unsigned long int mask;
  304. int ret;
  305. mask = AD5758_DAC_CONFIG_SR_EN_MSK |
  306. AD5758_DAC_CONFIG_SR_CLOCK_MSK |
  307. AD5758_DAC_CONFIG_SR_STEP_MSK;
  308. mode = AD5758_DAC_CONFIG_SR_EN_MODE(1) |
  309. AD5758_DAC_CONFIG_SR_STEP_MODE(sr_step_idx) |
  310. AD5758_DAC_CONFIG_SR_CLOCK_MODE(sr_clk_idx);
  311. ret = ad5758_spi_write_mask(st, AD5758_DAC_CONFIG, mask, mode);
  312. if (ret < 0)
  313. return ret;
  314. /* Wait to allow time for the internal calibrations to complete */
  315. return ad5758_wait_for_task_complete(st, AD5758_DIGITAL_DIAG_RESULTS,
  316. AD5758_CAL_MEM_UNREFRESHED_MSK);
  317. }
  318. static int ad5758_slew_rate_config(struct ad5758_state *st)
  319. {
  320. unsigned int sr_clk_idx, sr_step_idx;
  321. int i, res;
  322. s64 diff_new, diff_old;
  323. u64 sr_step, calc_slew_time;
  324. sr_clk_idx = 0;
  325. sr_step_idx = 0;
  326. diff_old = S64_MAX;
  327. /*
  328. * The slew time can be determined by using the formula:
  329. * Slew Time = (Full Scale Out / (Step Size x Update Clk Freq))
  330. * where Slew time is expressed in microseconds
  331. * Given the desired slew time, the following algorithm determines the
  332. * best match for the step size and the update clock frequency.
  333. */
  334. for (i = 0; i < ARRAY_SIZE(ad5758_sr_clk); i++) {
  335. /*
  336. * Go through each valid update clock freq and determine a raw
  337. * value for the step size by using the formula:
  338. * Step Size = Full Scale Out / (Update Clk Freq * Slew Time)
  339. */
  340. sr_step = AD5758_FULL_SCALE_MICRO;
  341. do_div(sr_step, ad5758_sr_clk[i]);
  342. do_div(sr_step, st->slew_time);
  343. /*
  344. * After a raw value for step size was determined, find the
  345. * closest valid match
  346. */
  347. res = ad5758_find_closest_match(ad5758_sr_step,
  348. ARRAY_SIZE(ad5758_sr_step),
  349. sr_step);
  350. /* Calculate the slew time */
  351. calc_slew_time = AD5758_FULL_SCALE_MICRO;
  352. do_div(calc_slew_time, ad5758_sr_step[res]);
  353. do_div(calc_slew_time, ad5758_sr_clk[i]);
  354. /*
  355. * Determine with how many microseconds the calculated slew time
  356. * is different from the desired slew time and store the diff
  357. * for the next iteration
  358. */
  359. diff_new = abs(st->slew_time - calc_slew_time);
  360. if (diff_new < diff_old) {
  361. diff_old = diff_new;
  362. sr_clk_idx = i;
  363. sr_step_idx = res;
  364. }
  365. }
  366. return ad5758_slew_rate_set(st, sr_clk_idx, sr_step_idx);
  367. }
  368. static int ad5758_set_out_range(struct ad5758_state *st, int range)
  369. {
  370. int ret;
  371. ret = ad5758_spi_write_mask(st, AD5758_DAC_CONFIG,
  372. AD5758_DAC_CONFIG_RANGE_MSK,
  373. AD5758_DAC_CONFIG_RANGE_MODE(range));
  374. if (ret < 0)
  375. return ret;
  376. /* Wait to allow time for the internal calibrations to complete */
  377. return ad5758_wait_for_task_complete(st, AD5758_DIGITAL_DIAG_RESULTS,
  378. AD5758_CAL_MEM_UNREFRESHED_MSK);
  379. }
  380. static int ad5758_fault_prot_switch_en(struct ad5758_state *st, bool enable)
  381. {
  382. int ret;
  383. ret = ad5758_spi_write_mask(st, AD5758_DCDC_CONFIG1,
  384. AD5758_DCDC_CONFIG1_PROT_SW_EN_MSK,
  385. AD5758_DCDC_CONFIG1_PROT_SW_EN_MODE(enable));
  386. if (ret < 0)
  387. return ret;
  388. /*
  389. * Poll the BUSY_3WI bit in the DCDC_CONFIG2 register until it is 0.
  390. * This allows the 3-wire interface communication to complete.
  391. */
  392. return ad5758_wait_for_task_complete(st, AD5758_DCDC_CONFIG2,
  393. AD5758_DCDC_CONFIG2_BUSY_3WI_MSK);
  394. }
  395. static int ad5758_internal_buffers_en(struct ad5758_state *st, bool enable)
  396. {
  397. int ret;
  398. ret = ad5758_spi_write_mask(st, AD5758_DAC_CONFIG,
  399. AD5758_DAC_CONFIG_INT_EN_MSK,
  400. AD5758_DAC_CONFIG_INT_EN_MODE(enable));
  401. if (ret < 0)
  402. return ret;
  403. /* Wait to allow time for the internal calibrations to complete */
  404. return ad5758_wait_for_task_complete(st, AD5758_DIGITAL_DIAG_RESULTS,
  405. AD5758_CAL_MEM_UNREFRESHED_MSK);
  406. }
  407. static int ad5758_reg_access(struct iio_dev *indio_dev,
  408. unsigned int reg,
  409. unsigned int writeval,
  410. unsigned int *readval)
  411. {
  412. struct ad5758_state *st = iio_priv(indio_dev);
  413. int ret;
  414. mutex_lock(&st->lock);
  415. if (readval) {
  416. ret = ad5758_spi_reg_read(st, reg);
  417. if (ret < 0) {
  418. mutex_unlock(&st->lock);
  419. return ret;
  420. }
  421. *readval = ret;
  422. ret = 0;
  423. } else {
  424. ret = ad5758_spi_reg_write(st, reg, writeval);
  425. }
  426. mutex_unlock(&st->lock);
  427. return ret;
  428. }
  429. static int ad5758_read_raw(struct iio_dev *indio_dev,
  430. struct iio_chan_spec const *chan,
  431. int *val, int *val2, long info)
  432. {
  433. struct ad5758_state *st = iio_priv(indio_dev);
  434. int max, min, ret;
  435. switch (info) {
  436. case IIO_CHAN_INFO_RAW:
  437. mutex_lock(&st->lock);
  438. ret = ad5758_spi_reg_read(st, AD5758_DAC_INPUT);
  439. mutex_unlock(&st->lock);
  440. if (ret < 0)
  441. return ret;
  442. *val = ret;
  443. return IIO_VAL_INT;
  444. case IIO_CHAN_INFO_SCALE:
  445. min = st->out_range.min;
  446. max = st->out_range.max;
  447. *val = (max - min) / 1000;
  448. *val2 = 16;
  449. return IIO_VAL_FRACTIONAL_LOG2;
  450. case IIO_CHAN_INFO_OFFSET:
  451. min = st->out_range.min;
  452. max = st->out_range.max;
  453. *val = ((min * (1 << 16)) / (max - min)) / 1000;
  454. return IIO_VAL_INT;
  455. default:
  456. return -EINVAL;
  457. }
  458. }
  459. static int ad5758_write_raw(struct iio_dev *indio_dev,
  460. struct iio_chan_spec const *chan,
  461. int val, int val2, long info)
  462. {
  463. struct ad5758_state *st = iio_priv(indio_dev);
  464. int ret;
  465. switch (info) {
  466. case IIO_CHAN_INFO_RAW:
  467. mutex_lock(&st->lock);
  468. ret = ad5758_spi_reg_write(st, AD5758_DAC_INPUT, val);
  469. mutex_unlock(&st->lock);
  470. return ret;
  471. default:
  472. return -EINVAL;
  473. }
  474. }
  475. static ssize_t ad5758_read_powerdown(struct iio_dev *indio_dev,
  476. uintptr_t priv,
  477. const struct iio_chan_spec *chan,
  478. char *buf)
  479. {
  480. struct ad5758_state *st = iio_priv(indio_dev);
  481. return sprintf(buf, "%d\n", st->pwr_down);
  482. }
  483. static ssize_t ad5758_write_powerdown(struct iio_dev *indio_dev,
  484. uintptr_t priv,
  485. struct iio_chan_spec const *chan,
  486. const char *buf, size_t len)
  487. {
  488. struct ad5758_state *st = iio_priv(indio_dev);
  489. bool pwr_down;
  490. unsigned int dcdc_config1_mode, dc_dc_mode, dac_config_mode, val;
  491. unsigned long int dcdc_config1_msk, dac_config_msk;
  492. int ret;
  493. ret = kstrtobool(buf, &pwr_down);
  494. if (ret)
  495. return ret;
  496. mutex_lock(&st->lock);
  497. if (pwr_down) {
  498. dc_dc_mode = AD5758_DCDC_MODE_POWER_OFF;
  499. val = 0;
  500. } else {
  501. dc_dc_mode = st->dc_dc_mode;
  502. val = 1;
  503. }
  504. dcdc_config1_mode = AD5758_DCDC_CONFIG1_DCDC_MODE_MODE(dc_dc_mode) |
  505. AD5758_DCDC_CONFIG1_PROT_SW_EN_MODE(val);
  506. dcdc_config1_msk = AD5758_DCDC_CONFIG1_DCDC_MODE_MSK |
  507. AD5758_DCDC_CONFIG1_PROT_SW_EN_MSK;
  508. ret = ad5758_spi_write_mask(st, AD5758_DCDC_CONFIG1,
  509. dcdc_config1_msk,
  510. dcdc_config1_mode);
  511. if (ret < 0)
  512. goto err_unlock;
  513. dac_config_mode = AD5758_DAC_CONFIG_OUT_EN_MODE(val) |
  514. AD5758_DAC_CONFIG_INT_EN_MODE(val);
  515. dac_config_msk = AD5758_DAC_CONFIG_OUT_EN_MSK |
  516. AD5758_DAC_CONFIG_INT_EN_MSK;
  517. ret = ad5758_spi_write_mask(st, AD5758_DAC_CONFIG,
  518. dac_config_msk,
  519. dac_config_mode);
  520. if (ret < 0)
  521. goto err_unlock;
  522. st->pwr_down = pwr_down;
  523. err_unlock:
  524. mutex_unlock(&st->lock);
  525. return ret ? ret : len;
  526. }
  527. static const struct iio_info ad5758_info = {
  528. .read_raw = ad5758_read_raw,
  529. .write_raw = ad5758_write_raw,
  530. .debugfs_reg_access = &ad5758_reg_access,
  531. };
  532. static const struct iio_chan_spec_ext_info ad5758_ext_info[] = {
  533. {
  534. .name = "powerdown",
  535. .read = ad5758_read_powerdown,
  536. .write = ad5758_write_powerdown,
  537. .shared = IIO_SHARED_BY_TYPE,
  538. },
  539. { }
  540. };
  541. #define AD5758_DAC_CHAN(_chan_type) { \
  542. .type = (_chan_type), \
  543. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_RAW) | \
  544. BIT(IIO_CHAN_INFO_SCALE) | \
  545. BIT(IIO_CHAN_INFO_OFFSET), \
  546. .indexed = 1, \
  547. .output = 1, \
  548. .ext_info = ad5758_ext_info, \
  549. }
  550. static const struct iio_chan_spec ad5758_voltage_ch[] = {
  551. AD5758_DAC_CHAN(IIO_VOLTAGE)
  552. };
  553. static const struct iio_chan_spec ad5758_current_ch[] = {
  554. AD5758_DAC_CHAN(IIO_CURRENT)
  555. };
  556. static bool ad5758_is_valid_mode(enum ad5758_dc_dc_mode mode)
  557. {
  558. switch (mode) {
  559. case AD5758_DCDC_MODE_DPC_CURRENT:
  560. case AD5758_DCDC_MODE_DPC_VOLTAGE:
  561. case AD5758_DCDC_MODE_PPC_CURRENT:
  562. return true;
  563. default:
  564. return false;
  565. }
  566. }
  567. static int ad5758_crc_disable(struct ad5758_state *st)
  568. {
  569. unsigned int mask;
  570. mask = (AD5758_WR_FLAG_MSK(AD5758_DIGITAL_DIAG_CONFIG) << 24) | 0x5C3A;
  571. st->d32[0] = cpu_to_be32(mask);
  572. return spi_write(st->spi, &st->d32[0], 4);
  573. }
  574. static int ad5758_find_out_range(struct ad5758_state *st,
  575. const struct ad5758_range *range,
  576. unsigned int size,
  577. int min, int max)
  578. {
  579. int i;
  580. for (i = 0; i < size; i++) {
  581. if ((min == range[i].min) && (max == range[i].max)) {
  582. st->out_range.reg = range[i].reg;
  583. st->out_range.min = range[i].min;
  584. st->out_range.max = range[i].max;
  585. return 0;
  586. }
  587. }
  588. return -EINVAL;
  589. }
  590. static int ad5758_parse_dt(struct ad5758_state *st)
  591. {
  592. unsigned int tmp, tmparray[2], size;
  593. const struct ad5758_range *range;
  594. int *index, ret;
  595. st->dc_dc_ilim = 0;
  596. ret = device_property_read_u32(&st->spi->dev,
  597. "adi,dc-dc-ilim-microamp", &tmp);
  598. if (ret) {
  599. dev_dbg(&st->spi->dev,
  600. "Missing \"dc-dc-ilim-microamp\" property\n");
  601. } else {
  602. index = bsearch(&tmp, ad5758_dc_dc_ilim,
  603. ARRAY_SIZE(ad5758_dc_dc_ilim),
  604. sizeof(int), cmpfunc);
  605. if (!index)
  606. dev_dbg(&st->spi->dev, "dc-dc-ilim out of range\n");
  607. else
  608. st->dc_dc_ilim = index - ad5758_dc_dc_ilim;
  609. }
  610. ret = device_property_read_u32(&st->spi->dev, "adi,dc-dc-mode",
  611. &st->dc_dc_mode);
  612. if (ret) {
  613. dev_err(&st->spi->dev, "Missing \"dc-dc-mode\" property\n");
  614. return ret;
  615. }
  616. if (!ad5758_is_valid_mode(st->dc_dc_mode))
  617. return -EINVAL;
  618. if (st->dc_dc_mode == AD5758_DCDC_MODE_DPC_VOLTAGE) {
  619. ret = device_property_read_u32_array(&st->spi->dev,
  620. "adi,range-microvolt",
  621. tmparray, 2);
  622. if (ret) {
  623. dev_err(&st->spi->dev,
  624. "Missing \"range-microvolt\" property\n");
  625. return ret;
  626. }
  627. range = ad5758_voltage_range;
  628. size = ARRAY_SIZE(ad5758_voltage_range);
  629. } else {
  630. ret = device_property_read_u32_array(&st->spi->dev,
  631. "adi,range-microamp",
  632. tmparray, 2);
  633. if (ret) {
  634. dev_err(&st->spi->dev,
  635. "Missing \"range-microamp\" property\n");
  636. return ret;
  637. }
  638. range = ad5758_current_range;
  639. size = ARRAY_SIZE(ad5758_current_range);
  640. }
  641. ret = ad5758_find_out_range(st, range, size, tmparray[0], tmparray[1]);
  642. if (ret) {
  643. dev_err(&st->spi->dev, "range invalid\n");
  644. return ret;
  645. }
  646. ret = device_property_read_u32(&st->spi->dev, "adi,slew-time-us", &tmp);
  647. if (ret) {
  648. dev_dbg(&st->spi->dev, "Missing \"slew-time-us\" property\n");
  649. st->slew_time = 0;
  650. } else {
  651. st->slew_time = tmp;
  652. }
  653. return 0;
  654. }
  655. static int ad5758_init(struct ad5758_state *st)
  656. {
  657. int regval, ret;
  658. /* Disable CRC checks */
  659. ret = ad5758_crc_disable(st);
  660. if (ret < 0)
  661. return ret;
  662. /* Perform a software reset */
  663. ret = ad5758_soft_reset(st);
  664. if (ret < 0)
  665. return ret;
  666. /* Disable CRC checks */
  667. ret = ad5758_crc_disable(st);
  668. if (ret < 0)
  669. return ret;
  670. /* Perform a calibration memory refresh */
  671. ret = ad5758_calib_mem_refresh(st);
  672. if (ret < 0)
  673. return ret;
  674. regval = ad5758_spi_reg_read(st, AD5758_DIGITAL_DIAG_RESULTS);
  675. if (regval < 0)
  676. return regval;
  677. /* Clear all the error flags */
  678. ret = ad5758_spi_reg_write(st, AD5758_DIGITAL_DIAG_RESULTS, regval);
  679. if (ret < 0)
  680. return ret;
  681. /* Set the dc-to-dc current limit */
  682. ret = ad5758_set_dc_dc_ilim(st, st->dc_dc_ilim);
  683. if (ret < 0)
  684. return ret;
  685. /* Configure the dc-to-dc controller mode */
  686. ret = ad5758_set_dc_dc_conv_mode(st, st->dc_dc_mode);
  687. if (ret < 0)
  688. return ret;
  689. /* Configure the output range */
  690. ret = ad5758_set_out_range(st, st->out_range.reg);
  691. if (ret < 0)
  692. return ret;
  693. /* Enable Slew Rate Control, set the slew rate clock and step */
  694. if (st->slew_time) {
  695. ret = ad5758_slew_rate_config(st);
  696. if (ret < 0)
  697. return ret;
  698. }
  699. /* Enable the VIOUT fault protection switch (FPS is closed) */
  700. ret = ad5758_fault_prot_switch_en(st, 1);
  701. if (ret < 0)
  702. return ret;
  703. /* Power up the DAC and internal (INT) amplifiers */
  704. ret = ad5758_internal_buffers_en(st, 1);
  705. if (ret < 0)
  706. return ret;
  707. /* Enable VIOUT */
  708. return ad5758_spi_write_mask(st, AD5758_DAC_CONFIG,
  709. AD5758_DAC_CONFIG_OUT_EN_MSK,
  710. AD5758_DAC_CONFIG_OUT_EN_MODE(1));
  711. }
  712. static int ad5758_probe(struct spi_device *spi)
  713. {
  714. struct ad5758_state *st;
  715. struct iio_dev *indio_dev;
  716. int ret;
  717. indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
  718. if (!indio_dev)
  719. return -ENOMEM;
  720. st = iio_priv(indio_dev);
  721. spi_set_drvdata(spi, indio_dev);
  722. st->spi = spi;
  723. mutex_init(&st->lock);
  724. indio_dev->dev.parent = &spi->dev;
  725. indio_dev->name = spi_get_device_id(spi)->name;
  726. indio_dev->info = &ad5758_info;
  727. indio_dev->modes = INDIO_DIRECT_MODE;
  728. indio_dev->num_channels = 1;
  729. ret = ad5758_parse_dt(st);
  730. if (ret < 0)
  731. return ret;
  732. if (st->dc_dc_mode == AD5758_DCDC_MODE_DPC_VOLTAGE)
  733. indio_dev->channels = ad5758_voltage_ch;
  734. else
  735. indio_dev->channels = ad5758_current_ch;
  736. ret = ad5758_init(st);
  737. if (ret < 0) {
  738. dev_err(&spi->dev, "AD5758 init failed\n");
  739. return ret;
  740. }
  741. return devm_iio_device_register(&st->spi->dev, indio_dev);
  742. }
  743. static const struct spi_device_id ad5758_id[] = {
  744. { "ad5758", 0 },
  745. {}
  746. };
  747. MODULE_DEVICE_TABLE(spi, ad5758_id);
  748. static struct spi_driver ad5758_driver = {
  749. .driver = {
  750. .name = KBUILD_MODNAME,
  751. },
  752. .probe = ad5758_probe,
  753. .id_table = ad5758_id,
  754. };
  755. module_spi_driver(ad5758_driver);
  756. MODULE_AUTHOR("Stefan Popa <stefan.popa@analog.com>");
  757. MODULE_DESCRIPTION("Analog Devices AD5758 DAC");
  758. MODULE_LICENSE("GPL v2");