esp_scsi: spellcheck 'driver'
[linux/fpc-iii.git] / drivers / mfd / tc3589x.c
blob0072e668c208abc22608b245395323f7122f3283
1 /*
2 * Copyright (C) ST-Ericsson SA 2010
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 */
9 #include <linux/module.h>
10 #include <linux/interrupt.h>
11 #include <linux/irq.h>
12 #include <linux/irqdomain.h>
13 #include <linux/slab.h>
14 #include <linux/i2c.h>
15 #include <linux/of.h>
16 #include <linux/of_device.h>
17 #include <linux/mfd/core.h>
18 #include <linux/mfd/tc3589x.h>
19 #include <linux/err.h>
21 /**
22 * enum tc3589x_version - indicates the TC3589x version
24 enum tc3589x_version {
25 TC3589X_TC35890,
26 TC3589X_TC35892,
27 TC3589X_TC35893,
28 TC3589X_TC35894,
29 TC3589X_TC35895,
30 TC3589X_TC35896,
31 TC3589X_UNKNOWN,
34 #define TC3589x_CLKMODE_MODCTL_SLEEP 0x0
35 #define TC3589x_CLKMODE_MODCTL_OPERATION (1 << 0)
37 /**
38 * tc3589x_reg_read() - read a single TC3589x register
39 * @tc3589x: Device to read from
40 * @reg: Register to read
42 int tc3589x_reg_read(struct tc3589x *tc3589x, u8 reg)
44 int ret;
46 ret = i2c_smbus_read_byte_data(tc3589x->i2c, reg);
47 if (ret < 0)
48 dev_err(tc3589x->dev, "failed to read reg %#x: %d\n",
49 reg, ret);
51 return ret;
53 EXPORT_SYMBOL_GPL(tc3589x_reg_read);
55 /**
56 * tc3589x_reg_read() - write a single TC3589x register
57 * @tc3589x: Device to write to
58 * @reg: Register to read
59 * @data: Value to write
61 int tc3589x_reg_write(struct tc3589x *tc3589x, u8 reg, u8 data)
63 int ret;
65 ret = i2c_smbus_write_byte_data(tc3589x->i2c, reg, data);
66 if (ret < 0)
67 dev_err(tc3589x->dev, "failed to write reg %#x: %d\n",
68 reg, ret);
70 return ret;
72 EXPORT_SYMBOL_GPL(tc3589x_reg_write);
74 /**
75 * tc3589x_block_read() - read multiple TC3589x registers
76 * @tc3589x: Device to read from
77 * @reg: First register
78 * @length: Number of registers
79 * @values: Buffer to write to
81 int tc3589x_block_read(struct tc3589x *tc3589x, u8 reg, u8 length, u8 *values)
83 int ret;
85 ret = i2c_smbus_read_i2c_block_data(tc3589x->i2c, reg, length, values);
86 if (ret < 0)
87 dev_err(tc3589x->dev, "failed to read regs %#x: %d\n",
88 reg, ret);
90 return ret;
92 EXPORT_SYMBOL_GPL(tc3589x_block_read);
94 /**
95 * tc3589x_block_write() - write multiple TC3589x registers
96 * @tc3589x: Device to write to
97 * @reg: First register
98 * @length: Number of registers
99 * @values: Values to write
101 int tc3589x_block_write(struct tc3589x *tc3589x, u8 reg, u8 length,
102 const u8 *values)
104 int ret;
106 ret = i2c_smbus_write_i2c_block_data(tc3589x->i2c, reg, length,
107 values);
108 if (ret < 0)
109 dev_err(tc3589x->dev, "failed to write regs %#x: %d\n",
110 reg, ret);
112 return ret;
114 EXPORT_SYMBOL_GPL(tc3589x_block_write);
117 * tc3589x_set_bits() - set the value of a bitfield in a TC3589x register
118 * @tc3589x: Device to write to
119 * @reg: Register to write
120 * @mask: Mask of bits to set
121 * @values: Value to set
123 int tc3589x_set_bits(struct tc3589x *tc3589x, u8 reg, u8 mask, u8 val)
125 int ret;
127 mutex_lock(&tc3589x->lock);
129 ret = tc3589x_reg_read(tc3589x, reg);
130 if (ret < 0)
131 goto out;
133 ret &= ~mask;
134 ret |= val;
136 ret = tc3589x_reg_write(tc3589x, reg, ret);
138 out:
139 mutex_unlock(&tc3589x->lock);
140 return ret;
142 EXPORT_SYMBOL_GPL(tc3589x_set_bits);
144 static struct resource gpio_resources[] = {
146 .start = TC3589x_INT_GPIIRQ,
147 .end = TC3589x_INT_GPIIRQ,
148 .flags = IORESOURCE_IRQ,
152 static struct resource keypad_resources[] = {
154 .start = TC3589x_INT_KBDIRQ,
155 .end = TC3589x_INT_KBDIRQ,
156 .flags = IORESOURCE_IRQ,
160 static const struct mfd_cell tc3589x_dev_gpio[] = {
162 .name = "tc3589x-gpio",
163 .num_resources = ARRAY_SIZE(gpio_resources),
164 .resources = &gpio_resources[0],
165 .of_compatible = "toshiba,tc3589x-gpio",
169 static const struct mfd_cell tc3589x_dev_keypad[] = {
171 .name = "tc3589x-keypad",
172 .num_resources = ARRAY_SIZE(keypad_resources),
173 .resources = &keypad_resources[0],
174 .of_compatible = "toshiba,tc3589x-keypad",
178 static irqreturn_t tc3589x_irq(int irq, void *data)
180 struct tc3589x *tc3589x = data;
181 int status;
183 again:
184 status = tc3589x_reg_read(tc3589x, TC3589x_IRQST);
185 if (status < 0)
186 return IRQ_NONE;
188 while (status) {
189 int bit = __ffs(status);
190 int virq = irq_create_mapping(tc3589x->domain, bit);
192 handle_nested_irq(virq);
193 status &= ~(1 << bit);
197 * A dummy read or write (to any register) appears to be necessary to
198 * have the last interrupt clear (for example, GPIO IC write) take
199 * effect. In such a case, recheck for any interrupt which is still
200 * pending.
202 status = tc3589x_reg_read(tc3589x, TC3589x_IRQST);
203 if (status)
204 goto again;
206 return IRQ_HANDLED;
209 static int tc3589x_irq_map(struct irq_domain *d, unsigned int virq,
210 irq_hw_number_t hwirq)
212 struct tc3589x *tc3589x = d->host_data;
214 irq_set_chip_data(virq, tc3589x);
215 irq_set_chip_and_handler(virq, &dummy_irq_chip,
216 handle_edge_irq);
217 irq_set_nested_thread(virq, 1);
218 #ifdef CONFIG_ARM
219 set_irq_flags(virq, IRQF_VALID);
220 #else
221 irq_set_noprobe(virq);
222 #endif
224 return 0;
227 static void tc3589x_irq_unmap(struct irq_domain *d, unsigned int virq)
229 #ifdef CONFIG_ARM
230 set_irq_flags(virq, 0);
231 #endif
232 irq_set_chip_and_handler(virq, NULL, NULL);
233 irq_set_chip_data(virq, NULL);
236 static struct irq_domain_ops tc3589x_irq_ops = {
237 .map = tc3589x_irq_map,
238 .unmap = tc3589x_irq_unmap,
239 .xlate = irq_domain_xlate_onecell,
242 static int tc3589x_irq_init(struct tc3589x *tc3589x, struct device_node *np)
244 int base = tc3589x->irq_base;
246 tc3589x->domain = irq_domain_add_simple(
247 np, TC3589x_NR_INTERNAL_IRQS, base,
248 &tc3589x_irq_ops, tc3589x);
250 if (!tc3589x->domain) {
251 dev_err(tc3589x->dev, "Failed to create irqdomain\n");
252 return -ENOSYS;
255 return 0;
258 static int tc3589x_chip_init(struct tc3589x *tc3589x)
260 int manf, ver, ret;
262 manf = tc3589x_reg_read(tc3589x, TC3589x_MANFCODE);
263 if (manf < 0)
264 return manf;
266 ver = tc3589x_reg_read(tc3589x, TC3589x_VERSION);
267 if (ver < 0)
268 return ver;
270 if (manf != TC3589x_MANFCODE_MAGIC) {
271 dev_err(tc3589x->dev, "unknown manufacturer: %#x\n", manf);
272 return -EINVAL;
275 dev_info(tc3589x->dev, "manufacturer: %#x, version: %#x\n", manf, ver);
278 * Put everything except the IRQ module into reset;
279 * also spare the GPIO module for any pin initialization
280 * done during pre-kernel boot
282 ret = tc3589x_reg_write(tc3589x, TC3589x_RSTCTRL,
283 TC3589x_RSTCTRL_TIMRST
284 | TC3589x_RSTCTRL_ROTRST
285 | TC3589x_RSTCTRL_KBDRST);
286 if (ret < 0)
287 return ret;
289 /* Clear the reset interrupt. */
290 return tc3589x_reg_write(tc3589x, TC3589x_RSTINTCLR, 0x1);
293 static int tc3589x_device_init(struct tc3589x *tc3589x)
295 int ret = 0;
296 unsigned int blocks = tc3589x->pdata->block;
298 if (blocks & TC3589x_BLOCK_GPIO) {
299 ret = mfd_add_devices(tc3589x->dev, -1, tc3589x_dev_gpio,
300 ARRAY_SIZE(tc3589x_dev_gpio), NULL,
301 tc3589x->irq_base, tc3589x->domain);
302 if (ret) {
303 dev_err(tc3589x->dev, "failed to add gpio child\n");
304 return ret;
306 dev_info(tc3589x->dev, "added gpio block\n");
309 if (blocks & TC3589x_BLOCK_KEYPAD) {
310 ret = mfd_add_devices(tc3589x->dev, -1, tc3589x_dev_keypad,
311 ARRAY_SIZE(tc3589x_dev_keypad), NULL,
312 tc3589x->irq_base, tc3589x->domain);
313 if (ret) {
314 dev_err(tc3589x->dev, "failed to keypad child\n");
315 return ret;
317 dev_info(tc3589x->dev, "added keypad block\n");
320 return ret;
323 #ifdef CONFIG_OF
324 static const struct of_device_id tc3589x_match[] = {
325 /* Legacy compatible string */
326 { .compatible = "tc3589x", .data = (void *) TC3589X_UNKNOWN },
327 { .compatible = "toshiba,tc35890", .data = (void *) TC3589X_TC35890 },
328 { .compatible = "toshiba,tc35892", .data = (void *) TC3589X_TC35892 },
329 { .compatible = "toshiba,tc35893", .data = (void *) TC3589X_TC35893 },
330 { .compatible = "toshiba,tc35894", .data = (void *) TC3589X_TC35894 },
331 { .compatible = "toshiba,tc35895", .data = (void *) TC3589X_TC35895 },
332 { .compatible = "toshiba,tc35896", .data = (void *) TC3589X_TC35896 },
336 MODULE_DEVICE_TABLE(of, tc3589x_match);
338 static struct tc3589x_platform_data *
339 tc3589x_of_probe(struct device *dev, enum tc3589x_version *version)
341 struct device_node *np = dev->of_node;
342 struct tc3589x_platform_data *pdata;
343 struct device_node *child;
344 const struct of_device_id *of_id;
346 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
347 if (!pdata)
348 return ERR_PTR(-ENOMEM);
350 of_id = of_match_device(tc3589x_match, dev);
351 if (!of_id)
352 return ERR_PTR(-ENODEV);
353 *version = (enum tc3589x_version) of_id->data;
355 for_each_child_of_node(np, child) {
356 if (of_device_is_compatible(child, "toshiba,tc3589x-gpio"))
357 pdata->block |= TC3589x_BLOCK_GPIO;
358 if (of_device_is_compatible(child, "toshiba,tc3589x-keypad"))
359 pdata->block |= TC3589x_BLOCK_KEYPAD;
362 return pdata;
364 #else
365 static inline struct tc3589x_platform_data *
366 tc3589x_of_probe(struct device *dev, enum tc3589x_version *version)
368 dev_err(dev, "no device tree support\n");
369 return ERR_PTR(-ENODEV);
371 #endif
373 static int tc3589x_probe(struct i2c_client *i2c,
374 const struct i2c_device_id *id)
376 struct device_node *np = i2c->dev.of_node;
377 struct tc3589x_platform_data *pdata = dev_get_platdata(&i2c->dev);
378 struct tc3589x *tc3589x;
379 enum tc3589x_version version;
380 int ret;
382 if (!pdata) {
383 pdata = tc3589x_of_probe(&i2c->dev, &version);
384 if (IS_ERR(pdata)) {
385 dev_err(&i2c->dev, "No platform data or DT found\n");
386 return PTR_ERR(pdata);
388 } else {
389 /* When not probing from device tree we have this ID */
390 version = id->driver_data;
393 if (!i2c_check_functionality(i2c->adapter, I2C_FUNC_SMBUS_BYTE_DATA
394 | I2C_FUNC_SMBUS_I2C_BLOCK))
395 return -EIO;
397 tc3589x = devm_kzalloc(&i2c->dev, sizeof(struct tc3589x),
398 GFP_KERNEL);
399 if (!tc3589x)
400 return -ENOMEM;
402 mutex_init(&tc3589x->lock);
404 tc3589x->dev = &i2c->dev;
405 tc3589x->i2c = i2c;
406 tc3589x->pdata = pdata;
407 tc3589x->irq_base = pdata->irq_base;
409 switch (version) {
410 case TC3589X_TC35893:
411 case TC3589X_TC35895:
412 case TC3589X_TC35896:
413 tc3589x->num_gpio = 20;
414 break;
415 case TC3589X_TC35890:
416 case TC3589X_TC35892:
417 case TC3589X_TC35894:
418 case TC3589X_UNKNOWN:
419 default:
420 tc3589x->num_gpio = 24;
421 break;
424 i2c_set_clientdata(i2c, tc3589x);
426 ret = tc3589x_chip_init(tc3589x);
427 if (ret)
428 return ret;
430 ret = tc3589x_irq_init(tc3589x, np);
431 if (ret)
432 return ret;
434 ret = request_threaded_irq(tc3589x->i2c->irq, NULL, tc3589x_irq,
435 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
436 "tc3589x", tc3589x);
437 if (ret) {
438 dev_err(tc3589x->dev, "failed to request IRQ: %d\n", ret);
439 return ret;
442 ret = tc3589x_device_init(tc3589x);
443 if (ret) {
444 dev_err(tc3589x->dev, "failed to add child devices\n");
445 return ret;
448 return 0;
451 static int tc3589x_remove(struct i2c_client *client)
453 struct tc3589x *tc3589x = i2c_get_clientdata(client);
455 mfd_remove_devices(tc3589x->dev);
457 return 0;
460 #ifdef CONFIG_PM_SLEEP
461 static int tc3589x_suspend(struct device *dev)
463 struct tc3589x *tc3589x = dev_get_drvdata(dev);
464 struct i2c_client *client = tc3589x->i2c;
465 int ret = 0;
467 /* put the system to sleep mode */
468 if (!device_may_wakeup(&client->dev))
469 ret = tc3589x_reg_write(tc3589x, TC3589x_CLKMODE,
470 TC3589x_CLKMODE_MODCTL_SLEEP);
472 return ret;
475 static int tc3589x_resume(struct device *dev)
477 struct tc3589x *tc3589x = dev_get_drvdata(dev);
478 struct i2c_client *client = tc3589x->i2c;
479 int ret = 0;
481 /* enable the system into operation */
482 if (!device_may_wakeup(&client->dev))
483 ret = tc3589x_reg_write(tc3589x, TC3589x_CLKMODE,
484 TC3589x_CLKMODE_MODCTL_OPERATION);
486 return ret;
488 #endif
490 static SIMPLE_DEV_PM_OPS(tc3589x_dev_pm_ops, tc3589x_suspend, tc3589x_resume);
492 static const struct i2c_device_id tc3589x_id[] = {
493 { "tc35890", TC3589X_TC35890 },
494 { "tc35892", TC3589X_TC35892 },
495 { "tc35893", TC3589X_TC35893 },
496 { "tc35894", TC3589X_TC35894 },
497 { "tc35895", TC3589X_TC35895 },
498 { "tc35896", TC3589X_TC35896 },
499 { "tc3589x", TC3589X_UNKNOWN },
502 MODULE_DEVICE_TABLE(i2c, tc3589x_id);
504 static struct i2c_driver tc3589x_driver = {
505 .driver = {
506 .name = "tc3589x",
507 .owner = THIS_MODULE,
508 .pm = &tc3589x_dev_pm_ops,
509 .of_match_table = of_match_ptr(tc3589x_match),
511 .probe = tc3589x_probe,
512 .remove = tc3589x_remove,
513 .id_table = tc3589x_id,
516 static int __init tc3589x_init(void)
518 return i2c_add_driver(&tc3589x_driver);
520 subsys_initcall(tc3589x_init);
522 static void __exit tc3589x_exit(void)
524 i2c_del_driver(&tc3589x_driver);
526 module_exit(tc3589x_exit);
528 MODULE_LICENSE("GPL v2");
529 MODULE_DESCRIPTION("TC3589x MFD core driver");
530 MODULE_AUTHOR("Hanumath Prasad, Rabin Vincent");