• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /asuswrt-rt-n18u-9.0.0.4.380.2695/release/src-rt-6.x.4708/linux/linux-2.6/drivers/gpio/
1/*
2 *  max732x.c - I2C Port Expander with 8/16 I/O
3 *
4 *  Copyright (C) 2007 Marvell International Ltd.
5 *  Copyright (C) 2008 Jack Ren <jack.ren@marvell.com>
6 *  Copyright (C) 2008 Eric Miao <eric.miao@marvell.com>
7 *
8 *  Derived from drivers/gpio/pca953x.c
9 *
10 *  This program is free software; you can redistribute it and/or modify
11 *  it under the terms of the GNU General Public License as published by
12 *  the Free Software Foundation; version 2 of the License.
13 */
14
15#include <linux/module.h>
16#include <linux/init.h>
17#include <linux/slab.h>
18#include <linux/string.h>
19#include <linux/gpio.h>
20#include <linux/interrupt.h>
21#include <linux/irq.h>
22#include <linux/i2c.h>
23#include <linux/i2c/max732x.h>
24
25
26
27#define PORT_NONE	0x0	/* '/' No Port */
28#define PORT_OUTPUT	0x1	/* 'O' Push-Pull, Output Only */
29#define PORT_INPUT	0x2	/* 'I' Input Only */
30#define PORT_OPENDRAIN	0x3	/* 'P' Open-Drain, I/O */
31
32#define IO_4I4O		0x5AA5	/* O7 O6 I5 I4 I3 I2 O1 O0 */
33#define IO_4P4O		0x5FF5	/* O7 O6 P5 P4 P3 P2 O1 O0 */
34#define IO_8I		0xAAAA	/* I7 I6 I5 I4 I3 I2 I1 I0 */
35#define IO_8P		0xFFFF	/* P7 P6 P5 P4 P3 P2 P1 P0 */
36#define IO_8O		0x5555	/* O7 O6 O5 O4 O3 O2 O1 O0 */
37
38#define GROUP_A(x)	((x) & 0xffff)	/* I2C Addr: 0b'110xxxx */
39#define GROUP_B(x)	((x) << 16)	/* I2C Addr: 0b'101xxxx */
40
41#define INT_NONE	0x0	/* No interrupt capability */
42#define INT_NO_MASK	0x1	/* Has interrupts, no mask */
43#define INT_INDEP_MASK	0x2	/* Has interrupts, independent mask */
44#define INT_MERGED_MASK 0x3	/* Has interrupts, merged mask */
45
46#define INT_CAPS(x)	(((uint64_t)(x)) << 32)
47
48enum {
49	MAX7319,
50	MAX7320,
51	MAX7321,
52	MAX7322,
53	MAX7323,
54	MAX7324,
55	MAX7325,
56	MAX7326,
57	MAX7327,
58};
59
60static uint64_t max732x_features[] = {
61	[MAX7319] = GROUP_A(IO_8I) | INT_CAPS(INT_MERGED_MASK),
62	[MAX7320] = GROUP_B(IO_8O),
63	[MAX7321] = GROUP_A(IO_8P) | INT_CAPS(INT_NO_MASK),
64	[MAX7322] = GROUP_A(IO_4I4O) | INT_CAPS(INT_MERGED_MASK),
65	[MAX7323] = GROUP_A(IO_4P4O) | INT_CAPS(INT_INDEP_MASK),
66	[MAX7324] = GROUP_A(IO_8I) | GROUP_B(IO_8O) | INT_CAPS(INT_MERGED_MASK),
67	[MAX7325] = GROUP_A(IO_8P) | GROUP_B(IO_8O) | INT_CAPS(INT_NO_MASK),
68	[MAX7326] = GROUP_A(IO_4I4O) | GROUP_B(IO_8O) | INT_CAPS(INT_MERGED_MASK),
69	[MAX7327] = GROUP_A(IO_4P4O) | GROUP_B(IO_8O) | INT_CAPS(INT_NO_MASK),
70};
71
72static const struct i2c_device_id max732x_id[] = {
73	{ "max7319", MAX7319 },
74	{ "max7320", MAX7320 },
75	{ "max7321", MAX7321 },
76	{ "max7322", MAX7322 },
77	{ "max7323", MAX7323 },
78	{ "max7324", MAX7324 },
79	{ "max7325", MAX7325 },
80	{ "max7326", MAX7326 },
81	{ "max7327", MAX7327 },
82	{ },
83};
84MODULE_DEVICE_TABLE(i2c, max732x_id);
85
86struct max732x_chip {
87	struct gpio_chip gpio_chip;
88
89	struct i2c_client *client;	/* "main" client */
90	struct i2c_client *client_dummy;
91	struct i2c_client *client_group_a;
92	struct i2c_client *client_group_b;
93
94	unsigned int	mask_group_a;
95	unsigned int	dir_input;
96	unsigned int	dir_output;
97
98	struct mutex	lock;
99	uint8_t		reg_out[2];
100
101#ifdef CONFIG_GPIO_MAX732X_IRQ
102	struct mutex	irq_lock;
103	int		irq_base;
104	uint8_t		irq_mask;
105	uint8_t		irq_mask_cur;
106	uint8_t		irq_trig_raise;
107	uint8_t		irq_trig_fall;
108	uint8_t		irq_features;
109#endif
110};
111
112static int max732x_writeb(struct max732x_chip *chip, int group_a, uint8_t val)
113{
114	struct i2c_client *client;
115	int ret;
116
117	client = group_a ? chip->client_group_a : chip->client_group_b;
118	ret = i2c_smbus_write_byte(client, val);
119	if (ret < 0) {
120		dev_err(&client->dev, "failed writing\n");
121		return ret;
122	}
123
124	return 0;
125}
126
127static int max732x_readb(struct max732x_chip *chip, int group_a, uint8_t *val)
128{
129	struct i2c_client *client;
130	int ret;
131
132	client = group_a ? chip->client_group_a : chip->client_group_b;
133	ret = i2c_smbus_read_byte(client);
134	if (ret < 0) {
135		dev_err(&client->dev, "failed reading\n");
136		return ret;
137	}
138
139	*val = (uint8_t)ret;
140	return 0;
141}
142
143static inline int is_group_a(struct max732x_chip *chip, unsigned off)
144{
145	return (1u << off) & chip->mask_group_a;
146}
147
148static int max732x_gpio_get_value(struct gpio_chip *gc, unsigned off)
149{
150	struct max732x_chip *chip;
151	uint8_t reg_val;
152	int ret;
153
154	chip = container_of(gc, struct max732x_chip, gpio_chip);
155
156	ret = max732x_readb(chip, is_group_a(chip, off), &reg_val);
157	if (ret < 0)
158		return 0;
159
160	return reg_val & (1u << (off & 0x7));
161}
162
163static void max732x_gpio_set_value(struct gpio_chip *gc, unsigned off, int val)
164{
165	struct max732x_chip *chip;
166	uint8_t reg_out, mask = 1u << (off & 0x7);
167	int ret;
168
169	chip = container_of(gc, struct max732x_chip, gpio_chip);
170
171	mutex_lock(&chip->lock);
172
173	reg_out = (off > 7) ? chip->reg_out[1] : chip->reg_out[0];
174	reg_out = (val) ? reg_out | mask : reg_out & ~mask;
175
176	ret = max732x_writeb(chip, is_group_a(chip, off), reg_out);
177	if (ret < 0)
178		goto out;
179
180	/* update the shadow register then */
181	if (off > 7)
182		chip->reg_out[1] = reg_out;
183	else
184		chip->reg_out[0] = reg_out;
185out:
186	mutex_unlock(&chip->lock);
187}
188
189static int max732x_gpio_direction_input(struct gpio_chip *gc, unsigned off)
190{
191	struct max732x_chip *chip;
192	unsigned int mask = 1u << off;
193
194	chip = container_of(gc, struct max732x_chip, gpio_chip);
195
196	if ((mask & chip->dir_input) == 0) {
197		dev_dbg(&chip->client->dev, "%s port %d is output only\n",
198			chip->client->name, off);
199		return -EACCES;
200	}
201
202	/*
203	 * Open-drain pins must be set to high impedance (which is
204	 * equivalent to output-high) to be turned into an input.
205	 */
206	if ((mask & chip->dir_output))
207		max732x_gpio_set_value(gc, off, 1);
208
209	return 0;
210}
211
212static int max732x_gpio_direction_output(struct gpio_chip *gc,
213		unsigned off, int val)
214{
215	struct max732x_chip *chip;
216	unsigned int mask = 1u << off;
217
218	chip = container_of(gc, struct max732x_chip, gpio_chip);
219
220	if ((mask & chip->dir_output) == 0) {
221		dev_dbg(&chip->client->dev, "%s port %d is input only\n",
222			chip->client->name, off);
223		return -EACCES;
224	}
225
226	max732x_gpio_set_value(gc, off, val);
227	return 0;
228}
229
230#ifdef CONFIG_GPIO_MAX732X_IRQ
231static int max732x_writew(struct max732x_chip *chip, uint16_t val)
232{
233	int ret;
234
235	val = cpu_to_le16(val);
236
237	ret = i2c_master_send(chip->client_group_a, (char *)&val, 2);
238	if (ret < 0) {
239		dev_err(&chip->client_group_a->dev, "failed writing\n");
240		return ret;
241	}
242
243	return 0;
244}
245
246static int max732x_readw(struct max732x_chip *chip, uint16_t *val)
247{
248	int ret;
249
250	ret = i2c_master_recv(chip->client_group_a, (char *)val, 2);
251	if (ret < 0) {
252		dev_err(&chip->client_group_a->dev, "failed reading\n");
253		return ret;
254	}
255
256	*val = le16_to_cpu(*val);
257	return 0;
258}
259
260static void max732x_irq_update_mask(struct max732x_chip *chip)
261{
262	uint16_t msg;
263
264	if (chip->irq_mask == chip->irq_mask_cur)
265		return;
266
267	chip->irq_mask = chip->irq_mask_cur;
268
269	if (chip->irq_features == INT_NO_MASK)
270		return;
271
272	mutex_lock(&chip->lock);
273
274	switch (chip->irq_features) {
275	case INT_INDEP_MASK:
276		msg = (chip->irq_mask << 8) | chip->reg_out[0];
277		max732x_writew(chip, msg);
278		break;
279
280	case INT_MERGED_MASK:
281		msg = chip->irq_mask | chip->reg_out[0];
282		max732x_writeb(chip, 1, (uint8_t)msg);
283		break;
284	}
285
286	mutex_unlock(&chip->lock);
287}
288
289static int max732x_gpio_to_irq(struct gpio_chip *gc, unsigned off)
290{
291	struct max732x_chip *chip;
292
293	chip = container_of(gc, struct max732x_chip, gpio_chip);
294	return chip->irq_base + off;
295}
296
297static void max732x_irq_mask(unsigned int irq)
298{
299	struct max732x_chip *chip = get_irq_chip_data(irq);
300
301	chip->irq_mask_cur &= ~(1 << (irq - chip->irq_base));
302}
303
304static void max732x_irq_unmask(unsigned int irq)
305{
306	struct max732x_chip *chip = get_irq_chip_data(irq);
307
308	chip->irq_mask_cur |= 1 << (irq - chip->irq_base);
309}
310
311static void max732x_irq_bus_lock(unsigned int irq)
312{
313	struct max732x_chip *chip = get_irq_chip_data(irq);
314
315	mutex_lock(&chip->irq_lock);
316	chip->irq_mask_cur = chip->irq_mask;
317}
318
319static void max732x_irq_bus_sync_unlock(unsigned int irq)
320{
321	struct max732x_chip *chip = get_irq_chip_data(irq);
322
323	max732x_irq_update_mask(chip);
324	mutex_unlock(&chip->irq_lock);
325}
326
327static int max732x_irq_set_type(unsigned int irq, unsigned int type)
328{
329	struct max732x_chip *chip = get_irq_chip_data(irq);
330	uint16_t off = irq - chip->irq_base;
331	uint16_t mask = 1 << off;
332
333	if (!(mask & chip->dir_input)) {
334		dev_dbg(&chip->client->dev, "%s port %d is output only\n",
335			chip->client->name, off);
336		return -EACCES;
337	}
338
339	if (!(type & IRQ_TYPE_EDGE_BOTH)) {
340		dev_err(&chip->client->dev, "irq %d: unsupported type %d\n",
341			irq, type);
342		return -EINVAL;
343	}
344
345	if (type & IRQ_TYPE_EDGE_FALLING)
346		chip->irq_trig_fall |= mask;
347	else
348		chip->irq_trig_fall &= ~mask;
349
350	if (type & IRQ_TYPE_EDGE_RISING)
351		chip->irq_trig_raise |= mask;
352	else
353		chip->irq_trig_raise &= ~mask;
354
355	return max732x_gpio_direction_input(&chip->gpio_chip, off);
356}
357
358static struct irq_chip max732x_irq_chip = {
359	.name			= "max732x",
360	.mask			= max732x_irq_mask,
361	.unmask			= max732x_irq_unmask,
362	.bus_lock		= max732x_irq_bus_lock,
363	.bus_sync_unlock	= max732x_irq_bus_sync_unlock,
364	.set_type		= max732x_irq_set_type,
365};
366
367static uint8_t max732x_irq_pending(struct max732x_chip *chip)
368{
369	uint8_t cur_stat;
370	uint8_t old_stat;
371	uint8_t trigger;
372	uint8_t pending;
373	uint16_t status;
374	int ret;
375
376	ret = max732x_readw(chip, &status);
377	if (ret)
378		return 0;
379
380	trigger = status >> 8;
381	trigger &= chip->irq_mask;
382
383	if (!trigger)
384		return 0;
385
386	cur_stat = status & 0xFF;
387	cur_stat &= chip->irq_mask;
388
389	old_stat = cur_stat ^ trigger;
390
391	pending = (old_stat & chip->irq_trig_fall) |
392		  (cur_stat & chip->irq_trig_raise);
393	pending &= trigger;
394
395	return pending;
396}
397
398static irqreturn_t max732x_irq_handler(int irq, void *devid)
399{
400	struct max732x_chip *chip = devid;
401	uint8_t pending;
402	uint8_t level;
403
404	pending = max732x_irq_pending(chip);
405
406	if (!pending)
407		return IRQ_HANDLED;
408
409	do {
410		level = __ffs(pending);
411		handle_nested_irq(level + chip->irq_base);
412
413		pending &= ~(1 << level);
414	} while (pending);
415
416	return IRQ_HANDLED;
417}
418
419static int max732x_irq_setup(struct max732x_chip *chip,
420			     const struct i2c_device_id *id)
421{
422	struct i2c_client *client = chip->client;
423	struct max732x_platform_data *pdata = client->dev.platform_data;
424	int has_irq = max732x_features[id->driver_data] >> 32;
425	int ret;
426
427	if (pdata->irq_base && has_irq != INT_NONE) {
428		int lvl;
429
430		chip->irq_base = pdata->irq_base;
431		chip->irq_features = has_irq;
432		mutex_init(&chip->irq_lock);
433
434		for (lvl = 0; lvl < chip->gpio_chip.ngpio; lvl++) {
435			int irq = lvl + chip->irq_base;
436
437			if (!(chip->dir_input & (1 << lvl)))
438				continue;
439
440			set_irq_chip_data(irq, chip);
441			set_irq_chip_and_handler(irq, &max732x_irq_chip,
442						 handle_edge_irq);
443			set_irq_nested_thread(irq, 1);
444#ifdef CONFIG_ARM
445			set_irq_flags(irq, IRQF_VALID);
446#else
447			set_irq_noprobe(irq);
448#endif
449		}
450
451		ret = request_threaded_irq(client->irq,
452					   NULL,
453					   max732x_irq_handler,
454					   IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
455					   dev_name(&client->dev), chip);
456		if (ret) {
457			dev_err(&client->dev, "failed to request irq %d\n",
458				client->irq);
459			goto out_failed;
460		}
461
462		chip->gpio_chip.to_irq = max732x_gpio_to_irq;
463	}
464
465	return 0;
466
467out_failed:
468	chip->irq_base = 0;
469	return ret;
470}
471
472static void max732x_irq_teardown(struct max732x_chip *chip)
473{
474	if (chip->irq_base)
475		free_irq(chip->client->irq, chip);
476}
477#else /* CONFIG_GPIO_MAX732X_IRQ */
478static int max732x_irq_setup(struct max732x_chip *chip,
479			     const struct i2c_device_id *id)
480{
481	struct i2c_client *client = chip->client;
482	struct max732x_platform_data *pdata = client->dev.platform_data;
483	int has_irq = max732x_features[id->driver_data] >> 32;
484
485	if (pdata->irq_base && has_irq != INT_NONE)
486		dev_warn(&client->dev, "interrupt support not compiled in\n");
487
488	return 0;
489}
490
491static void max732x_irq_teardown(struct max732x_chip *chip)
492{
493}
494#endif
495
496static int __devinit max732x_setup_gpio(struct max732x_chip *chip,
497					const struct i2c_device_id *id,
498					unsigned gpio_start)
499{
500	struct gpio_chip *gc = &chip->gpio_chip;
501	uint32_t id_data = (uint32_t)max732x_features[id->driver_data];
502	int i, port = 0;
503
504	for (i = 0; i < 16; i++, id_data >>= 2) {
505		unsigned int mask = 1 << port;
506
507		switch (id_data & 0x3) {
508		case PORT_OUTPUT:
509			chip->dir_output |= mask;
510			break;
511		case PORT_INPUT:
512			chip->dir_input |= mask;
513			break;
514		case PORT_OPENDRAIN:
515			chip->dir_output |= mask;
516			chip->dir_input |= mask;
517			break;
518		default:
519			continue;
520		}
521
522		if (i < 8)
523			chip->mask_group_a |= mask;
524		port++;
525	}
526
527	if (chip->dir_input)
528		gc->direction_input = max732x_gpio_direction_input;
529	if (chip->dir_output) {
530		gc->direction_output = max732x_gpio_direction_output;
531		gc->set = max732x_gpio_set_value;
532	}
533	gc->get = max732x_gpio_get_value;
534	gc->can_sleep = 1;
535
536	gc->base = gpio_start;
537	gc->ngpio = port;
538	gc->label = chip->client->name;
539	gc->owner = THIS_MODULE;
540
541	return port;
542}
543
544static int __devinit max732x_probe(struct i2c_client *client,
545				   const struct i2c_device_id *id)
546{
547	struct max732x_platform_data *pdata;
548	struct max732x_chip *chip;
549	struct i2c_client *c;
550	uint16_t addr_a, addr_b;
551	int ret, nr_port;
552
553	pdata = client->dev.platform_data;
554	if (pdata == NULL) {
555		dev_dbg(&client->dev, "no platform data\n");
556		return -EINVAL;
557	}
558
559	chip = kzalloc(sizeof(struct max732x_chip), GFP_KERNEL);
560	if (chip == NULL)
561		return -ENOMEM;
562	chip->client = client;
563
564	nr_port = max732x_setup_gpio(chip, id, pdata->gpio_base);
565
566	addr_a = (client->addr & 0x0f) | 0x60;
567	addr_b = (client->addr & 0x0f) | 0x50;
568
569	switch (client->addr & 0x70) {
570	case 0x60:
571		chip->client_group_a = client;
572		if (nr_port > 8) {
573			c = i2c_new_dummy(client->adapter, addr_b);
574			chip->client_group_b = chip->client_dummy = c;
575		}
576		break;
577	case 0x50:
578		chip->client_group_b = client;
579		if (nr_port > 8) {
580			c = i2c_new_dummy(client->adapter, addr_a);
581			chip->client_group_a = chip->client_dummy = c;
582		}
583		break;
584	default:
585		dev_err(&client->dev, "invalid I2C address specified %02x\n",
586				client->addr);
587		ret = -EINVAL;
588		goto out_failed;
589	}
590
591	mutex_init(&chip->lock);
592
593	max732x_readb(chip, is_group_a(chip, 0), &chip->reg_out[0]);
594	if (nr_port > 8)
595		max732x_readb(chip, is_group_a(chip, 8), &chip->reg_out[1]);
596
597	ret = max732x_irq_setup(chip, id);
598	if (ret)
599		goto out_failed;
600
601	ret = gpiochip_add(&chip->gpio_chip);
602	if (ret)
603		goto out_failed;
604
605	if (pdata->setup) {
606		ret = pdata->setup(client, chip->gpio_chip.base,
607				chip->gpio_chip.ngpio, pdata->context);
608		if (ret < 0)
609			dev_warn(&client->dev, "setup failed, %d\n", ret);
610	}
611
612	i2c_set_clientdata(client, chip);
613	return 0;
614
615out_failed:
616	max732x_irq_teardown(chip);
617	kfree(chip);
618	return ret;
619}
620
621static int __devexit max732x_remove(struct i2c_client *client)
622{
623	struct max732x_platform_data *pdata = client->dev.platform_data;
624	struct max732x_chip *chip = i2c_get_clientdata(client);
625	int ret;
626
627	if (pdata->teardown) {
628		ret = pdata->teardown(client, chip->gpio_chip.base,
629				chip->gpio_chip.ngpio, pdata->context);
630		if (ret < 0) {
631			dev_err(&client->dev, "%s failed, %d\n",
632					"teardown", ret);
633			return ret;
634		}
635	}
636
637	ret = gpiochip_remove(&chip->gpio_chip);
638	if (ret) {
639		dev_err(&client->dev, "%s failed, %d\n",
640				"gpiochip_remove()", ret);
641		return ret;
642	}
643
644	max732x_irq_teardown(chip);
645
646	/* unregister any dummy i2c_client */
647	if (chip->client_dummy)
648		i2c_unregister_device(chip->client_dummy);
649
650	kfree(chip);
651	return 0;
652}
653
654static struct i2c_driver max732x_driver = {
655	.driver = {
656		.name	= "max732x",
657		.owner	= THIS_MODULE,
658	},
659	.probe		= max732x_probe,
660	.remove		= __devexit_p(max732x_remove),
661	.id_table	= max732x_id,
662};
663
664static int __init max732x_init(void)
665{
666	return i2c_add_driver(&max732x_driver);
667}
668/* register after i2c postcore initcall and before
669 * subsys initcalls that may rely on these GPIOs
670 */
671subsys_initcall(max732x_init);
672
673static void __exit max732x_exit(void)
674{
675	i2c_del_driver(&max732x_driver);
676}
677module_exit(max732x_exit);
678
679MODULE_AUTHOR("Eric Miao <eric.miao@marvell.com>");
680MODULE_DESCRIPTION("GPIO expander driver for MAX732X");
681MODULE_LICENSE("GPL");
682