]>
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 <[email protected]> for ST-Ericsson | |
6 | * Author: Rabin Vincent <[email protected]> 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 | 16 | |
593e9d70 SI |
17 | #define TC3589x_CLKMODE_MODCTL_SLEEP 0x0 |
18 | #define TC3589x_CLKMODE_MODCTL_OPERATION (1 << 0) | |
19 | ||
b4ecd326 | 20 | /** |
20406ebf SI |
21 | * tc3589x_reg_read() - read a single TC3589x register |
22 | * @tc3589x: Device to read from | |
b4ecd326 RV |
23 | * @reg: Register to read |
24 | */ | |
20406ebf | 25 | int tc3589x_reg_read(struct tc3589x *tc3589x, u8 reg) |
b4ecd326 RV |
26 | { |
27 | int ret; | |
28 | ||
20406ebf | 29 | ret = i2c_smbus_read_byte_data(tc3589x->i2c, reg); |
b4ecd326 | 30 | if (ret < 0) |
20406ebf | 31 | dev_err(tc3589x->dev, "failed to read reg %#x: %d\n", |
b4ecd326 RV |
32 | reg, ret); |
33 | ||
34 | return ret; | |
35 | } | |
20406ebf | 36 | EXPORT_SYMBOL_GPL(tc3589x_reg_read); |
b4ecd326 RV |
37 | |
38 | /** | |
20406ebf SI |
39 | * tc3589x_reg_read() - write a single TC3589x register |
40 | * @tc3589x: Device to write to | |
b4ecd326 RV |
41 | * @reg: Register to read |
42 | * @data: Value to write | |
43 | */ | |
20406ebf | 44 | int tc3589x_reg_write(struct tc3589x *tc3589x, u8 reg, u8 data) |
b4ecd326 RV |
45 | { |
46 | int ret; | |
47 | ||
20406ebf | 48 | ret = i2c_smbus_write_byte_data(tc3589x->i2c, reg, data); |
b4ecd326 | 49 | if (ret < 0) |
20406ebf | 50 | dev_err(tc3589x->dev, "failed to write reg %#x: %d\n", |
b4ecd326 RV |
51 | reg, ret); |
52 | ||
53 | return ret; | |
54 | } | |
20406ebf | 55 | EXPORT_SYMBOL_GPL(tc3589x_reg_write); |
b4ecd326 RV |
56 | |
57 | /** | |
20406ebf SI |
58 | * tc3589x_block_read() - read multiple TC3589x registers |
59 | * @tc3589x: Device to read from | |
b4ecd326 RV |
60 | * @reg: First register |
61 | * @length: Number of registers | |
62 | * @values: Buffer to write to | |
63 | */ | |
20406ebf | 64 | int tc3589x_block_read(struct tc3589x *tc3589x, u8 reg, u8 length, u8 *values) |
b4ecd326 RV |
65 | { |
66 | int ret; | |
67 | ||
20406ebf | 68 | ret = i2c_smbus_read_i2c_block_data(tc3589x->i2c, reg, length, values); |
b4ecd326 | 69 | if (ret < 0) |
20406ebf | 70 | dev_err(tc3589x->dev, "failed to read regs %#x: %d\n", |
b4ecd326 RV |
71 | reg, ret); |
72 | ||
73 | return ret; | |
74 | } | |
20406ebf | 75 | EXPORT_SYMBOL_GPL(tc3589x_block_read); |
b4ecd326 RV |
76 | |
77 | /** | |
20406ebf SI |
78 | * tc3589x_block_write() - write multiple TC3589x registers |
79 | * @tc3589x: Device to write to | |
b4ecd326 RV |
80 | * @reg: First register |
81 | * @length: Number of registers | |
82 | * @values: Values to write | |
83 | */ | |
20406ebf | 84 | int tc3589x_block_write(struct tc3589x *tc3589x, u8 reg, u8 length, |
b4ecd326 RV |
85 | const u8 *values) |
86 | { | |
87 | int ret; | |
88 | ||
20406ebf | 89 | ret = i2c_smbus_write_i2c_block_data(tc3589x->i2c, reg, length, |
b4ecd326 RV |
90 | values); |
91 | if (ret < 0) | |
20406ebf | 92 | dev_err(tc3589x->dev, "failed to write regs %#x: %d\n", |
b4ecd326 RV |
93 | reg, ret); |
94 | ||
95 | return ret; | |
96 | } | |
20406ebf | 97 | EXPORT_SYMBOL_GPL(tc3589x_block_write); |
b4ecd326 RV |
98 | |
99 | /** | |
20406ebf SI |
100 | * tc3589x_set_bits() - set the value of a bitfield in a TC3589x register |
101 | * @tc3589x: Device to write to | |
b4ecd326 RV |
102 | * @reg: Register to write |
103 | * @mask: Mask of bits to set | |
104 | * @values: Value to set | |
105 | */ | |
20406ebf | 106 | int tc3589x_set_bits(struct tc3589x *tc3589x, u8 reg, u8 mask, u8 val) |
b4ecd326 RV |
107 | { |
108 | int ret; | |
109 | ||
20406ebf | 110 | mutex_lock(&tc3589x->lock); |
b4ecd326 | 111 | |
20406ebf | 112 | ret = tc3589x_reg_read(tc3589x, reg); |
b4ecd326 RV |
113 | if (ret < 0) |
114 | goto out; | |
115 | ||
116 | ret &= ~mask; | |
117 | ret |= val; | |
118 | ||
20406ebf | 119 | ret = tc3589x_reg_write(tc3589x, reg, ret); |
b4ecd326 RV |
120 | |
121 | out: | |
20406ebf | 122 | mutex_unlock(&tc3589x->lock); |
b4ecd326 RV |
123 | return ret; |
124 | } | |
20406ebf | 125 | EXPORT_SYMBOL_GPL(tc3589x_set_bits); |
b4ecd326 RV |
126 | |
127 | static struct resource gpio_resources[] = { | |
128 | { | |
20406ebf SI |
129 | .start = TC3589x_INT_GPIIRQ, |
130 | .end = TC3589x_INT_GPIIRQ, | |
b4ecd326 RV |
131 | .flags = IORESOURCE_IRQ, |
132 | }, | |
133 | }; | |
134 | ||
09c730a4 SI |
135 | static struct resource keypad_resources[] = { |
136 | { | |
137 | .start = TC3589x_INT_KBDIRQ, | |
138 | .end = TC3589x_INT_KBDIRQ, | |
139 | .flags = IORESOURCE_IRQ, | |
140 | }, | |
141 | }; | |
142 | ||
611b7590 | 143 | static struct mfd_cell tc3589x_dev_gpio[] = { |
b4ecd326 | 144 | { |
20406ebf | 145 | .name = "tc3589x-gpio", |
b4ecd326 RV |
146 | .num_resources = ARRAY_SIZE(gpio_resources), |
147 | .resources = &gpio_resources[0], | |
148 | }, | |
149 | }; | |
150 | ||
09c730a4 SI |
151 | static struct mfd_cell tc3589x_dev_keypad[] = { |
152 | { | |
153 | .name = "tc3589x-keypad", | |
154 | .num_resources = ARRAY_SIZE(keypad_resources), | |
155 | .resources = &keypad_resources[0], | |
156 | }, | |
157 | }; | |
158 | ||
20406ebf | 159 | static irqreturn_t tc3589x_irq(int irq, void *data) |
b4ecd326 | 160 | { |
20406ebf | 161 | struct tc3589x *tc3589x = data; |
b4ecd326 RV |
162 | int status; |
163 | ||
bd77efd0 | 164 | again: |
20406ebf | 165 | status = tc3589x_reg_read(tc3589x, TC3589x_IRQST); |
b4ecd326 RV |
166 | if (status < 0) |
167 | return IRQ_NONE; | |
168 | ||
169 | while (status) { | |
170 | int bit = __ffs(status); | |
171 | ||
20406ebf | 172 | handle_nested_irq(tc3589x->irq_base + bit); |
b4ecd326 RV |
173 | status &= ~(1 << bit); |
174 | } | |
175 | ||
176 | /* | |
177 | * A dummy read or write (to any register) appears to be necessary to | |
178 | * have the last interrupt clear (for example, GPIO IC write) take | |
bd77efd0 SI |
179 | * effect. In such a case, recheck for any interrupt which is still |
180 | * pending. | |
b4ecd326 | 181 | */ |
bd77efd0 SI |
182 | status = tc3589x_reg_read(tc3589x, TC3589x_IRQST); |
183 | if (status) | |
184 | goto again; | |
b4ecd326 RV |
185 | |
186 | return IRQ_HANDLED; | |
187 | } | |
188 | ||
20406ebf | 189 | static int tc3589x_irq_init(struct tc3589x *tc3589x) |
b4ecd326 | 190 | { |
20406ebf | 191 | int base = tc3589x->irq_base; |
b4ecd326 RV |
192 | int irq; |
193 | ||
20406ebf | 194 | for (irq = base; irq < base + TC3589x_NR_INTERNAL_IRQS; irq++) { |
d5bb1221 TG |
195 | irq_set_chip_data(irq, tc3589x); |
196 | irq_set_chip_and_handler(irq, &dummy_irq_chip, | |
b4ecd326 | 197 | handle_edge_irq); |
d5bb1221 | 198 | irq_set_nested_thread(irq, 1); |
b4ecd326 RV |
199 | #ifdef CONFIG_ARM |
200 | set_irq_flags(irq, IRQF_VALID); | |
201 | #else | |
d5bb1221 | 202 | irq_set_noprobe(irq); |
b4ecd326 RV |
203 | #endif |
204 | } | |
205 | ||
206 | return 0; | |
207 | } | |
208 | ||
20406ebf | 209 | static void tc3589x_irq_remove(struct tc3589x *tc3589x) |
b4ecd326 | 210 | { |
20406ebf | 211 | int base = tc3589x->irq_base; |
b4ecd326 RV |
212 | int irq; |
213 | ||
20406ebf | 214 | for (irq = base; irq < base + TC3589x_NR_INTERNAL_IRQS; irq++) { |
b4ecd326 RV |
215 | #ifdef CONFIG_ARM |
216 | set_irq_flags(irq, 0); | |
217 | #endif | |
d5bb1221 TG |
218 | irq_set_chip_and_handler(irq, NULL, NULL); |
219 | irq_set_chip_data(irq, NULL); | |
b4ecd326 RV |
220 | } |
221 | } | |
222 | ||
20406ebf | 223 | static int tc3589x_chip_init(struct tc3589x *tc3589x) |
b4ecd326 RV |
224 | { |
225 | int manf, ver, ret; | |
226 | ||
20406ebf | 227 | manf = tc3589x_reg_read(tc3589x, TC3589x_MANFCODE); |
b4ecd326 RV |
228 | if (manf < 0) |
229 | return manf; | |
230 | ||
20406ebf | 231 | ver = tc3589x_reg_read(tc3589x, TC3589x_VERSION); |
b4ecd326 RV |
232 | if (ver < 0) |
233 | return ver; | |
234 | ||
20406ebf SI |
235 | if (manf != TC3589x_MANFCODE_MAGIC) { |
236 | dev_err(tc3589x->dev, "unknown manufacturer: %#x\n", manf); | |
b4ecd326 RV |
237 | return -EINVAL; |
238 | } | |
239 | ||
20406ebf | 240 | dev_info(tc3589x->dev, "manufacturer: %#x, version: %#x\n", manf, ver); |
b4ecd326 | 241 | |
523bc382 SI |
242 | /* |
243 | * Put everything except the IRQ module into reset; | |
244 | * also spare the GPIO module for any pin initialization | |
245 | * done during pre-kernel boot | |
246 | */ | |
20406ebf SI |
247 | ret = tc3589x_reg_write(tc3589x, TC3589x_RSTCTRL, |
248 | TC3589x_RSTCTRL_TIMRST | |
249 | | TC3589x_RSTCTRL_ROTRST | |
523bc382 | 250 | | TC3589x_RSTCTRL_KBDRST); |
b4ecd326 RV |
251 | if (ret < 0) |
252 | return ret; | |
253 | ||
254 | /* Clear the reset interrupt. */ | |
20406ebf | 255 | return tc3589x_reg_write(tc3589x, TC3589x_RSTINTCLR, 0x1); |
b4ecd326 RV |
256 | } |
257 | ||
611b7590 SI |
258 | static int __devinit tc3589x_device_init(struct tc3589x *tc3589x) |
259 | { | |
260 | int ret = 0; | |
261 | unsigned int blocks = tc3589x->pdata->block; | |
262 | ||
263 | if (blocks & TC3589x_BLOCK_GPIO) { | |
264 | ret = mfd_add_devices(tc3589x->dev, -1, tc3589x_dev_gpio, | |
265 | ARRAY_SIZE(tc3589x_dev_gpio), NULL, | |
266 | tc3589x->irq_base); | |
267 | if (ret) { | |
268 | dev_err(tc3589x->dev, "failed to add gpio child\n"); | |
269 | return ret; | |
270 | } | |
271 | dev_info(tc3589x->dev, "added gpio block\n"); | |
272 | } | |
273 | ||
09c730a4 SI |
274 | if (blocks & TC3589x_BLOCK_KEYPAD) { |
275 | ret = mfd_add_devices(tc3589x->dev, -1, tc3589x_dev_keypad, | |
276 | ARRAY_SIZE(tc3589x_dev_keypad), NULL, | |
277 | tc3589x->irq_base); | |
278 | if (ret) { | |
279 | dev_err(tc3589x->dev, "failed to keypad child\n"); | |
280 | return ret; | |
281 | } | |
282 | dev_info(tc3589x->dev, "added keypad block\n"); | |
283 | } | |
611b7590 | 284 | |
09c730a4 | 285 | return ret; |
611b7590 SI |
286 | } |
287 | ||
20406ebf | 288 | static int __devinit tc3589x_probe(struct i2c_client *i2c, |
b4ecd326 RV |
289 | const struct i2c_device_id *id) |
290 | { | |
20406ebf SI |
291 | struct tc3589x_platform_data *pdata = i2c->dev.platform_data; |
292 | struct tc3589x *tc3589x; | |
b4ecd326 RV |
293 | int ret; |
294 | ||
295 | if (!i2c_check_functionality(i2c->adapter, I2C_FUNC_SMBUS_BYTE_DATA | |
296 | | I2C_FUNC_SMBUS_I2C_BLOCK)) | |
297 | return -EIO; | |
298 | ||
20406ebf SI |
299 | tc3589x = kzalloc(sizeof(struct tc3589x), GFP_KERNEL); |
300 | if (!tc3589x) | |
b4ecd326 RV |
301 | return -ENOMEM; |
302 | ||
20406ebf | 303 | mutex_init(&tc3589x->lock); |
b4ecd326 | 304 | |
20406ebf SI |
305 | tc3589x->dev = &i2c->dev; |
306 | tc3589x->i2c = i2c; | |
307 | tc3589x->pdata = pdata; | |
308 | tc3589x->irq_base = pdata->irq_base; | |
309 | tc3589x->num_gpio = id->driver_data; | |
b4ecd326 | 310 | |
20406ebf | 311 | i2c_set_clientdata(i2c, tc3589x); |
b4ecd326 | 312 | |
20406ebf | 313 | ret = tc3589x_chip_init(tc3589x); |
b4ecd326 RV |
314 | if (ret) |
315 | goto out_free; | |
316 | ||
20406ebf | 317 | ret = tc3589x_irq_init(tc3589x); |
b4ecd326 RV |
318 | if (ret) |
319 | goto out_free; | |
320 | ||
20406ebf | 321 | ret = request_threaded_irq(tc3589x->i2c->irq, NULL, tc3589x_irq, |
b4ecd326 | 322 | IRQF_TRIGGER_FALLING | IRQF_ONESHOT, |
20406ebf | 323 | "tc3589x", tc3589x); |
b4ecd326 | 324 | if (ret) { |
20406ebf | 325 | dev_err(tc3589x->dev, "failed to request IRQ: %d\n", ret); |
b4ecd326 RV |
326 | goto out_removeirq; |
327 | } | |
328 | ||
611b7590 | 329 | ret = tc3589x_device_init(tc3589x); |
b4ecd326 | 330 | if (ret) { |
611b7590 | 331 | dev_err(tc3589x->dev, "failed to add child devices\n"); |
b4ecd326 RV |
332 | goto out_freeirq; |
333 | } | |
334 | ||
335 | return 0; | |
336 | ||
337 | out_freeirq: | |
20406ebf | 338 | free_irq(tc3589x->i2c->irq, tc3589x); |
b4ecd326 | 339 | out_removeirq: |
20406ebf | 340 | tc3589x_irq_remove(tc3589x); |
b4ecd326 | 341 | out_free: |
20406ebf | 342 | kfree(tc3589x); |
b4ecd326 RV |
343 | return ret; |
344 | } | |
345 | ||
20406ebf | 346 | static int __devexit tc3589x_remove(struct i2c_client *client) |
b4ecd326 | 347 | { |
20406ebf | 348 | struct tc3589x *tc3589x = i2c_get_clientdata(client); |
b4ecd326 | 349 | |
20406ebf | 350 | mfd_remove_devices(tc3589x->dev); |
b4ecd326 | 351 | |
20406ebf SI |
352 | free_irq(tc3589x->i2c->irq, tc3589x); |
353 | tc3589x_irq_remove(tc3589x); | |
b4ecd326 | 354 | |
20406ebf | 355 | kfree(tc3589x); |
b4ecd326 RV |
356 | |
357 | return 0; | |
358 | } | |
359 | ||
930bf022 | 360 | #ifdef CONFIG_PM_SLEEP |
593e9d70 SI |
361 | static int tc3589x_suspend(struct device *dev) |
362 | { | |
363 | struct tc3589x *tc3589x = dev_get_drvdata(dev); | |
364 | struct i2c_client *client = tc3589x->i2c; | |
365 | int ret = 0; | |
366 | ||
367 | /* put the system to sleep mode */ | |
368 | if (!device_may_wakeup(&client->dev)) | |
369 | ret = tc3589x_reg_write(tc3589x, TC3589x_CLKMODE, | |
370 | TC3589x_CLKMODE_MODCTL_SLEEP); | |
371 | ||
372 | return ret; | |
373 | } | |
374 | ||
375 | static int tc3589x_resume(struct device *dev) | |
376 | { | |
377 | struct tc3589x *tc3589x = dev_get_drvdata(dev); | |
378 | struct i2c_client *client = tc3589x->i2c; | |
379 | int ret = 0; | |
380 | ||
381 | /* enable the system into operation */ | |
382 | if (!device_may_wakeup(&client->dev)) | |
383 | ret = tc3589x_reg_write(tc3589x, TC3589x_CLKMODE, | |
384 | TC3589x_CLKMODE_MODCTL_OPERATION); | |
385 | ||
386 | return ret; | |
387 | } | |
54d8e2c3 | 388 | #endif |
593e9d70 | 389 | |
930bf022 AL |
390 | static SIMPLE_DEV_PM_OPS(tc3589x_dev_pm_ops, tc3589x_suspend, tc3589x_resume); |
391 | ||
20406ebf SI |
392 | static const struct i2c_device_id tc3589x_id[] = { |
393 | { "tc3589x", 24 }, | |
b4ecd326 RV |
394 | { } |
395 | }; | |
20406ebf | 396 | MODULE_DEVICE_TABLE(i2c, tc3589x_id); |
b4ecd326 | 397 | |
20406ebf SI |
398 | static struct i2c_driver tc3589x_driver = { |
399 | .driver.name = "tc3589x", | |
b4ecd326 | 400 | .driver.owner = THIS_MODULE, |
593e9d70 | 401 | .driver.pm = &tc3589x_dev_pm_ops, |
20406ebf SI |
402 | .probe = tc3589x_probe, |
403 | .remove = __devexit_p(tc3589x_remove), | |
404 | .id_table = tc3589x_id, | |
b4ecd326 RV |
405 | }; |
406 | ||
20406ebf | 407 | static int __init tc3589x_init(void) |
b4ecd326 | 408 | { |
20406ebf | 409 | return i2c_add_driver(&tc3589x_driver); |
b4ecd326 | 410 | } |
20406ebf | 411 | subsys_initcall(tc3589x_init); |
b4ecd326 | 412 | |
20406ebf | 413 | static void __exit tc3589x_exit(void) |
b4ecd326 | 414 | { |
20406ebf | 415 | i2c_del_driver(&tc3589x_driver); |
b4ecd326 | 416 | } |
20406ebf | 417 | module_exit(tc3589x_exit); |
b4ecd326 RV |
418 | |
419 | MODULE_LICENSE("GPL v2"); | |
20406ebf | 420 | MODULE_DESCRIPTION("TC3589x MFD core driver"); |
b4ecd326 | 421 | MODULE_AUTHOR("Hanumath Prasad, Rabin Vincent"); |