Merge tag 'armsoc-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/arm...
[deliverable/linux.git] / drivers / mfd / tc3589x.c
CommitLineData
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>
15e27b10 12#include <linux/irqdomain.h>
b4ecd326
RV
13#include <linux/slab.h>
14#include <linux/i2c.h>
a435ae1d 15#include <linux/of.h>
a381b13e 16#include <linux/of_device.h>
b4ecd326 17#include <linux/mfd/core.h>
c6eda6c5 18#include <linux/mfd/tc3589x.h>
a381b13e 19#include <linux/err.h>
b4ecd326 20
e64c1eb4
LW
21/**
22 * enum tc3589x_version - indicates the TC3589x version
23 */
24enum tc3589x_version {
25 TC3589X_TC35890,
26 TC3589X_TC35892,
27 TC3589X_TC35893,
28 TC3589X_TC35894,
29 TC3589X_TC35895,
30 TC3589X_TC35896,
31 TC3589X_UNKNOWN,
32};
33
593e9d70
SI
34#define TC3589x_CLKMODE_MODCTL_SLEEP 0x0
35#define TC3589x_CLKMODE_MODCTL_OPERATION (1 << 0)
36
b4ecd326 37/**
20406ebf
SI
38 * tc3589x_reg_read() - read a single TC3589x register
39 * @tc3589x: Device to read from
b4ecd326
RV
40 * @reg: Register to read
41 */
20406ebf 42int tc3589x_reg_read(struct tc3589x *tc3589x, u8 reg)
b4ecd326
RV
43{
44 int ret;
45
20406ebf 46 ret = i2c_smbus_read_byte_data(tc3589x->i2c, reg);
b4ecd326 47 if (ret < 0)
20406ebf 48 dev_err(tc3589x->dev, "failed to read reg %#x: %d\n",
b4ecd326
RV
49 reg, ret);
50
51 return ret;
52}
20406ebf 53EXPORT_SYMBOL_GPL(tc3589x_reg_read);
b4ecd326
RV
54
55/**
20406ebf
SI
56 * tc3589x_reg_read() - write a single TC3589x register
57 * @tc3589x: Device to write to
b4ecd326
RV
58 * @reg: Register to read
59 * @data: Value to write
60 */
20406ebf 61int tc3589x_reg_write(struct tc3589x *tc3589x, u8 reg, u8 data)
b4ecd326
RV
62{
63 int ret;
64
20406ebf 65 ret = i2c_smbus_write_byte_data(tc3589x->i2c, reg, data);
b4ecd326 66 if (ret < 0)
20406ebf 67 dev_err(tc3589x->dev, "failed to write reg %#x: %d\n",
b4ecd326
RV
68 reg, ret);
69
70 return ret;
71}
20406ebf 72EXPORT_SYMBOL_GPL(tc3589x_reg_write);
b4ecd326
RV
73
74/**
20406ebf
SI
75 * tc3589x_block_read() - read multiple TC3589x registers
76 * @tc3589x: Device to read from
b4ecd326
RV
77 * @reg: First register
78 * @length: Number of registers
79 * @values: Buffer to write to
80 */
20406ebf 81int tc3589x_block_read(struct tc3589x *tc3589x, u8 reg, u8 length, u8 *values)
b4ecd326
RV
82{
83 int ret;
84
20406ebf 85 ret = i2c_smbus_read_i2c_block_data(tc3589x->i2c, reg, length, values);
b4ecd326 86 if (ret < 0)
20406ebf 87 dev_err(tc3589x->dev, "failed to read regs %#x: %d\n",
b4ecd326
RV
88 reg, ret);
89
90 return ret;
91}
20406ebf 92EXPORT_SYMBOL_GPL(tc3589x_block_read);
b4ecd326
RV
93
94/**
20406ebf
SI
95 * tc3589x_block_write() - write multiple TC3589x registers
96 * @tc3589x: Device to write to
b4ecd326
RV
97 * @reg: First register
98 * @length: Number of registers
99 * @values: Values to write
100 */
20406ebf 101int tc3589x_block_write(struct tc3589x *tc3589x, u8 reg, u8 length,
b4ecd326
RV
102 const u8 *values)
103{
104 int ret;
105
20406ebf 106 ret = i2c_smbus_write_i2c_block_data(tc3589x->i2c, reg, length,
b4ecd326
RV
107 values);
108 if (ret < 0)
20406ebf 109 dev_err(tc3589x->dev, "failed to write regs %#x: %d\n",
b4ecd326
RV
110 reg, ret);
111
112 return ret;
113}
20406ebf 114EXPORT_SYMBOL_GPL(tc3589x_block_write);
b4ecd326
RV
115
116/**
20406ebf
SI
117 * tc3589x_set_bits() - set the value of a bitfield in a TC3589x register
118 * @tc3589x: Device to write to
b4ecd326
RV
119 * @reg: Register to write
120 * @mask: Mask of bits to set
121 * @values: Value to set
122 */
20406ebf 123int tc3589x_set_bits(struct tc3589x *tc3589x, u8 reg, u8 mask, u8 val)
b4ecd326
RV
124{
125 int ret;
126
20406ebf 127 mutex_lock(&tc3589x->lock);
b4ecd326 128
20406ebf 129 ret = tc3589x_reg_read(tc3589x, reg);
b4ecd326
RV
130 if (ret < 0)
131 goto out;
132
133 ret &= ~mask;
134 ret |= val;
135
20406ebf 136 ret = tc3589x_reg_write(tc3589x, reg, ret);
b4ecd326
RV
137
138out:
20406ebf 139 mutex_unlock(&tc3589x->lock);
b4ecd326
RV
140 return ret;
141}
20406ebf 142EXPORT_SYMBOL_GPL(tc3589x_set_bits);
b4ecd326
RV
143
144static struct resource gpio_resources[] = {
145 {
20406ebf
SI
146 .start = TC3589x_INT_GPIIRQ,
147 .end = TC3589x_INT_GPIIRQ,
b4ecd326
RV
148 .flags = IORESOURCE_IRQ,
149 },
150};
151
09c730a4
SI
152static struct resource keypad_resources[] = {
153 {
154 .start = TC3589x_INT_KBDIRQ,
155 .end = TC3589x_INT_KBDIRQ,
156 .flags = IORESOURCE_IRQ,
157 },
158};
159
afb580a9 160static const struct mfd_cell tc3589x_dev_gpio[] = {
b4ecd326 161 {
20406ebf 162 .name = "tc3589x-gpio",
b4ecd326
RV
163 .num_resources = ARRAY_SIZE(gpio_resources),
164 .resources = &gpio_resources[0],
a381b13e 165 .of_compatible = "toshiba,tc3589x-gpio",
b4ecd326
RV
166 },
167};
168
afb580a9 169static const struct mfd_cell tc3589x_dev_keypad[] = {
09c730a4
SI
170 {
171 .name = "tc3589x-keypad",
172 .num_resources = ARRAY_SIZE(keypad_resources),
173 .resources = &keypad_resources[0],
a381b13e 174 .of_compatible = "toshiba,tc3589x-keypad",
09c730a4
SI
175 },
176};
177
20406ebf 178static irqreturn_t tc3589x_irq(int irq, void *data)
b4ecd326 179{
20406ebf 180 struct tc3589x *tc3589x = data;
b4ecd326
RV
181 int status;
182
bd77efd0 183again:
20406ebf 184 status = tc3589x_reg_read(tc3589x, TC3589x_IRQST);
b4ecd326
RV
185 if (status < 0)
186 return IRQ_NONE;
187
188 while (status) {
189 int bit = __ffs(status);
15e27b10 190 int virq = irq_create_mapping(tc3589x->domain, bit);
b4ecd326 191
15e27b10 192 handle_nested_irq(virq);
b4ecd326
RV
193 status &= ~(1 << bit);
194 }
195
196 /*
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
bd77efd0
SI
199 * effect. In such a case, recheck for any interrupt which is still
200 * pending.
b4ecd326 201 */
bd77efd0
SI
202 status = tc3589x_reg_read(tc3589x, TC3589x_IRQST);
203 if (status)
204 goto again;
b4ecd326
RV
205
206 return IRQ_HANDLED;
207}
208
15e27b10
LJ
209static int tc3589x_irq_map(struct irq_domain *d, unsigned int virq,
210 irq_hw_number_t hwirq)
b4ecd326 211{
15e27b10 212 struct tc3589x *tc3589x = d->host_data;
b4ecd326 213
15e27b10
LJ
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);
b4ecd326 218#ifdef CONFIG_ARM
15e27b10 219 set_irq_flags(virq, IRQF_VALID);
b4ecd326 220#else
15e27b10 221 irq_set_noprobe(virq);
b4ecd326 222#endif
b4ecd326
RV
223
224 return 0;
225}
226
15e27b10 227static void tc3589x_irq_unmap(struct irq_domain *d, unsigned int virq)
b4ecd326 228{
b4ecd326 229#ifdef CONFIG_ARM
15e27b10 230 set_irq_flags(virq, 0);
b4ecd326 231#endif
15e27b10
LJ
232 irq_set_chip_and_handler(virq, NULL, NULL);
233 irq_set_chip_data(virq, NULL);
234}
235
7ce7b26f 236static const struct irq_domain_ops tc3589x_irq_ops = {
1f0529b4 237 .map = tc3589x_irq_map,
15e27b10 238 .unmap = tc3589x_irq_unmap,
627918ed 239 .xlate = irq_domain_xlate_onecell,
15e27b10
LJ
240};
241
a435ae1d 242static int tc3589x_irq_init(struct tc3589x *tc3589x, struct device_node *np)
15e27b10 243{
1f0529b4 244 tc3589x->domain = irq_domain_add_simple(
90f2d0f7 245 np, TC3589x_NR_INTERNAL_IRQS, 0,
1f0529b4 246 &tc3589x_irq_ops, tc3589x);
15e27b10
LJ
247
248 if (!tc3589x->domain) {
249 dev_err(tc3589x->dev, "Failed to create irqdomain\n");
250 return -ENOSYS;
251 }
252
253 return 0;
b4ecd326
RV
254}
255
20406ebf 256static int tc3589x_chip_init(struct tc3589x *tc3589x)
b4ecd326
RV
257{
258 int manf, ver, ret;
259
20406ebf 260 manf = tc3589x_reg_read(tc3589x, TC3589x_MANFCODE);
b4ecd326
RV
261 if (manf < 0)
262 return manf;
263
20406ebf 264 ver = tc3589x_reg_read(tc3589x, TC3589x_VERSION);
b4ecd326
RV
265 if (ver < 0)
266 return ver;
267
20406ebf
SI
268 if (manf != TC3589x_MANFCODE_MAGIC) {
269 dev_err(tc3589x->dev, "unknown manufacturer: %#x\n", manf);
b4ecd326
RV
270 return -EINVAL;
271 }
272
20406ebf 273 dev_info(tc3589x->dev, "manufacturer: %#x, version: %#x\n", manf, ver);
b4ecd326 274
523bc382
SI
275 /*
276 * Put everything except the IRQ module into reset;
277 * also spare the GPIO module for any pin initialization
278 * done during pre-kernel boot
279 */
20406ebf
SI
280 ret = tc3589x_reg_write(tc3589x, TC3589x_RSTCTRL,
281 TC3589x_RSTCTRL_TIMRST
282 | TC3589x_RSTCTRL_ROTRST
523bc382 283 | TC3589x_RSTCTRL_KBDRST);
b4ecd326
RV
284 if (ret < 0)
285 return ret;
286
287 /* Clear the reset interrupt. */
20406ebf 288 return tc3589x_reg_write(tc3589x, TC3589x_RSTINTCLR, 0x1);
b4ecd326
RV
289}
290
f791be49 291static int tc3589x_device_init(struct tc3589x *tc3589x)
611b7590
SI
292{
293 int ret = 0;
294 unsigned int blocks = tc3589x->pdata->block;
295
296 if (blocks & TC3589x_BLOCK_GPIO) {
297 ret = mfd_add_devices(tc3589x->dev, -1, tc3589x_dev_gpio,
55692af5 298 ARRAY_SIZE(tc3589x_dev_gpio), NULL,
90f2d0f7 299 0, tc3589x->domain);
611b7590
SI
300 if (ret) {
301 dev_err(tc3589x->dev, "failed to add gpio child\n");
302 return ret;
303 }
304 dev_info(tc3589x->dev, "added gpio block\n");
305 }
306
09c730a4
SI
307 if (blocks & TC3589x_BLOCK_KEYPAD) {
308 ret = mfd_add_devices(tc3589x->dev, -1, tc3589x_dev_keypad,
55692af5 309 ARRAY_SIZE(tc3589x_dev_keypad), NULL,
90f2d0f7 310 0, tc3589x->domain);
09c730a4
SI
311 if (ret) {
312 dev_err(tc3589x->dev, "failed to keypad child\n");
313 return ret;
314 }
315 dev_info(tc3589x->dev, "added keypad block\n");
316 }
611b7590 317
09c730a4 318 return ret;
611b7590
SI
319}
320
a381b13e
LW
321static const struct of_device_id tc3589x_match[] = {
322 /* Legacy compatible string */
323 { .compatible = "tc3589x", .data = (void *) TC3589X_UNKNOWN },
324 { .compatible = "toshiba,tc35890", .data = (void *) TC3589X_TC35890 },
325 { .compatible = "toshiba,tc35892", .data = (void *) TC3589X_TC35892 },
326 { .compatible = "toshiba,tc35893", .data = (void *) TC3589X_TC35893 },
327 { .compatible = "toshiba,tc35894", .data = (void *) TC3589X_TC35894 },
328 { .compatible = "toshiba,tc35895", .data = (void *) TC3589X_TC35895 },
329 { .compatible = "toshiba,tc35896", .data = (void *) TC3589X_TC35896 },
330 { }
331};
332
333MODULE_DEVICE_TABLE(of, tc3589x_match);
334
335static struct tc3589x_platform_data *
336tc3589x_of_probe(struct device *dev, enum tc3589x_version *version)
a435ae1d 337{
a381b13e
LW
338 struct device_node *np = dev->of_node;
339 struct tc3589x_platform_data *pdata;
a435ae1d 340 struct device_node *child;
a381b13e
LW
341 const struct of_device_id *of_id;
342
343 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
344 if (!pdata)
345 return ERR_PTR(-ENOMEM);
346
347 of_id = of_match_device(tc3589x_match, dev);
348 if (!of_id)
349 return ERR_PTR(-ENODEV);
350 *version = (enum tc3589x_version) of_id->data;
a435ae1d
LJ
351
352 for_each_child_of_node(np, child) {
a381b13e 353 if (of_device_is_compatible(child, "toshiba,tc3589x-gpio"))
a435ae1d 354 pdata->block |= TC3589x_BLOCK_GPIO;
a381b13e 355 if (of_device_is_compatible(child, "toshiba,tc3589x-keypad"))
a435ae1d 356 pdata->block |= TC3589x_BLOCK_KEYPAD;
a435ae1d
LJ
357 }
358
a381b13e 359 return pdata;
a435ae1d
LJ
360}
361
f791be49 362static int tc3589x_probe(struct i2c_client *i2c,
b4ecd326
RV
363 const struct i2c_device_id *id)
364{
a435ae1d 365 struct device_node *np = i2c->dev.of_node;
a381b13e 366 struct tc3589x_platform_data *pdata = dev_get_platdata(&i2c->dev);
20406ebf 367 struct tc3589x *tc3589x;
a381b13e 368 enum tc3589x_version version;
b4ecd326
RV
369 int ret;
370
a435ae1d 371 if (!pdata) {
a381b13e
LW
372 pdata = tc3589x_of_probe(&i2c->dev, &version);
373 if (IS_ERR(pdata)) {
a435ae1d 374 dev_err(&i2c->dev, "No platform data or DT found\n");
a381b13e 375 return PTR_ERR(pdata);
a435ae1d 376 }
a381b13e
LW
377 } else {
378 /* When not probing from device tree we have this ID */
379 version = id->driver_data;
a435ae1d
LJ
380 }
381
b4ecd326
RV
382 if (!i2c_check_functionality(i2c->adapter, I2C_FUNC_SMBUS_BYTE_DATA
383 | I2C_FUNC_SMBUS_I2C_BLOCK))
384 return -EIO;
385
1383e00f
JH
386 tc3589x = devm_kzalloc(&i2c->dev, sizeof(struct tc3589x),
387 GFP_KERNEL);
20406ebf 388 if (!tc3589x)
b4ecd326
RV
389 return -ENOMEM;
390
20406ebf 391 mutex_init(&tc3589x->lock);
b4ecd326 392
20406ebf
SI
393 tc3589x->dev = &i2c->dev;
394 tc3589x->i2c = i2c;
395 tc3589x->pdata = pdata;
e64c1eb4 396
a381b13e 397 switch (version) {
e64c1eb4
LW
398 case TC3589X_TC35893:
399 case TC3589X_TC35895:
400 case TC3589X_TC35896:
401 tc3589x->num_gpio = 20;
402 break;
403 case TC3589X_TC35890:
404 case TC3589X_TC35892:
405 case TC3589X_TC35894:
406 case TC3589X_UNKNOWN:
407 default:
408 tc3589x->num_gpio = 24;
409 break;
410 }
b4ecd326 411
20406ebf 412 i2c_set_clientdata(i2c, tc3589x);
b4ecd326 413
20406ebf 414 ret = tc3589x_chip_init(tc3589x);
b4ecd326 415 if (ret)
1383e00f 416 return ret;
b4ecd326 417
a435ae1d 418 ret = tc3589x_irq_init(tc3589x, np);
b4ecd326 419 if (ret)
1383e00f 420 return ret;
b4ecd326 421
20406ebf 422 ret = request_threaded_irq(tc3589x->i2c->irq, NULL, tc3589x_irq,
b4ecd326 423 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
20406ebf 424 "tc3589x", tc3589x);
b4ecd326 425 if (ret) {
20406ebf 426 dev_err(tc3589x->dev, "failed to request IRQ: %d\n", ret);
1383e00f 427 return ret;
b4ecd326
RV
428 }
429
611b7590 430 ret = tc3589x_device_init(tc3589x);
b4ecd326 431 if (ret) {
611b7590 432 dev_err(tc3589x->dev, "failed to add child devices\n");
1383e00f 433 return ret;
b4ecd326
RV
434 }
435
436 return 0;
b4ecd326
RV
437}
438
4740f73f 439static int tc3589x_remove(struct i2c_client *client)
b4ecd326 440{
20406ebf 441 struct tc3589x *tc3589x = i2c_get_clientdata(client);
b4ecd326 442
20406ebf 443 mfd_remove_devices(tc3589x->dev);
b4ecd326 444
b4ecd326
RV
445 return 0;
446}
447
930bf022 448#ifdef CONFIG_PM_SLEEP
593e9d70
SI
449static int tc3589x_suspend(struct device *dev)
450{
451 struct tc3589x *tc3589x = dev_get_drvdata(dev);
452 struct i2c_client *client = tc3589x->i2c;
453 int ret = 0;
454
455 /* put the system to sleep mode */
456 if (!device_may_wakeup(&client->dev))
457 ret = tc3589x_reg_write(tc3589x, TC3589x_CLKMODE,
458 TC3589x_CLKMODE_MODCTL_SLEEP);
459
460 return ret;
461}
462
463static int tc3589x_resume(struct device *dev)
464{
465 struct tc3589x *tc3589x = dev_get_drvdata(dev);
466 struct i2c_client *client = tc3589x->i2c;
467 int ret = 0;
468
469 /* enable the system into operation */
470 if (!device_may_wakeup(&client->dev))
471 ret = tc3589x_reg_write(tc3589x, TC3589x_CLKMODE,
472 TC3589x_CLKMODE_MODCTL_OPERATION);
473
474 return ret;
475}
54d8e2c3 476#endif
593e9d70 477
930bf022
AL
478static SIMPLE_DEV_PM_OPS(tc3589x_dev_pm_ops, tc3589x_suspend, tc3589x_resume);
479
20406ebf 480static const struct i2c_device_id tc3589x_id[] = {
e64c1eb4
LW
481 { "tc35890", TC3589X_TC35890 },
482 { "tc35892", TC3589X_TC35892 },
483 { "tc35893", TC3589X_TC35893 },
484 { "tc35894", TC3589X_TC35894 },
485 { "tc35895", TC3589X_TC35895 },
486 { "tc35896", TC3589X_TC35896 },
487 { "tc3589x", TC3589X_UNKNOWN },
b4ecd326
RV
488 { }
489};
20406ebf 490MODULE_DEVICE_TABLE(i2c, tc3589x_id);
b4ecd326 491
20406ebf 492static struct i2c_driver tc3589x_driver = {
a381b13e
LW
493 .driver = {
494 .name = "tc3589x",
495 .owner = THIS_MODULE,
496 .pm = &tc3589x_dev_pm_ops,
497 .of_match_table = of_match_ptr(tc3589x_match),
498 },
20406ebf 499 .probe = tc3589x_probe,
84449216 500 .remove = tc3589x_remove,
20406ebf 501 .id_table = tc3589x_id,
b4ecd326
RV
502};
503
20406ebf 504static int __init tc3589x_init(void)
b4ecd326 505{
20406ebf 506 return i2c_add_driver(&tc3589x_driver);
b4ecd326 507}
20406ebf 508subsys_initcall(tc3589x_init);
b4ecd326 509
20406ebf 510static void __exit tc3589x_exit(void)
b4ecd326 511{
20406ebf 512 i2c_del_driver(&tc3589x_driver);
b4ecd326 513}
20406ebf 514module_exit(tc3589x_exit);
b4ecd326
RV
515
516MODULE_LICENSE("GPL v2");
20406ebf 517MODULE_DESCRIPTION("TC3589x MFD core driver");
b4ecd326 518MODULE_AUTHOR("Hanumath Prasad, Rabin Vincent");
This page took 0.446132 seconds and 5 git commands to generate.