ARM: mm: Recreate kernel mappings in early_paging_init()
[linux/fpc-iii.git] / drivers / iio / adc / ti-adc081c.c
blobee5f72bffe5a8f993276d480ba5a65e685ada659
1 /*
2 * Copyright (C) 2012 Avionic Design GmbH
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
9 #include <linux/err.h>
10 #include <linux/i2c.h>
11 #include <linux/module.h>
13 #include <linux/iio/iio.h>
14 #include <linux/regulator/consumer.h>
16 struct adc081c {
17 struct i2c_client *i2c;
18 struct regulator *ref;
21 #define REG_CONV_RES 0x00
23 static int adc081c_read_raw(struct iio_dev *iio,
24 struct iio_chan_spec const *channel, int *value,
25 int *shift, long mask)
27 struct adc081c *adc = iio_priv(iio);
28 int err;
30 switch (mask) {
31 case IIO_CHAN_INFO_RAW:
32 err = i2c_smbus_read_word_swapped(adc->i2c, REG_CONV_RES);
33 if (err < 0)
34 return err;
36 *value = (err >> 4) & 0xff;
37 return IIO_VAL_INT;
39 case IIO_CHAN_INFO_SCALE:
40 err = regulator_get_voltage(adc->ref);
41 if (err < 0)
42 return err;
44 *value = err / 1000;
45 *shift = 8;
47 return IIO_VAL_FRACTIONAL_LOG2;
49 default:
50 break;
53 return -EINVAL;
56 static const struct iio_chan_spec adc081c_channel = {
57 .type = IIO_VOLTAGE,
58 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
59 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
62 static const struct iio_info adc081c_info = {
63 .read_raw = adc081c_read_raw,
64 .driver_module = THIS_MODULE,
67 static int adc081c_probe(struct i2c_client *client,
68 const struct i2c_device_id *id)
70 struct iio_dev *iio;
71 struct adc081c *adc;
72 int err;
74 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WORD_DATA))
75 return -ENODEV;
77 iio = devm_iio_device_alloc(&client->dev, sizeof(*adc));
78 if (!iio)
79 return -ENOMEM;
81 adc = iio_priv(iio);
82 adc->i2c = client;
84 adc->ref = devm_regulator_get(&client->dev, "vref");
85 if (IS_ERR(adc->ref))
86 return PTR_ERR(adc->ref);
88 err = regulator_enable(adc->ref);
89 if (err < 0)
90 return err;
92 iio->dev.parent = &client->dev;
93 iio->name = dev_name(&client->dev);
94 iio->modes = INDIO_DIRECT_MODE;
95 iio->info = &adc081c_info;
97 iio->channels = &adc081c_channel;
98 iio->num_channels = 1;
100 err = iio_device_register(iio);
101 if (err < 0)
102 goto regulator_disable;
104 i2c_set_clientdata(client, iio);
106 return 0;
108 regulator_disable:
109 regulator_disable(adc->ref);
111 return err;
114 static int adc081c_remove(struct i2c_client *client)
116 struct iio_dev *iio = i2c_get_clientdata(client);
117 struct adc081c *adc = iio_priv(iio);
119 iio_device_unregister(iio);
120 regulator_disable(adc->ref);
122 return 0;
125 static const struct i2c_device_id adc081c_id[] = {
126 { "adc081c", 0 },
129 MODULE_DEVICE_TABLE(i2c, adc081c_id);
131 #ifdef CONFIG_OF
132 static const struct of_device_id adc081c_of_match[] = {
133 { .compatible = "ti,adc081c" },
136 MODULE_DEVICE_TABLE(of, adc081c_of_match);
137 #endif
139 static struct i2c_driver adc081c_driver = {
140 .driver = {
141 .name = "adc081c",
142 .owner = THIS_MODULE,
143 .of_match_table = of_match_ptr(adc081c_of_match),
145 .probe = adc081c_probe,
146 .remove = adc081c_remove,
147 .id_table = adc081c_id,
149 module_i2c_driver(adc081c_driver);
151 MODULE_AUTHOR("Thierry Reding <thierry.reding@avionic-design.de>");
152 MODULE_DESCRIPTION("Texas Instruments ADC081C021/027 driver");
153 MODULE_LICENSE("GPL v2");