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