ad5758.c 22 KB

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