Commit | Line | Data |
---|---|---|
b4ecd326 RV |
1 | /* |
2 | * Copyright (C) ST-Ericsson SA 2010 | |
3 | * | |
4 | * License Terms: GNU General Public License, version 2 | |
5 | * Author: Hanumath Prasad <hanumath.prasad@stericsson.com> for ST-Ericsson | |
6 | * Author: Rabin Vincent <rabin.vincent@stericsson.com> for ST-Ericsson | |
7 | */ | |
8 | ||
9 | #include <linux/module.h> | |
10 | #include <linux/interrupt.h> | |
11 | #include <linux/irq.h> | |
12 | #include <linux/slab.h> | |
13 | #include <linux/i2c.h> | |
14 | #include <linux/mfd/core.h> | |
c6eda6c5 | 15 | #include <linux/mfd/tc3589x.h> |
b4ecd326 RV |
16 | |
17 | /** | |
20406ebf SI |
18 | * tc3589x_reg_read() - read a single TC3589x register |
19 | * @tc3589x: Device to read from | |
b4ecd326 RV |
20 | * @reg: Register to read |
21 | */ | |
20406ebf | 22 | int tc3589x_reg_read(struct tc3589x *tc3589x, u8 reg) |
b4ecd326 RV |
23 | { |
24 | int ret; | |
25 | ||
20406ebf | 26 | ret = i2c_smbus_read_byte_data(tc3589x->i2c, reg); |
b4ecd326 | 27 | if (ret < 0) |
20406ebf | 28 | dev_err(tc3589x->dev, "failed to read reg %#x: %d\n", |
b4ecd326 RV |
29 | reg, ret); |
30 | ||
31 | return ret; | |
32 | } | |
20406ebf | 33 | EXPORT_SYMBOL_GPL(tc3589x_reg_read); |
b4ecd326 RV |
34 | |
35 | /** | |
20406ebf SI |
36 | * tc3589x_reg_read() - write a single TC3589x register |
37 | * @tc3589x: Device to write to | |
b4ecd326 RV |
38 | * @reg: Register to read |
39 | * @data: Value to write | |
40 | */ | |
20406ebf | 41 | int tc3589x_reg_write(struct tc3589x *tc3589x, u8 reg, u8 data) |
b4ecd326 RV |
42 | { |
43 | int ret; | |
44 | ||
20406ebf | 45 | ret = i2c_smbus_write_byte_data(tc3589x->i2c, reg, data); |
b4ecd326 | 46 | if (ret < 0) |
20406ebf | 47 | dev_err(tc3589x->dev, "failed to write reg %#x: %d\n", |
b4ecd326 RV |
48 | reg, ret); |
49 | ||
50 | return ret; | |
51 | } | |
20406ebf | 52 | EXPORT_SYMBOL_GPL(tc3589x_reg_write); |
b4ecd326 RV |
53 | |
54 | /** | |
20406ebf SI |
55 | * tc3589x_block_read() - read multiple TC3589x registers |
56 | * @tc3589x: Device to read from | |
b4ecd326 RV |
57 | * @reg: First register |
58 | * @length: Number of registers | |
59 | * @values: Buffer to write to | |
60 | */ | |
20406ebf | 61 | int tc3589x_block_read(struct tc3589x *tc3589x, u8 reg, u8 length, u8 *values) |
b4ecd326 RV |
62 | { |
63 | int ret; | |
64 | ||
20406ebf | 65 | ret = i2c_smbus_read_i2c_block_data(tc3589x->i2c, reg, length, values); |
b4ecd326 | 66 | if (ret < 0) |
20406ebf | 67 | dev_err(tc3589x->dev, "failed to read regs %#x: %d\n", |
b4ecd326 RV |
68 | reg, ret); |
69 | ||
70 | return ret; | |
71 | } | |
20406ebf | 72 | EXPORT_SYMBOL_GPL(tc3589x_block_read); |
b4ecd326 RV |
73 | |
74 | /** | |
20406ebf SI |
75 | * tc3589x_block_write() - write multiple TC3589x registers |
76 | * @tc3589x: Device to write to | |
b4ecd326 RV |
77 | * @reg: First register |
78 | * @length: Number of registers | |
79 | * @values: Values to write | |
80 | */ | |
20406ebf | 81 | int tc3589x_block_write(struct tc3589x *tc3589x, u8 reg, u8 length, |
b4ecd326 RV |
82 | const u8 *values) |
83 | { | |
84 | int ret; | |
85 | ||
20406ebf | 86 | ret = i2c_smbus_write_i2c_block_data(tc3589x->i2c, reg, length, |
b4ecd326 RV |
87 | values); |
88 | if (ret < 0) | |
20406ebf | 89 | dev_err(tc3589x->dev, "failed to write regs %#x: %d\n", |
b4ecd326 RV |
90 | reg, ret); |
91 | ||
92 | return ret; | |
93 | } | |
20406ebf | 94 | EXPORT_SYMBOL_GPL(tc3589x_block_write); |
b4ecd326 RV |
95 | |
96 | /** | |
20406ebf SI |
97 | * tc3589x_set_bits() - set the value of a bitfield in a TC3589x register |
98 | * @tc3589x: Device to write to | |
b4ecd326 RV |
99 | * @reg: Register to write |
100 | * @mask: Mask of bits to set | |
101 | * @values: Value to set | |
102 | */ | |
20406ebf | 103 | int tc3589x_set_bits(struct tc3589x *tc3589x, u8 reg, u8 mask, u8 val) |
b4ecd326 RV |
104 | { |
105 | int ret; | |
106 | ||
20406ebf | 107 | mutex_lock(&tc3589x->lock); |
b4ecd326 | 108 | |
20406ebf | 109 | ret = tc3589x_reg_read(tc3589x, reg); |
b4ecd326 RV |
110 | if (ret < 0) |
111 | goto out; | |
112 | ||
113 | ret &= ~mask; | |
114 | ret |= val; | |
115 | ||
20406ebf | 116 | ret = tc3589x_reg_write(tc3589x, reg, ret); |
b4ecd326 RV |
117 | |
118 | out: | |
20406ebf | 119 | mutex_unlock(&tc3589x->lock); |
b4ecd326 RV |
120 | return ret; |
121 | } | |
20406ebf | 122 | EXPORT_SYMBOL_GPL(tc3589x_set_bits); |
b4ecd326 RV |
123 | |
124 | static struct resource gpio_resources[] = { | |
125 | { | |
20406ebf SI |
126 | .start = TC3589x_INT_GPIIRQ, |
127 | .end = TC3589x_INT_GPIIRQ, | |
b4ecd326 RV |
128 | .flags = IORESOURCE_IRQ, |
129 | }, | |
130 | }; | |
131 | ||
611b7590 | 132 | static struct mfd_cell tc3589x_dev_gpio[] = { |
b4ecd326 | 133 | { |
20406ebf | 134 | .name = "tc3589x-gpio", |
b4ecd326 RV |
135 | .num_resources = ARRAY_SIZE(gpio_resources), |
136 | .resources = &gpio_resources[0], | |
137 | }, | |
138 | }; | |
139 | ||
20406ebf | 140 | static irqreturn_t tc3589x_irq(int irq, void *data) |
b4ecd326 | 141 | { |
20406ebf | 142 | struct tc3589x *tc3589x = data; |
b4ecd326 RV |
143 | int status; |
144 | ||
bd77efd0 | 145 | again: |
20406ebf | 146 | status = tc3589x_reg_read(tc3589x, TC3589x_IRQST); |
b4ecd326 RV |
147 | if (status < 0) |
148 | return IRQ_NONE; | |
149 | ||
150 | while (status) { | |
151 | int bit = __ffs(status); | |
152 | ||
20406ebf | 153 | handle_nested_irq(tc3589x->irq_base + bit); |
b4ecd326 RV |
154 | status &= ~(1 << bit); |
155 | } | |
156 | ||
157 | /* | |
158 | * A dummy read or write (to any register) appears to be necessary to | |
159 | * have the last interrupt clear (for example, GPIO IC write) take | |
bd77efd0 SI |
160 | * effect. In such a case, recheck for any interrupt which is still |
161 | * pending. | |
b4ecd326 | 162 | */ |
bd77efd0 SI |
163 | status = tc3589x_reg_read(tc3589x, TC3589x_IRQST); |
164 | if (status) | |
165 | goto again; | |
b4ecd326 RV |
166 | |
167 | return IRQ_HANDLED; | |
168 | } | |
169 | ||
20406ebf | 170 | static void tc3589x_irq_dummy(unsigned int irq) |
b4ecd326 RV |
171 | { |
172 | /* No mask/unmask at this level */ | |
173 | } | |
174 | ||
20406ebf SI |
175 | static struct irq_chip tc3589x_irq_chip = { |
176 | .name = "tc3589x", | |
177 | .mask = tc3589x_irq_dummy, | |
178 | .unmask = tc3589x_irq_dummy, | |
b4ecd326 RV |
179 | }; |
180 | ||
20406ebf | 181 | static int tc3589x_irq_init(struct tc3589x *tc3589x) |
b4ecd326 | 182 | { |
20406ebf | 183 | int base = tc3589x->irq_base; |
b4ecd326 RV |
184 | int irq; |
185 | ||
20406ebf SI |
186 | for (irq = base; irq < base + TC3589x_NR_INTERNAL_IRQS; irq++) { |
187 | set_irq_chip_data(irq, tc3589x); | |
188 | set_irq_chip_and_handler(irq, &tc3589x_irq_chip, | |
b4ecd326 RV |
189 | handle_edge_irq); |
190 | set_irq_nested_thread(irq, 1); | |
191 | #ifdef CONFIG_ARM | |
192 | set_irq_flags(irq, IRQF_VALID); | |
193 | #else | |
194 | set_irq_noprobe(irq); | |
195 | #endif | |
196 | } | |
197 | ||
198 | return 0; | |
199 | } | |
200 | ||
20406ebf | 201 | static void tc3589x_irq_remove(struct tc3589x *tc3589x) |
b4ecd326 | 202 | { |
20406ebf | 203 | int base = tc3589x->irq_base; |
b4ecd326 RV |
204 | int irq; |
205 | ||
20406ebf | 206 | for (irq = base; irq < base + TC3589x_NR_INTERNAL_IRQS; irq++) { |
b4ecd326 RV |
207 | #ifdef CONFIG_ARM |
208 | set_irq_flags(irq, 0); | |
209 | #endif | |
210 | set_irq_chip_and_handler(irq, NULL, NULL); | |
211 | set_irq_chip_data(irq, NULL); | |
212 | } | |
213 | } | |
214 | ||
20406ebf | 215 | static int tc3589x_chip_init(struct tc3589x *tc3589x) |
b4ecd326 RV |
216 | { |
217 | int manf, ver, ret; | |
218 | ||
20406ebf | 219 | manf = tc3589x_reg_read(tc3589x, TC3589x_MANFCODE); |
b4ecd326 RV |
220 | if (manf < 0) |
221 | return manf; | |
222 | ||
20406ebf | 223 | ver = tc3589x_reg_read(tc3589x, TC3589x_VERSION); |
b4ecd326 RV |
224 | if (ver < 0) |
225 | return ver; | |
226 | ||
20406ebf SI |
227 | if (manf != TC3589x_MANFCODE_MAGIC) { |
228 | dev_err(tc3589x->dev, "unknown manufacturer: %#x\n", manf); | |
b4ecd326 RV |
229 | return -EINVAL; |
230 | } | |
231 | ||
20406ebf | 232 | dev_info(tc3589x->dev, "manufacturer: %#x, version: %#x\n", manf, ver); |
b4ecd326 RV |
233 | |
234 | /* Put everything except the IRQ module into reset */ | |
20406ebf SI |
235 | ret = tc3589x_reg_write(tc3589x, TC3589x_RSTCTRL, |
236 | TC3589x_RSTCTRL_TIMRST | |
237 | | TC3589x_RSTCTRL_ROTRST | |
238 | | TC3589x_RSTCTRL_KBDRST | |
239 | | TC3589x_RSTCTRL_GPIRST); | |
b4ecd326 RV |
240 | if (ret < 0) |
241 | return ret; | |
242 | ||
243 | /* Clear the reset interrupt. */ | |
20406ebf | 244 | return tc3589x_reg_write(tc3589x, TC3589x_RSTINTCLR, 0x1); |
b4ecd326 RV |
245 | } |
246 | ||
611b7590 SI |
247 | static int __devinit tc3589x_device_init(struct tc3589x *tc3589x) |
248 | { | |
249 | int ret = 0; | |
250 | unsigned int blocks = tc3589x->pdata->block; | |
251 | ||
252 | if (blocks & TC3589x_BLOCK_GPIO) { | |
253 | ret = mfd_add_devices(tc3589x->dev, -1, tc3589x_dev_gpio, | |
254 | ARRAY_SIZE(tc3589x_dev_gpio), NULL, | |
255 | tc3589x->irq_base); | |
256 | if (ret) { | |
257 | dev_err(tc3589x->dev, "failed to add gpio child\n"); | |
258 | return ret; | |
259 | } | |
260 | dev_info(tc3589x->dev, "added gpio block\n"); | |
261 | } | |
262 | ||
263 | return ret; | |
264 | ||
265 | } | |
266 | ||
20406ebf | 267 | static int __devinit tc3589x_probe(struct i2c_client *i2c, |
b4ecd326 RV |
268 | const struct i2c_device_id *id) |
269 | { | |
20406ebf SI |
270 | struct tc3589x_platform_data *pdata = i2c->dev.platform_data; |
271 | struct tc3589x *tc3589x; | |
b4ecd326 RV |
272 | int ret; |
273 | ||
274 | if (!i2c_check_functionality(i2c->adapter, I2C_FUNC_SMBUS_BYTE_DATA | |
275 | | I2C_FUNC_SMBUS_I2C_BLOCK)) | |
276 | return -EIO; | |
277 | ||
20406ebf SI |
278 | tc3589x = kzalloc(sizeof(struct tc3589x), GFP_KERNEL); |
279 | if (!tc3589x) | |
b4ecd326 RV |
280 | return -ENOMEM; |
281 | ||
20406ebf | 282 | mutex_init(&tc3589x->lock); |
b4ecd326 | 283 | |
20406ebf SI |
284 | tc3589x->dev = &i2c->dev; |
285 | tc3589x->i2c = i2c; | |
286 | tc3589x->pdata = pdata; | |
287 | tc3589x->irq_base = pdata->irq_base; | |
288 | tc3589x->num_gpio = id->driver_data; | |
b4ecd326 | 289 | |
20406ebf | 290 | i2c_set_clientdata(i2c, tc3589x); |
b4ecd326 | 291 | |
20406ebf | 292 | ret = tc3589x_chip_init(tc3589x); |
b4ecd326 RV |
293 | if (ret) |
294 | goto out_free; | |
295 | ||
20406ebf | 296 | ret = tc3589x_irq_init(tc3589x); |
b4ecd326 RV |
297 | if (ret) |
298 | goto out_free; | |
299 | ||
20406ebf | 300 | ret = request_threaded_irq(tc3589x->i2c->irq, NULL, tc3589x_irq, |
b4ecd326 | 301 | IRQF_TRIGGER_FALLING | IRQF_ONESHOT, |
20406ebf | 302 | "tc3589x", tc3589x); |
b4ecd326 | 303 | if (ret) { |
20406ebf | 304 | dev_err(tc3589x->dev, "failed to request IRQ: %d\n", ret); |
b4ecd326 RV |
305 | goto out_removeirq; |
306 | } | |
307 | ||
611b7590 | 308 | ret = tc3589x_device_init(tc3589x); |
b4ecd326 | 309 | if (ret) { |
611b7590 | 310 | dev_err(tc3589x->dev, "failed to add child devices\n"); |
b4ecd326 RV |
311 | goto out_freeirq; |
312 | } | |
313 | ||
314 | return 0; | |
315 | ||
316 | out_freeirq: | |
20406ebf | 317 | free_irq(tc3589x->i2c->irq, tc3589x); |
b4ecd326 | 318 | out_removeirq: |
20406ebf | 319 | tc3589x_irq_remove(tc3589x); |
b4ecd326 | 320 | out_free: |
20406ebf | 321 | kfree(tc3589x); |
b4ecd326 RV |
322 | return ret; |
323 | } | |
324 | ||
20406ebf | 325 | static int __devexit tc3589x_remove(struct i2c_client *client) |
b4ecd326 | 326 | { |
20406ebf | 327 | struct tc3589x *tc3589x = i2c_get_clientdata(client); |
b4ecd326 | 328 | |
20406ebf | 329 | mfd_remove_devices(tc3589x->dev); |
b4ecd326 | 330 | |
20406ebf SI |
331 | free_irq(tc3589x->i2c->irq, tc3589x); |
332 | tc3589x_irq_remove(tc3589x); | |
b4ecd326 | 333 | |
20406ebf | 334 | kfree(tc3589x); |
b4ecd326 RV |
335 | |
336 | return 0; | |
337 | } | |
338 | ||
20406ebf SI |
339 | static const struct i2c_device_id tc3589x_id[] = { |
340 | { "tc3589x", 24 }, | |
b4ecd326 RV |
341 | { } |
342 | }; | |
20406ebf | 343 | MODULE_DEVICE_TABLE(i2c, tc3589x_id); |
b4ecd326 | 344 | |
20406ebf SI |
345 | static struct i2c_driver tc3589x_driver = { |
346 | .driver.name = "tc3589x", | |
b4ecd326 | 347 | .driver.owner = THIS_MODULE, |
20406ebf SI |
348 | .probe = tc3589x_probe, |
349 | .remove = __devexit_p(tc3589x_remove), | |
350 | .id_table = tc3589x_id, | |
b4ecd326 RV |
351 | }; |
352 | ||
20406ebf | 353 | static int __init tc3589x_init(void) |
b4ecd326 | 354 | { |
20406ebf | 355 | return i2c_add_driver(&tc3589x_driver); |
b4ecd326 | 356 | } |
20406ebf | 357 | subsys_initcall(tc3589x_init); |
b4ecd326 | 358 | |
20406ebf | 359 | static void __exit tc3589x_exit(void) |
b4ecd326 | 360 | { |
20406ebf | 361 | i2c_del_driver(&tc3589x_driver); |
b4ecd326 | 362 | } |
20406ebf | 363 | module_exit(tc3589x_exit); |
b4ecd326 RV |
364 | |
365 | MODULE_LICENSE("GPL v2"); | |
20406ebf | 366 | MODULE_DESCRIPTION("TC3589x MFD core driver"); |
b4ecd326 | 367 | MODULE_AUTHOR("Hanumath Prasad, Rabin Vincent"); |