Lines Matching refs:st
186 static int ad5758_spi_reg_read(struct ad5758_state *st, unsigned int addr) in ad5758_spi_reg_read() argument
190 .tx_buf = &st->d32[0], in ad5758_spi_reg_read()
194 .tx_buf = &st->d32[1], in ad5758_spi_reg_read()
195 .rx_buf = &st->d32[2], in ad5758_spi_reg_read()
201 st->d32[0] = cpu_to_be32( in ad5758_spi_reg_read()
204 st->d32[1] = cpu_to_be32(AD5758_WR_FLAG_MSK(AD5758_NOP) << 24); in ad5758_spi_reg_read()
206 ret = spi_sync_transfer(st->spi, t, ARRAY_SIZE(t)); in ad5758_spi_reg_read()
210 return (be32_to_cpu(st->d32[2]) >> 8) & 0xFFFF; in ad5758_spi_reg_read()
213 static int ad5758_spi_reg_write(struct ad5758_state *st, in ad5758_spi_reg_write() argument
217 st->d32[0] = cpu_to_be32((AD5758_WR_FLAG_MSK(addr) << 24) | in ad5758_spi_reg_write()
220 return spi_write(st->spi, &st->d32[0], sizeof(st->d32[0])); in ad5758_spi_reg_write()
223 static int ad5758_spi_write_mask(struct ad5758_state *st, in ad5758_spi_write_mask() argument
230 regval = ad5758_spi_reg_read(st, addr); in ad5758_spi_write_mask()
237 return ad5758_spi_reg_write(st, addr, regval); in ad5758_spi_write_mask()
258 static int ad5758_wait_for_task_complete(struct ad5758_state *st, in ad5758_wait_for_task_complete() argument
267 ret = ad5758_spi_reg_read(st, reg); in ad5758_wait_for_task_complete()
277 dev_err(&st->spi->dev, in ad5758_wait_for_task_complete()
283 static int ad5758_calib_mem_refresh(struct ad5758_state *st) in ad5758_calib_mem_refresh() argument
287 ret = ad5758_spi_reg_write(st, AD5758_KEY, in ad5758_calib_mem_refresh()
290 dev_err(&st->spi->dev, in ad5758_calib_mem_refresh()
296 return ad5758_wait_for_task_complete(st, AD5758_DIGITAL_DIAG_RESULTS, in ad5758_calib_mem_refresh()
300 static int ad5758_soft_reset(struct ad5758_state *st) in ad5758_soft_reset() argument
304 ret = ad5758_spi_reg_write(st, AD5758_KEY, AD5758_KEY_CODE_RESET_1); in ad5758_soft_reset()
308 ret = ad5758_spi_reg_write(st, AD5758_KEY, AD5758_KEY_CODE_RESET_2); in ad5758_soft_reset()
316 static int ad5758_set_dc_dc_conv_mode(struct ad5758_state *st, in ad5758_set_dc_dc_conv_mode() argument
326 ret = ad5758_spi_write_mask(st, AD5758_ADC_CONFIG, in ad5758_set_dc_dc_conv_mode()
333 ret = ad5758_spi_write_mask(st, AD5758_DCDC_CONFIG1, in ad5758_set_dc_dc_conv_mode()
343 ret = ad5758_wait_for_task_complete(st, AD5758_DCDC_CONFIG2, in ad5758_set_dc_dc_conv_mode()
348 st->dc_dc_mode = mode; in ad5758_set_dc_dc_conv_mode()
353 static int ad5758_set_dc_dc_ilim(struct ad5758_state *st, unsigned int ilim) in ad5758_set_dc_dc_ilim() argument
357 ret = ad5758_spi_write_mask(st, AD5758_DCDC_CONFIG2, in ad5758_set_dc_dc_ilim()
366 return ad5758_wait_for_task_complete(st, AD5758_DCDC_CONFIG2, in ad5758_set_dc_dc_ilim()
370 static int ad5758_slew_rate_set(struct ad5758_state *st, in ad5758_slew_rate_set() argument
385 ret = ad5758_spi_write_mask(st, AD5758_DAC_CONFIG, mask, mode); in ad5758_slew_rate_set()
390 return ad5758_wait_for_task_complete(st, AD5758_DIGITAL_DIAG_RESULTS, in ad5758_slew_rate_set()
394 static int ad5758_slew_rate_config(struct ad5758_state *st) in ad5758_slew_rate_config() argument
419 do_div(sr_step, st->slew_time); in ad5758_slew_rate_config()
436 diff_new = abs(st->slew_time - calc_slew_time); in ad5758_slew_rate_config()
444 return ad5758_slew_rate_set(st, sr_clk_idx, sr_step_idx); in ad5758_slew_rate_config()
447 static int ad5758_set_out_range(struct ad5758_state *st, int range) in ad5758_set_out_range() argument
451 ret = ad5758_spi_write_mask(st, AD5758_DAC_CONFIG, in ad5758_set_out_range()
458 return ad5758_wait_for_task_complete(st, AD5758_DIGITAL_DIAG_RESULTS, in ad5758_set_out_range()
462 static int ad5758_internal_buffers_en(struct ad5758_state *st, bool enable) in ad5758_internal_buffers_en() argument
466 ret = ad5758_spi_write_mask(st, AD5758_DAC_CONFIG, in ad5758_internal_buffers_en()
473 return ad5758_wait_for_task_complete(st, AD5758_DIGITAL_DIAG_RESULTS, in ad5758_internal_buffers_en()
477 static int ad5758_reset(struct ad5758_state *st) in ad5758_reset() argument
479 if (st->gpio_reset) { in ad5758_reset()
480 gpiod_set_value(st->gpio_reset, 0); in ad5758_reset()
482 gpiod_set_value(st->gpio_reset, 1); in ad5758_reset()
488 return ad5758_soft_reset(st); in ad5758_reset()
497 struct ad5758_state *st = iio_priv(indio_dev); in ad5758_reg_access() local
500 mutex_lock(&st->lock); in ad5758_reg_access()
502 ret = ad5758_spi_reg_read(st, reg); in ad5758_reg_access()
504 mutex_unlock(&st->lock); in ad5758_reg_access()
511 ret = ad5758_spi_reg_write(st, reg, writeval); in ad5758_reg_access()
513 mutex_unlock(&st->lock); in ad5758_reg_access()
522 struct ad5758_state *st = iio_priv(indio_dev); in ad5758_read_raw() local
527 mutex_lock(&st->lock); in ad5758_read_raw()
528 ret = ad5758_spi_reg_read(st, AD5758_DAC_INPUT); in ad5758_read_raw()
529 mutex_unlock(&st->lock); in ad5758_read_raw()
536 min = st->out_range.min; in ad5758_read_raw()
537 max = st->out_range.max; in ad5758_read_raw()
542 min = st->out_range.min; in ad5758_read_raw()
543 max = st->out_range.max; in ad5758_read_raw()
555 struct ad5758_state *st = iio_priv(indio_dev); in ad5758_write_raw() local
560 mutex_lock(&st->lock); in ad5758_write_raw()
561 ret = ad5758_spi_reg_write(st, AD5758_DAC_INPUT, val); in ad5758_write_raw()
562 mutex_unlock(&st->lock); in ad5758_write_raw()
574 struct ad5758_state *st = iio_priv(indio_dev); in ad5758_read_powerdown() local
576 return sysfs_emit(buf, "%d\n", st->pwr_down); in ad5758_read_powerdown()
584 struct ad5758_state *st = iio_priv(indio_dev); in ad5758_write_powerdown() local
594 mutex_lock(&st->lock); in ad5758_write_powerdown()
605 ret = ad5758_spi_write_mask(st, AD5758_DAC_CONFIG, in ad5758_write_powerdown()
611 st->pwr_down = pwr_down; in ad5758_write_powerdown()
614 mutex_unlock(&st->lock); in ad5758_write_powerdown()
665 static int ad5758_crc_disable(struct ad5758_state *st) in ad5758_crc_disable() argument
670 st->d32[0] = cpu_to_be32(mask); in ad5758_crc_disable()
672 return spi_write(st->spi, &st->d32[0], 4); in ad5758_crc_disable()
675 static int ad5758_find_out_range(struct ad5758_state *st, in ad5758_find_out_range() argument
684 st->out_range.reg = range[i].reg; in ad5758_find_out_range()
685 st->out_range.min = range[i].min; in ad5758_find_out_range()
686 st->out_range.max = range[i].max; in ad5758_find_out_range()
695 static int ad5758_parse_dt(struct ad5758_state *st) in ad5758_parse_dt() argument
701 st->dc_dc_ilim = 0; in ad5758_parse_dt()
702 ret = device_property_read_u32(&st->spi->dev, in ad5758_parse_dt()
705 dev_dbg(&st->spi->dev, in ad5758_parse_dt()
712 dev_dbg(&st->spi->dev, "dc-dc-ilim out of range\n"); in ad5758_parse_dt()
714 st->dc_dc_ilim = index - ad5758_dc_dc_ilim; in ad5758_parse_dt()
717 ret = device_property_read_u32(&st->spi->dev, "adi,dc-dc-mode", in ad5758_parse_dt()
718 &st->dc_dc_mode); in ad5758_parse_dt()
720 dev_err(&st->spi->dev, "Missing \"dc-dc-mode\" property\n"); in ad5758_parse_dt()
724 if (!ad5758_is_valid_mode(st->dc_dc_mode)) in ad5758_parse_dt()
727 if (st->dc_dc_mode == AD5758_DCDC_MODE_DPC_VOLTAGE) { in ad5758_parse_dt()
728 ret = device_property_read_u32_array(&st->spi->dev, in ad5758_parse_dt()
732 dev_err(&st->spi->dev, in ad5758_parse_dt()
739 ret = device_property_read_u32_array(&st->spi->dev, in ad5758_parse_dt()
743 dev_err(&st->spi->dev, in ad5758_parse_dt()
751 ret = ad5758_find_out_range(st, range, size, tmparray[0], tmparray[1]); in ad5758_parse_dt()
753 dev_err(&st->spi->dev, "range invalid\n"); in ad5758_parse_dt()
757 ret = device_property_read_u32(&st->spi->dev, "adi,slew-time-us", &tmp); in ad5758_parse_dt()
759 dev_dbg(&st->spi->dev, "Missing \"slew-time-us\" property\n"); in ad5758_parse_dt()
760 st->slew_time = 0; in ad5758_parse_dt()
762 st->slew_time = tmp; in ad5758_parse_dt()
768 static int ad5758_init(struct ad5758_state *st) in ad5758_init() argument
772 st->gpio_reset = devm_gpiod_get_optional(&st->spi->dev, "reset", in ad5758_init()
774 if (IS_ERR(st->gpio_reset)) in ad5758_init()
775 return PTR_ERR(st->gpio_reset); in ad5758_init()
778 ret = ad5758_crc_disable(st); in ad5758_init()
783 ret = ad5758_reset(st); in ad5758_init()
788 ret = ad5758_crc_disable(st); in ad5758_init()
793 ret = ad5758_calib_mem_refresh(st); in ad5758_init()
797 regval = ad5758_spi_reg_read(st, AD5758_DIGITAL_DIAG_RESULTS); in ad5758_init()
802 ret = ad5758_spi_reg_write(st, AD5758_DIGITAL_DIAG_RESULTS, regval); in ad5758_init()
807 ret = ad5758_set_dc_dc_ilim(st, st->dc_dc_ilim); in ad5758_init()
812 ret = ad5758_set_dc_dc_conv_mode(st, st->dc_dc_mode); in ad5758_init()
817 ret = ad5758_set_out_range(st, st->out_range.reg); in ad5758_init()
822 if (st->slew_time) { in ad5758_init()
823 ret = ad5758_slew_rate_config(st); in ad5758_init()
829 ret = ad5758_internal_buffers_en(st, 1); in ad5758_init()
834 return ad5758_spi_write_mask(st, AD5758_DAC_CONFIG, in ad5758_init()
841 struct ad5758_state *st; in ad5758_probe() local
845 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st)); in ad5758_probe()
849 st = iio_priv(indio_dev); in ad5758_probe()
852 st->spi = spi; in ad5758_probe()
854 mutex_init(&st->lock); in ad5758_probe()
861 ret = ad5758_parse_dt(st); in ad5758_probe()
865 if (st->dc_dc_mode == AD5758_DCDC_MODE_DPC_VOLTAGE) in ad5758_probe()
870 ret = ad5758_init(st); in ad5758_probe()
876 return devm_iio_device_register(&st->spi->dev, indio_dev); in ad5758_probe()