Lines Matching full:adc

82 static int mcp3911_read(struct mcp3911 *adc, u8 reg, u32 *val, u8 len)  in mcp3911_read()  argument
86 reg = MCP3911_REG_READ(reg, adc->dev_addr); in mcp3911_read()
87 ret = spi_write_then_read(adc->spi, &reg, 1, val, len); in mcp3911_read()
93 dev_dbg(&adc->spi->dev, "reading 0x%x from register 0x%lx\n", *val, in mcp3911_read()
98 static int mcp3911_write(struct mcp3911 *adc, u8 reg, u32 val, u8 len) in mcp3911_write() argument
100 dev_dbg(&adc->spi->dev, "writing 0x%x to register 0x%x\n", val, reg); in mcp3911_write()
104 val |= MCP3911_REG_WRITE(reg, adc->dev_addr); in mcp3911_write()
106 return spi_write(adc->spi, &val, len + 1); in mcp3911_write()
109 static int mcp3911_update(struct mcp3911 *adc, u8 reg, u32 mask, in mcp3911_update() argument
115 ret = mcp3911_read(adc, reg, &tmp, len); in mcp3911_update()
121 return mcp3911_write(adc, reg, val, len); in mcp3911_update()
158 struct mcp3911 *adc = iio_priv(indio_dev); in mcp3911_read_raw() local
161 mutex_lock(&adc->lock); in mcp3911_read_raw()
164 ret = mcp3911_read(adc, in mcp3911_read_raw()
175 ret = mcp3911_read(adc, in mcp3911_read_raw()
183 ret = mcp3911_read(adc, MCP3911_REG_CONFIG, val, 2); in mcp3911_read_raw()
193 if (adc->vref) { in mcp3911_read_raw()
194 ret = regulator_get_voltage(adc->vref); in mcp3911_read_raw()
220 mutex_unlock(&adc->lock); in mcp3911_read_raw()
228 struct mcp3911 *adc = iio_priv(indio_dev); in mcp3911_write_raw() local
231 mutex_lock(&adc->lock); in mcp3911_write_raw()
240 ret = mcp3911_write(adc, MCP3911_OFFCAL(channel->channel), val, in mcp3911_write_raw()
246 ret = mcp3911_update(adc, MCP3911_REG_STATUSCOM, in mcp3911_write_raw()
255 ret = mcp3911_update(adc, MCP3911_REG_CONFIG, MCP3911_CONFIG_OSR, in mcp3911_write_raw()
264 mutex_unlock(&adc->lock); in mcp3911_write_raw()
297 struct mcp3911 *adc = iio_priv(indio_dev); in mcp3911_trigger_handler() local
300 .tx_buf = &adc->tx_buf, in mcp3911_trigger_handler()
303 .rx_buf = adc->rx_buf, in mcp3911_trigger_handler()
304 .len = sizeof(adc->rx_buf), in mcp3911_trigger_handler()
311 mutex_lock(&adc->lock); in mcp3911_trigger_handler()
312 adc->tx_buf = MCP3911_REG_READ(MCP3911_CHANNEL(0), adc->dev_addr); in mcp3911_trigger_handler()
313 ret = spi_sync_transfer(adc->spi, xfer, ARRAY_SIZE(xfer)); in mcp3911_trigger_handler()
315 dev_warn(&adc->spi->dev, in mcp3911_trigger_handler()
323 adc->scan.channels[i] = get_unaligned_be24(&adc->rx_buf[scan_chan->channel * 3]); in mcp3911_trigger_handler()
326 iio_push_to_buffers_with_timestamp(indio_dev, &adc->scan, in mcp3911_trigger_handler()
329 mutex_unlock(&adc->lock); in mcp3911_trigger_handler()
342 static int mcp3911_config(struct mcp3911 *adc) in mcp3911_config() argument
344 struct device *dev = &adc->spi->dev; in mcp3911_config()
348 ret = device_property_read_u32(dev, "microchip,device-addr", &adc->dev_addr); in mcp3911_config()
355 device_property_read_u32(dev, "device-addr", &adc->dev_addr); in mcp3911_config()
356 if (adc->dev_addr > 3) { in mcp3911_config()
357 dev_err(&adc->spi->dev, in mcp3911_config()
359 adc->dev_addr); in mcp3911_config()
362 dev_dbg(&adc->spi->dev, "use device address %i\n", adc->dev_addr); in mcp3911_config()
364 ret = mcp3911_read(adc, MCP3911_REG_CONFIG, &regval, 2); in mcp3911_config()
369 if (adc->vref) { in mcp3911_config()
370 dev_dbg(&adc->spi->dev, "use external voltage reference\n"); in mcp3911_config()
373 dev_dbg(&adc->spi->dev, in mcp3911_config()
379 if (adc->clki) { in mcp3911_config()
380 dev_dbg(&adc->spi->dev, "use external clock as clocksource\n"); in mcp3911_config()
383 dev_dbg(&adc->spi->dev, in mcp3911_config()
388 ret = mcp3911_write(adc, MCP3911_REG_CONFIG, regval, 2); in mcp3911_config()
392 ret = mcp3911_read(adc, MCP3911_REG_STATUSCOM, &regval, 2); in mcp3911_config()
400 return mcp3911_write(adc, MCP3911_REG_STATUSCOM, regval, 2); in mcp3911_config()
410 struct mcp3911 *adc = iio_trigger_get_drvdata(trig); in mcp3911_set_trigger_state() local
413 enable_irq(adc->spi->irq); in mcp3911_set_trigger_state()
415 disable_irq(adc->spi->irq); in mcp3911_set_trigger_state()
428 struct mcp3911 *adc; in mcp3911_probe() local
431 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*adc)); in mcp3911_probe()
435 adc = iio_priv(indio_dev); in mcp3911_probe()
436 adc->spi = spi; in mcp3911_probe()
438 adc->vref = devm_regulator_get_optional(&adc->spi->dev, "vref"); in mcp3911_probe()
439 if (IS_ERR(adc->vref)) { in mcp3911_probe()
440 if (PTR_ERR(adc->vref) == -ENODEV) { in mcp3911_probe()
441 adc->vref = NULL; in mcp3911_probe()
443 dev_err(&adc->spi->dev, in mcp3911_probe()
445 PTR_ERR(adc->vref)); in mcp3911_probe()
446 return PTR_ERR(adc->vref); in mcp3911_probe()
450 ret = regulator_enable(adc->vref); in mcp3911_probe()
455 mcp3911_cleanup_regulator, adc->vref); in mcp3911_probe()
460 adc->clki = devm_clk_get_enabled(&adc->spi->dev, NULL); in mcp3911_probe()
461 if (IS_ERR(adc->clki)) { in mcp3911_probe()
462 if (PTR_ERR(adc->clki) == -ENOENT) { in mcp3911_probe()
463 adc->clki = NULL; in mcp3911_probe()
465 dev_err(&adc->spi->dev, in mcp3911_probe()
466 "failed to get adc clk (%ld)\n", in mcp3911_probe()
467 PTR_ERR(adc->clki)); in mcp3911_probe()
468 return PTR_ERR(adc->clki); in mcp3911_probe()
472 ret = mcp3911_config(adc); in mcp3911_probe()
476 if (device_property_read_bool(&adc->spi->dev, "microchip,data-ready-hiz")) in mcp3911_probe()
477 ret = mcp3911_update(adc, MCP3911_REG_STATUSCOM, MCP3911_STATUSCOM_DRHIZ, in mcp3911_probe()
480 ret = mcp3911_update(adc, MCP3911_REG_STATUSCOM, MCP3911_STATUSCOM_DRHIZ, in mcp3911_probe()
493 mutex_init(&adc->lock); in mcp3911_probe()
496 adc->trig = devm_iio_trigger_alloc(&spi->dev, "%s-dev%d", in mcp3911_probe()
499 if (!adc->trig) in mcp3911_probe()
502 adc->trig->ops = &mcp3911_trigger_ops; in mcp3911_probe()
503 iio_trigger_set_drvdata(adc->trig, adc); in mcp3911_probe()
504 ret = devm_iio_trigger_register(&spi->dev, adc->trig); in mcp3911_probe()
515 indio_dev->name, adc->trig); in mcp3911_probe()
526 return devm_iio_device_register(&adc->spi->dev, indio_dev); in mcp3911_probe()