1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28#include <linux/module.h>
29#include <linux/init.h>
30#include <linux/interrupt.h>
31#include <linux/kthread.h>
32#include <linux/irq.h>
33#include <linux/gpio.h>
34#include <linux/platform_device.h>
35#include <linux/of.h>
36#include <linux/irqdomain.h>
37
38#include <linux/i2c/twl.h>
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53static struct gpio_chip twl_gpiochip;
54static int twl4030_gpio_base;
55static int twl4030_gpio_irq_base;
56
57
58#ifdef MODULE
59#define is_module() true
60#else
61#define is_module() false
62#endif
63
64
65#define MASK_GPIO_CTRL_GPIO0CD1 BIT(0)
66#define MASK_GPIO_CTRL_GPIO1CD2 BIT(1)
67#define MASK_GPIO_CTRL_GPIO_ON BIT(2)
68
69
70#define GPIO_32_MASK 0x0003ffff
71
72
73static DEFINE_MUTEX(gpio_lock);
74
75
76static unsigned int gpio_usage_count;
77
78
79
80
81
82
83static inline int gpio_twl4030_write(u8 address, u8 data)
84{
85 return twl_i2c_write_u8(TWL4030_MODULE_GPIO, data, address);
86}
87
88
89
90
91
92
93
94
95#define TWL4030_LED_LEDEN 0x0
96
97
98#define LEDEN_LEDAON BIT(0)
99#define LEDEN_LEDBON BIT(1)
100#define LEDEN_LEDAEXT BIT(2)
101#define LEDEN_LEDBEXT BIT(3)
102#define LEDEN_LEDAPWM BIT(4)
103#define LEDEN_LEDBPWM BIT(5)
104#define LEDEN_PWM_LENGTHA BIT(6)
105#define LEDEN_PWM_LENGTHB BIT(7)
106
107#define TWL4030_PWMx_PWMxON 0x0
108#define TWL4030_PWMx_PWMxOFF 0x1
109
110#define PWMxON_LENGTH BIT(7)
111
112
113
114
115
116
117static inline int gpio_twl4030_read(u8 address)
118{
119 u8 data;
120 int ret = 0;
121
122 ret = twl_i2c_read_u8(TWL4030_MODULE_GPIO, &data, address);
123 return (ret < 0) ? ret : data;
124}
125
126
127
128static u8 cached_leden;
129
130
131
132
133
134static void twl4030_led_set_value(int led, int value)
135{
136 u8 mask = LEDEN_LEDAON | LEDEN_LEDAPWM;
137 int status;
138
139 if (led)
140 mask <<= 1;
141
142 mutex_lock(&gpio_lock);
143 if (value)
144 cached_leden &= ~mask;
145 else
146 cached_leden |= mask;
147 status = twl_i2c_write_u8(TWL4030_MODULE_LED, cached_leden,
148 TWL4030_LED_LEDEN);
149 mutex_unlock(&gpio_lock);
150}
151
152static int twl4030_set_gpio_direction(int gpio, int is_input)
153{
154 u8 d_bnk = gpio >> 3;
155 u8 d_msk = BIT(gpio & 0x7);
156 u8 reg = 0;
157 u8 base = REG_GPIODATADIR1 + d_bnk;
158 int ret = 0;
159
160 mutex_lock(&gpio_lock);
161 ret = gpio_twl4030_read(base);
162 if (ret >= 0) {
163 if (is_input)
164 reg = ret & ~d_msk;
165 else
166 reg = ret | d_msk;
167
168 ret = gpio_twl4030_write(base, reg);
169 }
170 mutex_unlock(&gpio_lock);
171 return ret;
172}
173
174static int twl4030_set_gpio_dataout(int gpio, int enable)
175{
176 u8 d_bnk = gpio >> 3;
177 u8 d_msk = BIT(gpio & 0x7);
178 u8 base = 0;
179
180 if (enable)
181 base = REG_SETGPIODATAOUT1 + d_bnk;
182 else
183 base = REG_CLEARGPIODATAOUT1 + d_bnk;
184
185 return gpio_twl4030_write(base, d_msk);
186}
187
188static int twl4030_get_gpio_datain(int gpio)
189{
190 u8 d_bnk = gpio >> 3;
191 u8 d_off = gpio & 0x7;
192 u8 base = 0;
193 int ret = 0;
194
195 if (unlikely((gpio >= TWL4030_GPIO_MAX)
196 || !(gpio_usage_count & BIT(gpio))))
197 return -EPERM;
198
199 base = REG_GPIODATAIN1 + d_bnk;
200 ret = gpio_twl4030_read(base);
201 if (ret > 0)
202 ret = (ret >> d_off) & 0x1;
203
204 return ret;
205}
206
207
208
209static int twl_request(struct gpio_chip *chip, unsigned offset)
210{
211 int status = 0;
212
213 mutex_lock(&gpio_lock);
214
215
216 if (offset >= TWL4030_GPIO_MAX) {
217 u8 ledclr_mask = LEDEN_LEDAON | LEDEN_LEDAEXT
218 | LEDEN_LEDAPWM | LEDEN_PWM_LENGTHA;
219 u8 module = TWL4030_MODULE_PWMA;
220
221 offset -= TWL4030_GPIO_MAX;
222 if (offset) {
223 ledclr_mask <<= 1;
224 module = TWL4030_MODULE_PWMB;
225 }
226
227
228 status = twl_i2c_write_u8(module, 0x7f,
229 TWL4030_PWMx_PWMxOFF);
230 if (status < 0)
231 goto done;
232 status = twl_i2c_write_u8(module, 0x7f,
233 TWL4030_PWMx_PWMxON);
234 if (status < 0)
235 goto done;
236
237
238 module = TWL4030_MODULE_LED;
239 status = twl_i2c_read_u8(module, &cached_leden,
240 TWL4030_LED_LEDEN);
241 if (status < 0)
242 goto done;
243 cached_leden &= ~ledclr_mask;
244 status = twl_i2c_write_u8(module, cached_leden,
245 TWL4030_LED_LEDEN);
246 if (status < 0)
247 goto done;
248
249 status = 0;
250 goto done;
251 }
252
253
254 if (!gpio_usage_count) {
255 struct twl4030_gpio_platform_data *pdata;
256 u8 value = MASK_GPIO_CTRL_GPIO_ON;
257
258
259
260
261 pdata = chip->dev->platform_data;
262 if (pdata)
263 value |= pdata->mmc_cd & 0x03;
264
265 status = gpio_twl4030_write(REG_GPIO_CTRL, value);
266 }
267
268 if (!status)
269 gpio_usage_count |= (0x1 << offset);
270
271done:
272 mutex_unlock(&gpio_lock);
273 return status;
274}
275
276static void twl_free(struct gpio_chip *chip, unsigned offset)
277{
278 if (offset >= TWL4030_GPIO_MAX) {
279 twl4030_led_set_value(offset - TWL4030_GPIO_MAX, 1);
280 return;
281 }
282
283 mutex_lock(&gpio_lock);
284
285 gpio_usage_count &= ~BIT(offset);
286
287
288 if (!gpio_usage_count)
289 gpio_twl4030_write(REG_GPIO_CTRL, 0x0);
290
291 mutex_unlock(&gpio_lock);
292}
293
294static int twl_direction_in(struct gpio_chip *chip, unsigned offset)
295{
296 return (offset < TWL4030_GPIO_MAX)
297 ? twl4030_set_gpio_direction(offset, 1)
298 : -EINVAL;
299}
300
301static int twl_get(struct gpio_chip *chip, unsigned offset)
302{
303 int status = 0;
304
305 if (offset < TWL4030_GPIO_MAX)
306 status = twl4030_get_gpio_datain(offset);
307 else if (offset == TWL4030_GPIO_MAX)
308 status = cached_leden & LEDEN_LEDAON;
309 else
310 status = cached_leden & LEDEN_LEDBON;
311 return (status < 0) ? 0 : status;
312}
313
314static int twl_direction_out(struct gpio_chip *chip, unsigned offset, int value)
315{
316 if (offset < TWL4030_GPIO_MAX) {
317 twl4030_set_gpio_dataout(offset, value);
318 return twl4030_set_gpio_direction(offset, 0);
319 } else {
320 twl4030_led_set_value(offset - TWL4030_GPIO_MAX, value);
321 return 0;
322 }
323}
324
325static void twl_set(struct gpio_chip *chip, unsigned offset, int value)
326{
327 if (offset < TWL4030_GPIO_MAX)
328 twl4030_set_gpio_dataout(offset, value);
329 else
330 twl4030_led_set_value(offset - TWL4030_GPIO_MAX, value);
331}
332
333static int twl_to_irq(struct gpio_chip *chip, unsigned offset)
334{
335 return (twl4030_gpio_irq_base && (offset < TWL4030_GPIO_MAX))
336 ? (twl4030_gpio_irq_base + offset)
337 : -EINVAL;
338}
339
340static struct gpio_chip twl_gpiochip = {
341 .label = "twl4030",
342 .owner = THIS_MODULE,
343 .request = twl_request,
344 .free = twl_free,
345 .direction_input = twl_direction_in,
346 .get = twl_get,
347 .direction_output = twl_direction_out,
348 .set = twl_set,
349 .to_irq = twl_to_irq,
350 .can_sleep = 1,
351};
352
353
354
355static int __devinit gpio_twl4030_pulls(u32 ups, u32 downs)
356{
357 u8 message[6];
358 unsigned i, gpio_bit;
359
360
361
362
363 for (gpio_bit = 1, i = 1; i < 6; i++) {
364 u8 bit_mask;
365 unsigned j;
366
367 for (bit_mask = 0, j = 0; j < 8; j += 2, gpio_bit <<= 1) {
368 if (ups & gpio_bit)
369 bit_mask |= 1 << (j + 1);
370 else if (downs & gpio_bit)
371 bit_mask |= 1 << (j + 0);
372 }
373 message[i] = bit_mask;
374 }
375
376 return twl_i2c_write(TWL4030_MODULE_GPIO, message,
377 REG_GPIOPUPDCTR1, 5);
378}
379
380static int __devinit gpio_twl4030_debounce(u32 debounce, u8 mmc_cd)
381{
382 u8 message[4];
383
384
385
386
387 message[1] = (debounce & 0xff) | (mmc_cd & 0x03);
388 debounce >>= 8;
389 message[2] = (debounce & 0xff);
390 debounce >>= 8;
391 message[3] = (debounce & 0x03);
392
393 return twl_i2c_write(TWL4030_MODULE_GPIO, message,
394 REG_GPIO_DEBEN1, 3);
395}
396
397static int gpio_twl4030_remove(struct platform_device *pdev);
398
399static struct twl4030_gpio_platform_data *of_gpio_twl4030(struct device *dev)
400{
401 struct twl4030_gpio_platform_data *omap_twl_info;
402
403 omap_twl_info = devm_kzalloc(dev, sizeof(*omap_twl_info), GFP_KERNEL);
404 if (!omap_twl_info)
405 return NULL;
406
407 omap_twl_info->use_leds = of_property_read_bool(dev->of_node,
408 "ti,use-leds");
409
410 of_property_read_u32(dev->of_node, "ti,debounce",
411 &omap_twl_info->debounce);
412 of_property_read_u32(dev->of_node, "ti,mmc-cd",
413 (u32 *)&omap_twl_info->mmc_cd);
414 of_property_read_u32(dev->of_node, "ti,pullups",
415 &omap_twl_info->pullups);
416 of_property_read_u32(dev->of_node, "ti,pulldowns",
417 &omap_twl_info->pulldowns);
418
419 return omap_twl_info;
420}
421
422static int __devinit gpio_twl4030_probe(struct platform_device *pdev)
423{
424 struct twl4030_gpio_platform_data *pdata = pdev->dev.platform_data;
425 struct device_node *node = pdev->dev.of_node;
426 int ret, irq_base;
427
428
429 if (is_module()) {
430 dev_err(&pdev->dev, "can't dispatch IRQs from modules\n");
431 goto no_irqs;
432 }
433
434 irq_base = irq_alloc_descs(-1, 0, TWL4030_GPIO_MAX, 0);
435 if (irq_base < 0) {
436 dev_err(&pdev->dev, "Failed to alloc irq_descs\n");
437 return irq_base;
438 }
439
440 irq_domain_add_legacy(node, TWL4030_GPIO_MAX, irq_base, 0,
441 &irq_domain_simple_ops, NULL);
442
443 ret = twl4030_sih_setup(&pdev->dev, TWL4030_MODULE_GPIO, irq_base);
444 if (ret < 0)
445 return ret;
446
447 twl4030_gpio_irq_base = irq_base;
448
449no_irqs:
450 twl_gpiochip.base = -1;
451 twl_gpiochip.ngpio = TWL4030_GPIO_MAX;
452 twl_gpiochip.dev = &pdev->dev;
453
454 if (node)
455 pdata = of_gpio_twl4030(&pdev->dev);
456
457 if (pdata == NULL) {
458 dev_err(&pdev->dev, "Platform data is missing\n");
459 return -ENXIO;
460 }
461
462
463
464
465
466
467
468 ret = gpio_twl4030_pulls(pdata->pullups, pdata->pulldowns);
469 if (ret)
470 dev_dbg(&pdev->dev, "pullups %.05x %.05x --> %d\n",
471 pdata->pullups, pdata->pulldowns, ret);
472
473 ret = gpio_twl4030_debounce(pdata->debounce, pdata->mmc_cd);
474 if (ret)
475 dev_dbg(&pdev->dev, "debounce %.03x %.01x --> %d\n",
476 pdata->debounce, pdata->mmc_cd, ret);
477
478
479
480
481
482 if (pdata->use_leds)
483 twl_gpiochip.ngpio += 2;
484
485 ret = gpiochip_add(&twl_gpiochip);
486 if (ret < 0) {
487 dev_err(&pdev->dev, "could not register gpiochip, %d\n", ret);
488 twl_gpiochip.ngpio = 0;
489 gpio_twl4030_remove(pdev);
490 goto out;
491 }
492
493 twl4030_gpio_base = twl_gpiochip.base;
494
495 if (pdata && pdata->setup) {
496 int status;
497
498 status = pdata->setup(&pdev->dev,
499 twl4030_gpio_base, TWL4030_GPIO_MAX);
500 if (status)
501 dev_dbg(&pdev->dev, "setup --> %d\n", status);
502 }
503
504out:
505 return ret;
506}
507
508
509static int gpio_twl4030_remove(struct platform_device *pdev)
510{
511 struct twl4030_gpio_platform_data *pdata = pdev->dev.platform_data;
512 int status;
513
514 if (pdata && pdata->teardown) {
515 status = pdata->teardown(&pdev->dev,
516 twl4030_gpio_base, TWL4030_GPIO_MAX);
517 if (status) {
518 dev_dbg(&pdev->dev, "teardown --> %d\n", status);
519 return status;
520 }
521 }
522
523 status = gpiochip_remove(&twl_gpiochip);
524 if (status < 0)
525 return status;
526
527 if (is_module())
528 return 0;
529
530
531 WARN_ON(1);
532 return -EIO;
533}
534
535static const struct of_device_id twl_gpio_match[] = {
536 { .compatible = "ti,twl4030-gpio", },
537 { },
538};
539MODULE_DEVICE_TABLE(of, twl_gpio_match);
540
541
542MODULE_ALIAS("platform:twl4030_gpio");
543
544static struct platform_driver gpio_twl4030_driver = {
545 .driver = {
546 .name = "twl4030_gpio",
547 .owner = THIS_MODULE,
548 .of_match_table = of_match_ptr(twl_gpio_match),
549 },
550 .probe = gpio_twl4030_probe,
551 .remove = gpio_twl4030_remove,
552};
553
554static int __init gpio_twl4030_init(void)
555{
556 return platform_driver_register(&gpio_twl4030_driver);
557}
558subsys_initcall(gpio_twl4030_init);
559
560static void __exit gpio_twl4030_exit(void)
561{
562 platform_driver_unregister(&gpio_twl4030_driver);
563}
564module_exit(gpio_twl4030_exit);
565
566MODULE_AUTHOR("Texas Instruments, Inc.");
567MODULE_DESCRIPTION("GPIO interface for TWL4030");
568MODULE_LICENSE("GPL");
569