drivers: power: report battery voltage in AOSP compatible format
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / i2c / busses / i2c-gpio.c
CommitLineData
1c23af90
HS
1/*
2 * Bitbanging I2C bus driver using the GPIO API
3 *
4 * Copyright (C) 2007 Atmel Corporation
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10#include <linux/i2c.h>
11#include <linux/i2c-algo-bit.h>
12#include <linux/i2c-gpio.h>
13#include <linux/init.h>
14#include <linux/module.h>
5a0e3ad6 15#include <linux/slab.h>
1c23af90 16#include <linux/platform_device.h>
8ffaa0f4
JCPV
17#include <linux/gpio.h>
18#include <linux/of_gpio.h>
19#include <linux/of_i2c.h>
20
21struct i2c_gpio_private_data {
22 struct i2c_adapter adap;
23 struct i2c_algo_bit_data bit_data;
24 struct i2c_gpio_platform_data pdata;
25};
1c23af90
HS
26
27/* Toggle SDA by changing the direction of the pin */
28static void i2c_gpio_setsda_dir(void *data, int state)
29{
30 struct i2c_gpio_platform_data *pdata = data;
31
32 if (state)
33 gpio_direction_input(pdata->sda_pin);
34 else
35 gpio_direction_output(pdata->sda_pin, 0);
36}
37
38/*
39 * Toggle SDA by changing the output value of the pin. This is only
40 * valid for pins configured as open drain (i.e. setting the value
41 * high effectively turns off the output driver.)
42 */
43static void i2c_gpio_setsda_val(void *data, int state)
44{
45 struct i2c_gpio_platform_data *pdata = data;
46
47 gpio_set_value(pdata->sda_pin, state);
48}
49
50/* Toggle SCL by changing the direction of the pin. */
51static void i2c_gpio_setscl_dir(void *data, int state)
52{
53 struct i2c_gpio_platform_data *pdata = data;
54
55 if (state)
56 gpio_direction_input(pdata->scl_pin);
57 else
58 gpio_direction_output(pdata->scl_pin, 0);
59}
60
61/*
62 * Toggle SCL by changing the output value of the pin. This is used
63 * for pins that are configured as open drain and for output-only
64 * pins. The latter case will break the i2c protocol, but it will
65 * often work in practice.
66 */
67static void i2c_gpio_setscl_val(void *data, int state)
68{
69 struct i2c_gpio_platform_data *pdata = data;
70
71 gpio_set_value(pdata->scl_pin, state);
72}
73
4d6ceed4 74static int i2c_gpio_getsda(void *data)
1c23af90
HS
75{
76 struct i2c_gpio_platform_data *pdata = data;
77
78 return gpio_get_value(pdata->sda_pin);
79}
80
4d6ceed4 81static int i2c_gpio_getscl(void *data)
1c23af90
HS
82{
83 struct i2c_gpio_platform_data *pdata = data;
84
85 return gpio_get_value(pdata->scl_pin);
86}
87
1b295c83
JD
88static int of_i2c_gpio_get_pins(struct device_node *np,
89 unsigned int *sda_pin, unsigned int *scl_pin)
8ffaa0f4 90{
8ffaa0f4
JCPV
91 if (of_gpio_count(np) < 2)
92 return -ENODEV;
93
1b295c83
JD
94 *sda_pin = of_get_gpio(np, 0);
95 *scl_pin = of_get_gpio(np, 1);
8ffaa0f4 96
1b295c83 97 if (!gpio_is_valid(*sda_pin) || !gpio_is_valid(*scl_pin)) {
8ffaa0f4 98 pr_err("%s: invalid GPIO pins, sda=%d/scl=%d\n",
1b295c83 99 np->full_name, *sda_pin, *scl_pin);
8ffaa0f4
JCPV
100 return -ENODEV;
101 }
102
1b295c83
JD
103 return 0;
104}
105
106static void of_i2c_gpio_get_props(struct device_node *np,
107 struct i2c_gpio_platform_data *pdata)
108{
109 u32 reg;
110
8ffaa0f4
JCPV
111 of_property_read_u32(np, "i2c-gpio,delay-us", &pdata->udelay);
112
113 if (!of_property_read_u32(np, "i2c-gpio,timeout-ms", &reg))
114 pdata->timeout = msecs_to_jiffies(reg);
115
116 pdata->sda_is_open_drain =
117 of_property_read_bool(np, "i2c-gpio,sda-open-drain");
118 pdata->scl_is_open_drain =
119 of_property_read_bool(np, "i2c-gpio,scl-open-drain");
120 pdata->scl_is_output_only =
121 of_property_read_bool(np, "i2c-gpio,scl-output-only");
8ffaa0f4
JCPV
122}
123
0b255e92 124static int i2c_gpio_probe(struct platform_device *pdev)
1c23af90 125{
8ffaa0f4 126 struct i2c_gpio_private_data *priv;
1c23af90
HS
127 struct i2c_gpio_platform_data *pdata;
128 struct i2c_algo_bit_data *bit_data;
129 struct i2c_adapter *adap;
1b295c83 130 unsigned int sda_pin, scl_pin;
1c23af90
HS
131 int ret;
132
1b295c83 133 /* First get the GPIO pins; if it fails, we'll defer the probe. */
8ffaa0f4 134 if (pdev->dev.of_node) {
1b295c83
JD
135 ret = of_i2c_gpio_get_pins(pdev->dev.of_node,
136 &sda_pin, &scl_pin);
8ffaa0f4
JCPV
137 if (ret)
138 return ret;
139 } else {
140 if (!pdev->dev.platform_data)
141 return -ENXIO;
1b295c83
JD
142 pdata = pdev->dev.platform_data;
143 sda_pin = pdata->sda_pin;
144 scl_pin = pdata->scl_pin;
8ffaa0f4 145 }
1c23af90 146
1b295c83
JD
147 ret = gpio_request(sda_pin, "sda");
148 if (ret) {
149 if (ret == -EINVAL)
150 ret = -EPROBE_DEFER; /* Try again later */
1c23af90 151 goto err_request_sda;
1b295c83
JD
152 }
153 ret = gpio_request(scl_pin, "scl");
154 if (ret) {
155 if (ret == -EINVAL)
156 ret = -EPROBE_DEFER; /* Try again later */
1c23af90 157 goto err_request_scl;
1b295c83
JD
158 }
159
160 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
161 if (!priv) {
162 ret = -ENOMEM;
163 goto err_add_bus;
164 }
165 adap = &priv->adap;
166 bit_data = &priv->bit_data;
167 pdata = &priv->pdata;
168
169 if (pdev->dev.of_node) {
170 pdata->sda_pin = sda_pin;
171 pdata->scl_pin = scl_pin;
172 of_i2c_gpio_get_props(pdev->dev.of_node, pdata);
173 } else {
174 memcpy(pdata, pdev->dev.platform_data, sizeof(*pdata));
175 }
1c23af90
HS
176
177 if (pdata->sda_is_open_drain) {
178 gpio_direction_output(pdata->sda_pin, 1);
179 bit_data->setsda = i2c_gpio_setsda_val;
180 } else {
181 gpio_direction_input(pdata->sda_pin);
182 bit_data->setsda = i2c_gpio_setsda_dir;
183 }
184
185 if (pdata->scl_is_open_drain || pdata->scl_is_output_only) {
186 gpio_direction_output(pdata->scl_pin, 1);
187 bit_data->setscl = i2c_gpio_setscl_val;
188 } else {
189 gpio_direction_input(pdata->scl_pin);
190 bit_data->setscl = i2c_gpio_setscl_dir;
191 }
192
193 if (!pdata->scl_is_output_only)
194 bit_data->getscl = i2c_gpio_getscl;
195 bit_data->getsda = i2c_gpio_getsda;
196
197 if (pdata->udelay)
198 bit_data->udelay = pdata->udelay;
199 else if (pdata->scl_is_output_only)
200 bit_data->udelay = 50; /* 10 kHz */
201 else
202 bit_data->udelay = 5; /* 100 kHz */
203
204 if (pdata->timeout)
205 bit_data->timeout = pdata->timeout;
206 else
207 bit_data->timeout = HZ / 10; /* 100 ms */
208
209 bit_data->data = pdata;
210
211 adap->owner = THIS_MODULE;
58a7371a
BS
212 if (pdev->dev.of_node)
213 strlcpy(adap->name, dev_name(&pdev->dev), sizeof(adap->name));
214 else
215 snprintf(adap->name, sizeof(adap->name), "i2c-gpio%d", pdev->id);
216
1c23af90 217 adap->algo_data = bit_data;
3401b2ff 218 adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
1c23af90 219 adap->dev.parent = &pdev->dev;
8ffaa0f4 220 adap->dev.of_node = pdev->dev.of_node;
1c23af90 221
44454baa 222 adap->nr = pdev->id;
7e69c3ac 223 ret = i2c_bit_add_numbered_bus(adap);
1c23af90
HS
224 if (ret)
225 goto err_add_bus;
226
8ffaa0f4
JCPV
227 of_i2c_register_devices(adap);
228
229 platform_set_drvdata(pdev, priv);
1c23af90
HS
230
231 dev_info(&pdev->dev, "using pins %u (SDA) and %u (SCL%s)\n",
232 pdata->sda_pin, pdata->scl_pin,
233 pdata->scl_is_output_only
234 ? ", no clock stretching" : "");
235
236 return 0;
237
238err_add_bus:
1b295c83 239 gpio_free(scl_pin);
1c23af90 240err_request_scl:
1b295c83 241 gpio_free(sda_pin);
1c23af90 242err_request_sda:
1c23af90
HS
243 return ret;
244}
245
0b255e92 246static int i2c_gpio_remove(struct platform_device *pdev)
1c23af90 247{
8ffaa0f4 248 struct i2c_gpio_private_data *priv;
1c23af90
HS
249 struct i2c_gpio_platform_data *pdata;
250 struct i2c_adapter *adap;
251
8ffaa0f4
JCPV
252 priv = platform_get_drvdata(pdev);
253 adap = &priv->adap;
254 pdata = &priv->pdata;
1c23af90
HS
255
256 i2c_del_adapter(adap);
257 gpio_free(pdata->scl_pin);
258 gpio_free(pdata->sda_pin);
1c23af90
HS
259
260 return 0;
261}
262
8ffaa0f4
JCPV
263#if defined(CONFIG_OF)
264static const struct of_device_id i2c_gpio_dt_ids[] = {
265 { .compatible = "i2c-gpio", },
266 { /* sentinel */ }
267};
268
269MODULE_DEVICE_TABLE(of, i2c_gpio_dt_ids);
270#endif
271
1c23af90
HS
272static struct platform_driver i2c_gpio_driver = {
273 .driver = {
274 .name = "i2c-gpio",
275 .owner = THIS_MODULE,
8ffaa0f4 276 .of_match_table = of_match_ptr(i2c_gpio_dt_ids),
1c23af90 277 },
1efe7c55 278 .probe = i2c_gpio_probe,
0b255e92 279 .remove = i2c_gpio_remove,
1c23af90
HS
280};
281
282static int __init i2c_gpio_init(void)
283{
284 int ret;
285
1efe7c55 286 ret = platform_driver_register(&i2c_gpio_driver);
1c23af90
HS
287 if (ret)
288 printk(KERN_ERR "i2c-gpio: probe failed: %d\n", ret);
289
290 return ret;
291}
b8680784 292subsys_initcall(i2c_gpio_init);
1c23af90
HS
293
294static void __exit i2c_gpio_exit(void)
295{
296 platform_driver_unregister(&i2c_gpio_driver);
297}
298module_exit(i2c_gpio_exit);
299
e05503ef 300MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
1c23af90
HS
301MODULE_DESCRIPTION("Platform-independent bitbanging I2C driver");
302MODULE_LICENSE("GPL");
add8eda7 303MODULE_ALIAS("platform:i2c-gpio");