174aeac4dSJohannes Thumshirn /* 274aeac4dSJohannes Thumshirn * MEN 16z188 Analog to Digial Converter 374aeac4dSJohannes Thumshirn * 474aeac4dSJohannes Thumshirn * Copyright (C) 2014 MEN Mikroelektronik GmbH (www.men.de) 574aeac4dSJohannes Thumshirn * Author: Johannes Thumshirn <johannes.thumshirn@men.de> 674aeac4dSJohannes Thumshirn * 774aeac4dSJohannes Thumshirn * This program is free software; you can redistribute it and/or modify it 874aeac4dSJohannes Thumshirn * under the terms of the GNU General Public License as published by the Free 974aeac4dSJohannes Thumshirn * Software Foundation; version 2 of the License. 1074aeac4dSJohannes Thumshirn */ 1174aeac4dSJohannes Thumshirn 1274aeac4dSJohannes Thumshirn #include <linux/kernel.h> 1374aeac4dSJohannes Thumshirn #include <linux/module.h> 1474aeac4dSJohannes Thumshirn #include <linux/mcb.h> 1535272754SJohannes Thumshirn #include <linux/io.h> 1674aeac4dSJohannes Thumshirn #include <linux/iio/iio.h> 1774aeac4dSJohannes Thumshirn 1874aeac4dSJohannes Thumshirn #define Z188_ADC_MAX_CHAN 8 1974aeac4dSJohannes Thumshirn #define Z188_ADC_GAIN 0x0700000 2074aeac4dSJohannes Thumshirn #define Z188_MODE_VOLTAGE BIT(27) 2174aeac4dSJohannes Thumshirn #define Z188_CFG_AUTO 0x1 2274aeac4dSJohannes Thumshirn #define Z188_CTRL_REG 0x40 2374aeac4dSJohannes Thumshirn 2474aeac4dSJohannes Thumshirn #define ADC_DATA(x) (((x) >> 2) & 0x7ffffc) 2574aeac4dSJohannes Thumshirn #define ADC_OVR(x) ((x) & 0x1) 2674aeac4dSJohannes Thumshirn 2774aeac4dSJohannes Thumshirn struct z188_adc { 2874aeac4dSJohannes Thumshirn struct resource *mem; 2974aeac4dSJohannes Thumshirn void __iomem *base; 3074aeac4dSJohannes Thumshirn }; 3174aeac4dSJohannes Thumshirn 3274aeac4dSJohannes Thumshirn #define Z188_ADC_CHANNEL(idx) { \ 3374aeac4dSJohannes Thumshirn .type = IIO_VOLTAGE, \ 3474aeac4dSJohannes Thumshirn .indexed = 1, \ 3574aeac4dSJohannes Thumshirn .channel = (idx), \ 3674aeac4dSJohannes Thumshirn .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 3774aeac4dSJohannes Thumshirn } 3874aeac4dSJohannes Thumshirn 3974aeac4dSJohannes Thumshirn static const struct iio_chan_spec z188_adc_iio_channels[] = { 4074aeac4dSJohannes Thumshirn Z188_ADC_CHANNEL(0), 4174aeac4dSJohannes Thumshirn Z188_ADC_CHANNEL(1), 4274aeac4dSJohannes Thumshirn Z188_ADC_CHANNEL(2), 4374aeac4dSJohannes Thumshirn Z188_ADC_CHANNEL(3), 4474aeac4dSJohannes Thumshirn Z188_ADC_CHANNEL(4), 4574aeac4dSJohannes Thumshirn Z188_ADC_CHANNEL(5), 4674aeac4dSJohannes Thumshirn Z188_ADC_CHANNEL(6), 4774aeac4dSJohannes Thumshirn Z188_ADC_CHANNEL(7), 4874aeac4dSJohannes Thumshirn }; 4974aeac4dSJohannes Thumshirn 5074aeac4dSJohannes Thumshirn static int z188_iio_read_raw(struct iio_dev *iio_dev, 5174aeac4dSJohannes Thumshirn struct iio_chan_spec const *chan, 5274aeac4dSJohannes Thumshirn int *val, 5374aeac4dSJohannes Thumshirn int *val2, 5474aeac4dSJohannes Thumshirn long info) 5574aeac4dSJohannes Thumshirn { 5674aeac4dSJohannes Thumshirn struct z188_adc *adc = iio_priv(iio_dev); 5774aeac4dSJohannes Thumshirn int ret; 5874aeac4dSJohannes Thumshirn u16 tmp; 5974aeac4dSJohannes Thumshirn 6074aeac4dSJohannes Thumshirn switch (info) { 6174aeac4dSJohannes Thumshirn case IIO_CHAN_INFO_RAW: 6274aeac4dSJohannes Thumshirn tmp = readw(adc->base + chan->channel * 4); 6374aeac4dSJohannes Thumshirn 6474aeac4dSJohannes Thumshirn if (ADC_OVR(tmp)) { 6574aeac4dSJohannes Thumshirn dev_info(&iio_dev->dev, 6674aeac4dSJohannes Thumshirn "Oversampling error on ADC channel %d\n", 6774aeac4dSJohannes Thumshirn chan->channel); 6874aeac4dSJohannes Thumshirn return -EIO; 6974aeac4dSJohannes Thumshirn } 7074aeac4dSJohannes Thumshirn *val = ADC_DATA(tmp); 7174aeac4dSJohannes Thumshirn ret = IIO_VAL_INT; 7274aeac4dSJohannes Thumshirn break; 7374aeac4dSJohannes Thumshirn default: 7474aeac4dSJohannes Thumshirn ret = -EINVAL; 7574aeac4dSJohannes Thumshirn break; 7674aeac4dSJohannes Thumshirn } 7774aeac4dSJohannes Thumshirn 7874aeac4dSJohannes Thumshirn return ret; 7974aeac4dSJohannes Thumshirn } 8074aeac4dSJohannes Thumshirn 8174aeac4dSJohannes Thumshirn static struct iio_info z188_adc_info = { 8274aeac4dSJohannes Thumshirn .read_raw = &z188_iio_read_raw, 8374aeac4dSJohannes Thumshirn .driver_module = THIS_MODULE, 8474aeac4dSJohannes Thumshirn }; 8574aeac4dSJohannes Thumshirn 8674aeac4dSJohannes Thumshirn static void men_z188_config_channels(void __iomem *addr) 8774aeac4dSJohannes Thumshirn { 8874aeac4dSJohannes Thumshirn int i; 8974aeac4dSJohannes Thumshirn u32 cfg; 9074aeac4dSJohannes Thumshirn u32 ctl; 9174aeac4dSJohannes Thumshirn 9274aeac4dSJohannes Thumshirn ctl = readl(addr + Z188_CTRL_REG); 9374aeac4dSJohannes Thumshirn ctl |= Z188_CFG_AUTO; 9474aeac4dSJohannes Thumshirn writel(ctl, addr + Z188_CTRL_REG); 9574aeac4dSJohannes Thumshirn 9674aeac4dSJohannes Thumshirn for (i = 0; i < Z188_ADC_MAX_CHAN; i++) { 9774aeac4dSJohannes Thumshirn cfg = readl(addr + i); 9874aeac4dSJohannes Thumshirn cfg &= ~Z188_ADC_GAIN; 9974aeac4dSJohannes Thumshirn cfg |= Z188_MODE_VOLTAGE; 10074aeac4dSJohannes Thumshirn writel(cfg, addr + i); 10174aeac4dSJohannes Thumshirn } 10274aeac4dSJohannes Thumshirn } 10374aeac4dSJohannes Thumshirn 10474aeac4dSJohannes Thumshirn static int men_z188_probe(struct mcb_device *dev, 10574aeac4dSJohannes Thumshirn const struct mcb_device_id *id) 10674aeac4dSJohannes Thumshirn { 10774aeac4dSJohannes Thumshirn struct z188_adc *adc; 10874aeac4dSJohannes Thumshirn struct iio_dev *indio_dev; 10974aeac4dSJohannes Thumshirn struct resource *mem; 11074aeac4dSJohannes Thumshirn 11174aeac4dSJohannes Thumshirn indio_dev = devm_iio_device_alloc(&dev->dev, sizeof(struct z188_adc)); 11274aeac4dSJohannes Thumshirn if (!indio_dev) 11374aeac4dSJohannes Thumshirn return -ENOMEM; 11474aeac4dSJohannes Thumshirn 11574aeac4dSJohannes Thumshirn adc = iio_priv(indio_dev); 11674aeac4dSJohannes Thumshirn indio_dev->name = "z188-adc"; 11774aeac4dSJohannes Thumshirn indio_dev->dev.parent = &dev->dev; 11874aeac4dSJohannes Thumshirn indio_dev->info = &z188_adc_info; 11974aeac4dSJohannes Thumshirn indio_dev->modes = INDIO_DIRECT_MODE; 12074aeac4dSJohannes Thumshirn indio_dev->channels = z188_adc_iio_channels; 12174aeac4dSJohannes Thumshirn indio_dev->num_channels = ARRAY_SIZE(z188_adc_iio_channels); 12274aeac4dSJohannes Thumshirn 12374aeac4dSJohannes Thumshirn mem = mcb_request_mem(dev, "z188-adc"); 124*e94f62e7SDan Carpenter if (IS_ERR(mem)) 125*e94f62e7SDan Carpenter return PTR_ERR(mem); 12674aeac4dSJohannes Thumshirn 12774aeac4dSJohannes Thumshirn adc->base = ioremap(mem->start, resource_size(mem)); 12874aeac4dSJohannes Thumshirn if (adc->base == NULL) 12974aeac4dSJohannes Thumshirn goto err; 13074aeac4dSJohannes Thumshirn 13174aeac4dSJohannes Thumshirn men_z188_config_channels(adc->base); 13274aeac4dSJohannes Thumshirn 13374aeac4dSJohannes Thumshirn adc->mem = mem; 13474aeac4dSJohannes Thumshirn mcb_set_drvdata(dev, indio_dev); 13574aeac4dSJohannes Thumshirn 13674aeac4dSJohannes Thumshirn return iio_device_register(indio_dev); 13774aeac4dSJohannes Thumshirn 13874aeac4dSJohannes Thumshirn err: 13974aeac4dSJohannes Thumshirn mcb_release_mem(mem); 14074aeac4dSJohannes Thumshirn return -ENXIO; 14174aeac4dSJohannes Thumshirn } 14274aeac4dSJohannes Thumshirn 14374aeac4dSJohannes Thumshirn static void men_z188_remove(struct mcb_device *dev) 14474aeac4dSJohannes Thumshirn { 14574aeac4dSJohannes Thumshirn struct iio_dev *indio_dev = mcb_get_drvdata(dev); 14674aeac4dSJohannes Thumshirn struct z188_adc *adc = iio_priv(indio_dev); 14774aeac4dSJohannes Thumshirn 14874aeac4dSJohannes Thumshirn iio_device_unregister(indio_dev); 14974aeac4dSJohannes Thumshirn iounmap(adc->base); 15074aeac4dSJohannes Thumshirn mcb_release_mem(adc->mem); 15174aeac4dSJohannes Thumshirn } 15274aeac4dSJohannes Thumshirn 15374aeac4dSJohannes Thumshirn static const struct mcb_device_id men_z188_ids[] = { 15474aeac4dSJohannes Thumshirn { .device = 0xbc }, 15574aeac4dSJohannes Thumshirn }; 15674aeac4dSJohannes Thumshirn MODULE_DEVICE_TABLE(mcb, men_z188_ids); 15774aeac4dSJohannes Thumshirn 15874aeac4dSJohannes Thumshirn static struct mcb_driver men_z188_driver = { 15974aeac4dSJohannes Thumshirn .driver = { 16074aeac4dSJohannes Thumshirn .name = "z188-adc", 16174aeac4dSJohannes Thumshirn .owner = THIS_MODULE, 16274aeac4dSJohannes Thumshirn }, 16374aeac4dSJohannes Thumshirn .probe = men_z188_probe, 16474aeac4dSJohannes Thumshirn .remove = men_z188_remove, 16574aeac4dSJohannes Thumshirn .id_table = men_z188_ids, 16674aeac4dSJohannes Thumshirn }; 16774aeac4dSJohannes Thumshirn module_mcb_driver(men_z188_driver); 16874aeac4dSJohannes Thumshirn 16974aeac4dSJohannes Thumshirn MODULE_AUTHOR("Johannes Thumshirn <johannes.thumshirn@men.de>"); 17074aeac4dSJohannes Thumshirn MODULE_LICENSE("GPL"); 17174aeac4dSJohannes Thumshirn MODULE_DESCRIPTION("IIO ADC driver for MEN 16z188 ADC Core"); 17274aeac4dSJohannes Thumshirn MODULE_ALIAS("mcb:16z188"); 173