iio: dac: Add AD5758 support
Subsystems (2)
PATCH v3 1/2 iio: dac: Add AD5758 support
The AD5758 is a single channel DAC with 16-bit precision which uses the SPI interface that operates at clock rates up to 50MHz.
The output can be configured as voltage or current and is available on a single terminal.
Datasheet:
http://www.analog.com/media/en/technical-documentation/data-sheets/ad5758.pdf
---
Changes in v3:
- AD5758 can be both a current and voltage output DAC. The decision is made based on the DT and the channel type is set during probe.
- dc-dc-mode, range-microvolt and range-microamp are required properties.
- Introduced a slew-time-us property from which slew rate clock and slew rate step are calculated using a best match algorithm.
- Dropped the union from ad5758_state struct.
- Introduced a IIO_CHAN_INFO_OFFSET case part of ad5758_read_raw().
- Added a TODO comment which specifies that CRC is not supported.
- Kept the includes in order and removed the unused ones.
- Removed unused macros and shortened the lengthy ones.
- Renamed AD5758_REG_WRITE to AD5758_WR_FLAG_MSK.
- Added an explanation for enum ad5758_output_range.
- Used bsearch() instead of ad5758_get_array_index().
- Reduced the delays.
- strtobool() -> kstrtobool().
Changes in v2:
- removed unnecessary parenthesis in AD5758_REG_WRITE macro.
- added missing documentation fields of ad5758_state struct.
- changed the type of pwr_down attribute to bool.
- changed ad5758_dc_dc_ilimt[] to ad5758_dc_dc_ilim[].
- ad5758_spi_reg_write() now returns spi_write(st->spi, &st->data[0].d32, sizeof(st->data[0].d32));
- removed unnecessary new line in ad5758_calib_mem_refresh().
- changed the type of the mode parameter in ad5758_set_dc_dc_conv_mode() from unsigned int to enum ad5758_dc_dc_mode.
- removed unnecessary parenthesis in ad5758_slew_rate_config().
- changed the type of the enable parameter in ad5758_fault_prot_switch_en() from unsigned char to bool.
- the same as above, but for ad5758_internal_buffers_en().
- added a missing mutex_unlock() in ad5758_reg_access().
- moved the mutex_unlock() in ad5758_read_raw() and removed the unreachable return.
- returned directly where it was possible in ad5758_write_raw().
- removed the channel, scan_type and scan_index fields.
- in ad5758_parse_dt(), added missing "\n", and specified what the default mode actually is.
- returned directly at the end of ad5758_init().
- in ad5758_probe() used device managed for registering the device and returned directly without the error message.
MAINTAINERS | 7 + drivers/iio/dac/Kconfig | 10 + drivers/iio/dac/Makefile | 1 + drivers/iio/dac/ad5758.c | 899 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 917 insertions(+) create mode 100644 drivers/iio/dac/ad5758.c
MAINTAINERS +7 -0
@@ -796,6 +796,13 @@ M: Michael Hanselmann <[email protected]> S: Supported F: drivers/macintosh/ams/ +ANALOG DEVICES INC AD5758 DRIVER +M: Stefan Popa <[email protected]> +L: [email protected] +W: http://ez.analog.com/community/linux-device-drivers +S: Supported +F: drivers/iio/dac/ad5758.c + ANALOG DEVICES INC AD5686 DRIVER M: Stefan Popa <[email protected]> L: [email protected]
drivers/iio/dac/Kconfig +10 -0
@@ -167,6 +167,16 @@ config AD5755 To compile this driver as a module, choose M here: the module will be called ad5755. +config AD5758 + tristate "Analog Devices AD5758 DAC driver" + depends on SPI_MASTER + help + Say yes here to build support for Analog Devices AD5758 single channel + Digital to Analog Converter. + + To compile this driver as a module, choose M here: the + module will be called ad5758. + config AD5761 tristate "Analog Devices AD5761/61R/21/21R DAC driver" depends on SPI_MASTER
drivers/iio/dac/Makefile +1 -0
@@ -16,6 +16,7 @@ obj-$(CONFIG_AD5592R_BASE) += ad5592r-base.o obj-$(CONFIG_AD5592R) += ad5592r.o obj-$(CONFIG_AD5593R) += ad5593r.o obj-$(CONFIG_AD5755) += ad5755.o +obj-$(CONFIG_AD5755) += ad5758.o obj-$(CONFIG_AD5761) += ad5761.o obj-$(CONFIG_AD5764) += ad5764.o obj-$(CONFIG_AD5791) += ad5791.o
drivers/iio/dac/ad5758.c new file +899 -0
899 lines not on this page · raw message
Signature · 1 line
2.7.4
kbuild test robot
build botI love your patch! Perhaps something to improve:
Hi Stefan,
I love your patch! Perhaps something to improve:
[auto build test WARNING on iio/togreg]
[also build test WARNING on v4.18-rc2 next-20180628]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]
url: https://github.com/0day-ci/linux/commits/Stefan-Popa/iio-dac-Add-AD5758-support/20180628-205028 base: https://git.kernel.org/pub/scm/linux/kernel/git/jic23/iio.git togreg reproduce: # apt-get install sparse make ARCH=x86_64 allmodconfig make C=1 CF=-D__CHECK_ENDIAN__
sparse warnings: (new ones prefixed by >>)
>> drivers/iio/dac/ad5758.c:402:27: sparse: constant 65535000000 is so big it is long drivers/iio/dac/ad5758.c:413:34: sparse: constant 65535000000 is so big it is long >> drivers/iio/dac/ad5758.c:237:5: sparse: symbol 'cmpfunc' was not declared. Should it be static?
Please review and possibly fold the followup patch.
vim +402 drivers/iio/dac/ad5758.c
236
> 237 int cmpfunc(const void *a, const void *b)
238 {
239 return (*(int *)a - *(int *)b);
240 }
241
242 static int ad5758_find_closest_match(const int *array,
243 unsigned int size, int val)
244 {
245 int i;
246
247 for (i = 0; i < size; i++) {
248 if (val <= array[i])
249 return i;
250 }
251
252 return size - 1;
253 }
254
255 static int ad5758_wait_for_task_complete(struct ad5758_state *st,
256 unsigned int reg,
257 unsigned int mask)
258 {
259 unsigned int timeout;
260 int ret;
261
262 timeout = 10;
263 do {
264 ret = ad5758_spi_reg_read(st, reg);
265 if (ret < 0)
266 return ret;
267
268 if (!(ret & mask))
269 return 0;
270
271 udelay(100);
272 } while (--timeout);
273
274 dev_err(&st->spi->dev,
275 "Error reading bit 0x%x in 0x%x register\n", mask, reg);
276
277 return -EIO;
278 }
279
280 static int ad5758_calib_mem_refresh(struct ad5758_state *st)
281 {
282 int ret;
283
284 ret = ad5758_spi_reg_write(st, AD5758_KEY,
285 AD5758_KEY_CODE_CALIB_MEM_REFRESH);
286 if (ret < 0) {
287 dev_err(&st->spi->dev,
288 "Failed to initiate a calibration memory refresh\n");
289 return ret;
290 }
291
292 /* Wait to allow time for the internal calibrations to complete */
293 return ad5758_wait_for_task_complete(st, AD5758_DIGITAL_DIAG_RESULTS,
294 AD5758_CAL_MEM_UNREFRESHED_MSK);
295 }
296
297 static int ad5758_soft_reset(struct ad5758_state *st)
298 {
299 int ret;
300
301 ret = ad5758_spi_reg_write(st, AD5758_KEY, AD5758_KEY_CODE_RESET_1);
302 if (ret < 0)
303 return ret;
304
305 ret = ad5758_spi_reg_write(st, AD5758_KEY, AD5758_KEY_CODE_RESET_2);
306
307 /* Perform a software reset and wait 100us */
308 udelay(100);
309
310 return ret;
311 }
312
313 static int ad5758_set_dc_dc_conv_mode(struct ad5758_state *st,
314 enum ad5758_dc_dc_mode mode)
315 {
316 int ret;
317
318 ret = ad5758_spi_write_mask(st, AD5758_DCDC_CONFIG1,
319 AD5758_DCDC_CONFIG1_DCDC_MODE_MSK,
320 AD5758_DCDC_CONFIG1_DCDC_MODE_MODE(mode));
321 if (ret < 0)
322 return ret;
323
324 /*
325 * Poll the BUSY_3WI bit in the DCDC_CONFIG2 register until it is 0.
326 * This allows the 3-wire interface communication to complete.
327 */
328 ret = ad5758_wait_for_task_complete(st, AD5758_DCDC_CONFIG2,
329 AD5758_DCDC_CONFIG2_BUSY_3WI_MSK);
330 if (ret < 0)
331 return ret;
332
333 st->dc_dc_mode = mode;
334
335 return ret;
336 }
337
338 static int ad5758_set_dc_dc_ilim(struct ad5758_state *st, unsigned int ilim)
339 {
340 int ret;
341
342 ret = ad5758_spi_write_mask(st, AD5758_DCDC_CONFIG2,
343 AD5758_DCDC_CONFIG2_ILIMIT_MSK,
344 AD5758_DCDC_CONFIG2_ILIMIT_MODE(ilim));
345 if (ret < 0)
346 return ret;
347 /*
348 * Poll the BUSY_3WI bit in the DCDC_CONFIG2 register until it is 0.
349 * This allows the 3-wire interface communication to complete.
350 */
351 return ad5758_wait_for_task_complete(st, AD5758_DCDC_CONFIG2,
352 AD5758_DCDC_CONFIG2_BUSY_3WI_MSK);
353 }
354
355 static int ad5758_slew_rate_set(struct ad5758_state *st,
356 unsigned int sr_clk_idx,
357 unsigned int sr_step_idx)
358 {
359 unsigned int mode;
360 unsigned long int mask;
361 int ret;
362
363 mask = AD5758_DAC_CONFIG_SR_EN_MSK |
364 AD5758_DAC_CONFIG_SR_CLOCK_MSK |
365 AD5758_DAC_CONFIG_SR_STEP_MSK;
366 mode = AD5758_DAC_CONFIG_SR_EN_MODE(1) |
367 AD5758_DAC_CONFIG_SR_STEP_MODE(sr_step_idx) |
368 AD5758_DAC_CONFIG_SR_CLOCK_MODE(sr_clk_idx);
369
370 ret = ad5758_spi_write_mask(st, AD5758_DAC_CONFIG, mask, mode);
371 if (ret < 0)
372 return ret;
373
374 /* Wait to allow time for the internal calibrations to complete */
375 return ad5758_wait_for_task_complete(st, AD5758_DIGITAL_DIAG_RESULTS,
376 AD5758_CAL_MEM_UNREFRESHED_MSK);
377 }
378
379 static int ad5758_slew_rate_config(struct ad5758_state *st)
380 {
381 unsigned int sr_clk_idx, sr_step_idx;
382 int i, res;
383 s64 diff_new, diff_old;
384 u64 sr_step, calc_slew_time;
385
386 sr_clk_idx = 0;
387 sr_step_idx = 0;
388 diff_old = S64_MAX;
389 /*
390 * The slew time can be determined by using the formula:
391 * Slew Time = (Full Scale Out / (Step Size x Update Clk Freq))
392 * where Slew time is expressed in microseconds
393 * Given the desired slew time, the following algorithm determines the
394 * best match for the step size and the update clock frequency.
395 */
396 for (i = 0; i < ARRAY_SIZE(ad5758_sr_clk); i++) {
397 /*
398 * Go through each valid update clock freq and determine a raw
399 * value for the step size by using the formula:
400 * Step Size = Full Scale Out / (Update Clk Freq * Slew Time)
401 */
> 402 sr_step = AD5758_FULL_SCALE_MICRO; 403 do_div(sr_step, ad5758_sr_clk[i]);
404 do_div(sr_step, st->slew_time);
405 /*
406 * After a raw value for step size was determined, find the
407 * closest valid match
408 */
409 res = ad5758_find_closest_match(ad5758_sr_step,
410 ARRAY_SIZE(ad5758_sr_step),
411 sr_step);
412 /* Calculate the slew time */
413 calc_slew_time = AD5758_FULL_SCALE_MICRO;
414 do_div(calc_slew_time, ad5758_sr_step[res]);
415 do_div(calc_slew_time, ad5758_sr_clk[i]);
416 /*
417 * Determine with how many microseconds the calculated slew time
418 * is different from the desired slew time and store the diff
419 * for the next iteration
420 */
421 diff_new = abs(st->slew_time - calc_slew_time);
422 if (diff_new < diff_old) {
423 diff_old = diff_new;
424 sr_clk_idx = i;
425 sr_step_idx = res;
426 }
427 }
428
429 return ad5758_slew_rate_set(st, sr_clk_idx, sr_step_idx);
430 }
431
---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation
kbuild test robot
build bot[RFC PATCH] iio: dac: cmpfunc() can be static
In reply to Stefan PopaFixes: 2c1562568d29 ("iio: dac: Add AD5758 support")
--- ad5758.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/dac/ad5758.c +1 -1
@@ -234,7 +234,7 @@ static int ad5758_spi_write_mask(struct ad5758_state *st,return ad5758_spi_reg_write (st, addr, regval); } -int cmpfunc (const void *a,const void *b) +static int cmpfunc (const void *a,const void *b) {return (*(int *)a - *(int *)b); }
Quoted 11 lines · Stefan Popa
The AD5758 is a single channel DAC with 16-bit precision which uses the SPI interface that operates at clock rates up to 50MHz.
The output can be configured as voltage or current and is available on a single terminal.
Datasheet:
http://www.analog.com/media/en/technical-documentation/data-sheets/ad5758.pdfSigned-off-by Stefan Popa <[email protected]>---
[..]
Quoted 3 lines
drivers/iio/dac/ad5758.c new file +3
-0+static const int ad5758_dc_dc_ilim[6 ] = { +150000 ,200000 ,250000 ,300000 ,350000 ,400000 +};
[..]
drivers/iio/dac/ad5758.c line 240
Quoted 4 lines
drivers/iio/dac/ad5758.c new file +4
-0+int cmpfunc (const void *a,const void *b) +{ +return (*(int *)a - *(int *)b); +}
Since Kbuild hit a Sparse error in this function, I would also like to add that the above implementation of cmpfunc() is not safe.
For eg: https://wandbox.org/permlink/MqGnA3nVg7eAN4I7
The following will be safer:
static int cmpfunc(const void *a, const void *b)
{
int arg1 = *(const int*)a;
int arg2 = *(const int*)b;
return (arg1 > arg2) - (arg1 < arg2);
}[..]
drivers/iio/dac/ad5758.c line 708
Quoted 3 lines
drivers/iio/dac/ad5758.c new file +3
-0+ index = (int *)bsearch (&tmp, ad5758_dc_dc_ilim, +ARRAY_SIZE (ad5758_dc_dc_ilim), +sizeof (int ), cmpfunc);
Thanks.
Signature · 4 lines
Himanshu Jha Undergraduate Student Department of Electronics & Communication Guru Tegh Bahadur Institute of Technology
Quoted 36 lines · Himanshu Jha
Quoted 11 lines · Stefan Popa
The AD5758 is a single channel DAC with 16-bit precision which uses the SPI interface that operates at clock rates up to 50MHz.
The output can be configured as voltage or current and is available on a single terminal.
Datasheet:
http://www.analog.com/media/en/technical-documentation/data-sheets/ad5758.pdfSigned-off-by Stefan Popa <[email protected]>---
[..]
Quoted 3 lines
drivers/iio/dac/ad5758.c new file +3
-0+static const int ad5758_dc_dc_ilim[6 ] = { +150000 ,200000 ,250000 ,300000 ,350000 ,400000 +};[..]
drivers/iio/dac/ad5758.c line 240
Quoted 4 lines
drivers/iio/dac/ad5758.c new file +4
-0+int cmpfunc (const void *a,const void *b) +{ +return (*(int *)a - *(int *)b); +}Since
Kbuildhit a Sparse error in this function, I would also like to add that the above implementation ofcmpfunc()is not safe.For eg: https://wandbox.org/permlink/MqGnA3nVg7eAN4I7
The following will be safer:
static int
cmpfunc(const void *a, const void *b){ int arg1 = *(const int*)a; int arg2 = *(const int*)b; return (arg1 > arg2) - (arg1 < arg2); }
Quoted 4 lines
[..]
drivers/iio/dac/ad5758.c line 708
Quoted 3 lines
drivers/iio/dac/ad5758.c new file +3
-0+ index = (int *)bsearch (&tmp, ad5758_dc_dc_ilim, +ARRAY_SIZE (ad5758_dc_dc_ilim), +sizeof (int ), cmpfunc);
I somehow realized that my argument is useless since we would not have large integer subtraction, which could lead to signed integer overflow(UB) and hence cause problems(as evident in the eg link I posted).
Sorry Stefan :(
Signature · 4 lines
Himanshu Jha Undergraduate Student Department of Electronics & Communication Guru Tegh Bahadur Institute of Technology