Blame view

drivers/gpio/gpio-max732x.c 16.5 KB
bbcd6d543   Eric Miao   gpio: max732x driver
1
  /*
c103de240   Grant Likely   gpio: reorganize ...
2
   *  MAX732x I2C Port Expander with 8/16 I/O
bbcd6d543   Eric Miao   gpio: max732x driver
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
   *
   *  Copyright (C) 2007 Marvell International Ltd.
   *  Copyright (C) 2008 Jack Ren <jack.ren@marvell.com>
   *  Copyright (C) 2008 Eric Miao <eric.miao@marvell.com>
   *
   *  Derived from drivers/gpio/pca953x.c
   *
   *  This program is free software; you can redistribute it and/or modify
   *  it under the terms of the GNU General Public License as published by
   *  the Free Software Foundation; version 2 of the License.
   */
  
  #include <linux/module.h>
  #include <linux/init.h>
  #include <linux/slab.h>
  #include <linux/string.h>
  #include <linux/gpio.h>
a80a0bbee   Marc Zyngier   gpio: add interru...
20
21
  #include <linux/interrupt.h>
  #include <linux/irq.h>
bbcd6d543   Eric Miao   gpio: max732x driver
22
23
24
25
26
27
28
29
30
31
32
33
34
  #include <linux/i2c.h>
  #include <linux/i2c/max732x.h>
  
  
  /*
   * Each port of MAX732x (including MAX7319) falls into one of the
   * following three types:
   *
   *   - Push Pull Output
   *   - Input
   *   - Open Drain I/O
   *
   * designated by 'O', 'I' and 'P' individually according to MAXIM's
a80a0bbee   Marc Zyngier   gpio: add interru...
35
36
   * datasheets. 'I' and 'P' ports are interrupt capables, some with
   * a dedicated interrupt mask.
bbcd6d543   Eric Miao   gpio: max732x driver
37
38
39
40
41
42
43
44
45
46
47
48
   *
   * There are two groups of I/O ports, each group usually includes
   * up to 8 I/O ports, and is accessed by a specific I2C address:
   *
   *   - Group A : by I2C address 0b'110xxxx
   *   - Group B : by I2C address 0b'101xxxx
   *
   * where 'xxxx' is decided by the connections of pin AD2/AD0.  The
   * address used also affects the initial state of output signals.
   *
   * Within each group of ports, there are five known combinations of
   * I/O ports: 4I4O, 4P4O, 8I, 8P, 8O, see the definitions below for
a80a0bbee   Marc Zyngier   gpio: add interru...
49
50
   * the detailed organization of these ports. Only Goup A is interrupt
   * capable.
bbcd6d543   Eric Miao   gpio: max732x driver
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
   *
   * GPIO numbers start from 'gpio_base + 0' to 'gpio_base + 8/16',
   * and GPIOs from GROUP_A are numbered before those from GROUP_B
   * (if there are two groups).
   *
   * NOTE: MAX7328/MAX7329 are drop-in replacements for PCF8574/a, so
   * they are not supported by this driver.
   */
  
  #define PORT_NONE	0x0	/* '/' No Port */
  #define PORT_OUTPUT	0x1	/* 'O' Push-Pull, Output Only */
  #define PORT_INPUT	0x2	/* 'I' Input Only */
  #define PORT_OPENDRAIN	0x3	/* 'P' Open-Drain, I/O */
  
  #define IO_4I4O		0x5AA5	/* O7 O6 I5 I4 I3 I2 O1 O0 */
  #define IO_4P4O		0x5FF5	/* O7 O6 P5 P4 P3 P2 O1 O0 */
  #define IO_8I		0xAAAA	/* I7 I6 I5 I4 I3 I2 I1 I0 */
  #define IO_8P		0xFFFF	/* P7 P6 P5 P4 P3 P2 P1 P0 */
  #define IO_8O		0x5555	/* O7 O6 O5 O4 O3 O2 O1 O0 */
  
  #define GROUP_A(x)	((x) & 0xffff)	/* I2C Addr: 0b'110xxxx */
  #define GROUP_B(x)	((x) << 16)	/* I2C Addr: 0b'101xxxx */
a80a0bbee   Marc Zyngier   gpio: add interru...
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
  #define INT_NONE	0x0	/* No interrupt capability */
  #define INT_NO_MASK	0x1	/* Has interrupts, no mask */
  #define INT_INDEP_MASK	0x2	/* Has interrupts, independent mask */
  #define INT_MERGED_MASK 0x3	/* Has interrupts, merged mask */
  
  #define INT_CAPS(x)	(((uint64_t)(x)) << 32)
  
  enum {
  	MAX7319,
  	MAX7320,
  	MAX7321,
  	MAX7322,
  	MAX7323,
  	MAX7324,
  	MAX7325,
  	MAX7326,
  	MAX7327,
  };
  
  static uint64_t max732x_features[] = {
  	[MAX7319] = GROUP_A(IO_8I) | INT_CAPS(INT_MERGED_MASK),
  	[MAX7320] = GROUP_B(IO_8O),
  	[MAX7321] = GROUP_A(IO_8P) | INT_CAPS(INT_NO_MASK),
  	[MAX7322] = GROUP_A(IO_4I4O) | INT_CAPS(INT_MERGED_MASK),
  	[MAX7323] = GROUP_A(IO_4P4O) | INT_CAPS(INT_INDEP_MASK),
  	[MAX7324] = GROUP_A(IO_8I) | GROUP_B(IO_8O) | INT_CAPS(INT_MERGED_MASK),
  	[MAX7325] = GROUP_A(IO_8P) | GROUP_B(IO_8O) | INT_CAPS(INT_NO_MASK),
  	[MAX7326] = GROUP_A(IO_4I4O) | GROUP_B(IO_8O) | INT_CAPS(INT_MERGED_MASK),
  	[MAX7327] = GROUP_A(IO_4P4O) | GROUP_B(IO_8O) | INT_CAPS(INT_NO_MASK),
  };
bbcd6d543   Eric Miao   gpio: max732x driver
103
  static const struct i2c_device_id max732x_id[] = {
a80a0bbee   Marc Zyngier   gpio: add interru...
104
105
106
107
108
109
110
111
112
  	{ "max7319", MAX7319 },
  	{ "max7320", MAX7320 },
  	{ "max7321", MAX7321 },
  	{ "max7322", MAX7322 },
  	{ "max7323", MAX7323 },
  	{ "max7324", MAX7324 },
  	{ "max7325", MAX7325 },
  	{ "max7326", MAX7326 },
  	{ "max7327", MAX7327 },
bbcd6d543   Eric Miao   gpio: max732x driver
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
  	{ },
  };
  MODULE_DEVICE_TABLE(i2c, max732x_id);
  
  struct max732x_chip {
  	struct gpio_chip gpio_chip;
  
  	struct i2c_client *client;	/* "main" client */
  	struct i2c_client *client_dummy;
  	struct i2c_client *client_group_a;
  	struct i2c_client *client_group_b;
  
  	unsigned int	mask_group_a;
  	unsigned int	dir_input;
  	unsigned int	dir_output;
  
  	struct mutex	lock;
  	uint8_t		reg_out[2];
a80a0bbee   Marc Zyngier   gpio: add interru...
131
132
133
134
135
136
137
138
139
140
  
  #ifdef CONFIG_GPIO_MAX732X_IRQ
  	struct mutex	irq_lock;
  	int		irq_base;
  	uint8_t		irq_mask;
  	uint8_t		irq_mask_cur;
  	uint8_t		irq_trig_raise;
  	uint8_t		irq_trig_fall;
  	uint8_t		irq_features;
  #endif
bbcd6d543   Eric Miao   gpio: max732x driver
141
  };
a80a0bbee   Marc Zyngier   gpio: add interru...
142
  static int max732x_writeb(struct max732x_chip *chip, int group_a, uint8_t val)
bbcd6d543   Eric Miao   gpio: max732x driver
143
144
145
146
147
148
149
150
151
152
153
154
155
156
  {
  	struct i2c_client *client;
  	int ret;
  
  	client = group_a ? chip->client_group_a : chip->client_group_b;
  	ret = i2c_smbus_write_byte(client, val);
  	if (ret < 0) {
  		dev_err(&client->dev, "failed writing
  ");
  		return ret;
  	}
  
  	return 0;
  }
a80a0bbee   Marc Zyngier   gpio: add interru...
157
  static int max732x_readb(struct max732x_chip *chip, int group_a, uint8_t *val)
bbcd6d543   Eric Miao   gpio: max732x driver
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
  {
  	struct i2c_client *client;
  	int ret;
  
  	client = group_a ? chip->client_group_a : chip->client_group_b;
  	ret = i2c_smbus_read_byte(client);
  	if (ret < 0) {
  		dev_err(&client->dev, "failed reading
  ");
  		return ret;
  	}
  
  	*val = (uint8_t)ret;
  	return 0;
  }
  
  static inline int is_group_a(struct max732x_chip *chip, unsigned off)
  {
  	return (1u << off) & chip->mask_group_a;
  }
  
  static int max732x_gpio_get_value(struct gpio_chip *gc, unsigned off)
  {
  	struct max732x_chip *chip;
  	uint8_t reg_val;
  	int ret;
  
  	chip = container_of(gc, struct max732x_chip, gpio_chip);
a80a0bbee   Marc Zyngier   gpio: add interru...
186
  	ret = max732x_readb(chip, is_group_a(chip, off), &reg_val);
bbcd6d543   Eric Miao   gpio: max732x driver
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
  	if (ret < 0)
  		return 0;
  
  	return reg_val & (1u << (off & 0x7));
  }
  
  static void max732x_gpio_set_value(struct gpio_chip *gc, unsigned off, int val)
  {
  	struct max732x_chip *chip;
  	uint8_t reg_out, mask = 1u << (off & 0x7);
  	int ret;
  
  	chip = container_of(gc, struct max732x_chip, gpio_chip);
  
  	mutex_lock(&chip->lock);
  
  	reg_out = (off > 7) ? chip->reg_out[1] : chip->reg_out[0];
  	reg_out = (val) ? reg_out | mask : reg_out & ~mask;
a80a0bbee   Marc Zyngier   gpio: add interru...
205
  	ret = max732x_writeb(chip, is_group_a(chip, off), reg_out);
bbcd6d543   Eric Miao   gpio: max732x driver
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
  	if (ret < 0)
  		goto out;
  
  	/* update the shadow register then */
  	if (off > 7)
  		chip->reg_out[1] = reg_out;
  	else
  		chip->reg_out[0] = reg_out;
  out:
  	mutex_unlock(&chip->lock);
  }
  
  static int max732x_gpio_direction_input(struct gpio_chip *gc, unsigned off)
  {
  	struct max732x_chip *chip;
  	unsigned int mask = 1u << off;
  
  	chip = container_of(gc, struct max732x_chip, gpio_chip);
  
  	if ((mask & chip->dir_input) == 0) {
  		dev_dbg(&chip->client->dev, "%s port %d is output only
  ",
  			chip->client->name, off);
  		return -EACCES;
  	}
a13c1868a   Marc Zyngier   gpio: max732x: fi...
231
232
233
234
235
236
  	/*
  	 * Open-drain pins must be set to high impedance (which is
  	 * equivalent to output-high) to be turned into an input.
  	 */
  	if ((mask & chip->dir_output))
  		max732x_gpio_set_value(gc, off, 1);
bbcd6d543   Eric Miao   gpio: max732x driver
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
  	return 0;
  }
  
  static int max732x_gpio_direction_output(struct gpio_chip *gc,
  		unsigned off, int val)
  {
  	struct max732x_chip *chip;
  	unsigned int mask = 1u << off;
  
  	chip = container_of(gc, struct max732x_chip, gpio_chip);
  
  	if ((mask & chip->dir_output) == 0) {
  		dev_dbg(&chip->client->dev, "%s port %d is input only
  ",
  			chip->client->name, off);
  		return -EACCES;
  	}
  
  	max732x_gpio_set_value(gc, off, val);
  	return 0;
  }
a80a0bbee   Marc Zyngier   gpio: add interru...
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
  #ifdef CONFIG_GPIO_MAX732X_IRQ
  static int max732x_writew(struct max732x_chip *chip, uint16_t val)
  {
  	int ret;
  
  	val = cpu_to_le16(val);
  
  	ret = i2c_master_send(chip->client_group_a, (char *)&val, 2);
  	if (ret < 0) {
  		dev_err(&chip->client_group_a->dev, "failed writing
  ");
  		return ret;
  	}
  
  	return 0;
  }
  
  static int max732x_readw(struct max732x_chip *chip, uint16_t *val)
  {
  	int ret;
  
  	ret = i2c_master_recv(chip->client_group_a, (char *)val, 2);
  	if (ret < 0) {
  		dev_err(&chip->client_group_a->dev, "failed reading
  ");
  		return ret;
  	}
  
  	*val = le16_to_cpu(*val);
  	return 0;
  }
  
  static void max732x_irq_update_mask(struct max732x_chip *chip)
  {
  	uint16_t msg;
  
  	if (chip->irq_mask == chip->irq_mask_cur)
  		return;
  
  	chip->irq_mask = chip->irq_mask_cur;
  
  	if (chip->irq_features == INT_NO_MASK)
  		return;
  
  	mutex_lock(&chip->lock);
  
  	switch (chip->irq_features) {
  	case INT_INDEP_MASK:
  		msg = (chip->irq_mask << 8) | chip->reg_out[0];
  		max732x_writew(chip, msg);
  		break;
  
  	case INT_MERGED_MASK:
  		msg = chip->irq_mask | chip->reg_out[0];
  		max732x_writeb(chip, 1, (uint8_t)msg);
  		break;
  	}
  
  	mutex_unlock(&chip->lock);
  }
  
  static int max732x_gpio_to_irq(struct gpio_chip *gc, unsigned off)
  {
  	struct max732x_chip *chip;
  
  	chip = container_of(gc, struct max732x_chip, gpio_chip);
  	return chip->irq_base + off;
  }
fbc4667ab   Lennert Buytenhek   gpio: max732x: ir...
326
  static void max732x_irq_mask(struct irq_data *d)
a80a0bbee   Marc Zyngier   gpio: add interru...
327
  {
fbc4667ab   Lennert Buytenhek   gpio: max732x: ir...
328
  	struct max732x_chip *chip = irq_data_get_irq_chip_data(d);
a80a0bbee   Marc Zyngier   gpio: add interru...
329

fbc4667ab   Lennert Buytenhek   gpio: max732x: ir...
330
  	chip->irq_mask_cur &= ~(1 << (d->irq - chip->irq_base));
a80a0bbee   Marc Zyngier   gpio: add interru...
331
  }
fbc4667ab   Lennert Buytenhek   gpio: max732x: ir...
332
  static void max732x_irq_unmask(struct irq_data *d)
a80a0bbee   Marc Zyngier   gpio: add interru...
333
  {
fbc4667ab   Lennert Buytenhek   gpio: max732x: ir...
334
  	struct max732x_chip *chip = irq_data_get_irq_chip_data(d);
a80a0bbee   Marc Zyngier   gpio: add interru...
335

fbc4667ab   Lennert Buytenhek   gpio: max732x: ir...
336
  	chip->irq_mask_cur |= 1 << (d->irq - chip->irq_base);
a80a0bbee   Marc Zyngier   gpio: add interru...
337
  }
fbc4667ab   Lennert Buytenhek   gpio: max732x: ir...
338
  static void max732x_irq_bus_lock(struct irq_data *d)
a80a0bbee   Marc Zyngier   gpio: add interru...
339
  {
fbc4667ab   Lennert Buytenhek   gpio: max732x: ir...
340
  	struct max732x_chip *chip = irq_data_get_irq_chip_data(d);
a80a0bbee   Marc Zyngier   gpio: add interru...
341
342
343
344
  
  	mutex_lock(&chip->irq_lock);
  	chip->irq_mask_cur = chip->irq_mask;
  }
fbc4667ab   Lennert Buytenhek   gpio: max732x: ir...
345
  static void max732x_irq_bus_sync_unlock(struct irq_data *d)
a80a0bbee   Marc Zyngier   gpio: add interru...
346
  {
fbc4667ab   Lennert Buytenhek   gpio: max732x: ir...
347
  	struct max732x_chip *chip = irq_data_get_irq_chip_data(d);
a80a0bbee   Marc Zyngier   gpio: add interru...
348
349
350
351
  
  	max732x_irq_update_mask(chip);
  	mutex_unlock(&chip->irq_lock);
  }
fbc4667ab   Lennert Buytenhek   gpio: max732x: ir...
352
  static int max732x_irq_set_type(struct irq_data *d, unsigned int type)
a80a0bbee   Marc Zyngier   gpio: add interru...
353
  {
fbc4667ab   Lennert Buytenhek   gpio: max732x: ir...
354
355
  	struct max732x_chip *chip = irq_data_get_irq_chip_data(d);
  	uint16_t off = d->irq - chip->irq_base;
a80a0bbee   Marc Zyngier   gpio: add interru...
356
357
358
359
360
361
362
363
364
365
366
367
  	uint16_t mask = 1 << off;
  
  	if (!(mask & chip->dir_input)) {
  		dev_dbg(&chip->client->dev, "%s port %d is output only
  ",
  			chip->client->name, off);
  		return -EACCES;
  	}
  
  	if (!(type & IRQ_TYPE_EDGE_BOTH)) {
  		dev_err(&chip->client->dev, "irq %d: unsupported type %d
  ",
fbc4667ab   Lennert Buytenhek   gpio: max732x: ir...
368
  			d->irq, type);
a80a0bbee   Marc Zyngier   gpio: add interru...
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
  		return -EINVAL;
  	}
  
  	if (type & IRQ_TYPE_EDGE_FALLING)
  		chip->irq_trig_fall |= mask;
  	else
  		chip->irq_trig_fall &= ~mask;
  
  	if (type & IRQ_TYPE_EDGE_RISING)
  		chip->irq_trig_raise |= mask;
  	else
  		chip->irq_trig_raise &= ~mask;
  
  	return max732x_gpio_direction_input(&chip->gpio_chip, off);
  }
  
  static struct irq_chip max732x_irq_chip = {
  	.name			= "max732x",
fbc4667ab   Lennert Buytenhek   gpio: max732x: ir...
387
388
389
390
391
  	.irq_mask		= max732x_irq_mask,
  	.irq_unmask		= max732x_irq_unmask,
  	.irq_bus_lock		= max732x_irq_bus_lock,
  	.irq_bus_sync_unlock	= max732x_irq_bus_sync_unlock,
  	.irq_set_type		= max732x_irq_set_type,
a80a0bbee   Marc Zyngier   gpio: add interru...
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
  };
  
  static uint8_t max732x_irq_pending(struct max732x_chip *chip)
  {
  	uint8_t cur_stat;
  	uint8_t old_stat;
  	uint8_t trigger;
  	uint8_t pending;
  	uint16_t status;
  	int ret;
  
  	ret = max732x_readw(chip, &status);
  	if (ret)
  		return 0;
  
  	trigger = status >> 8;
  	trigger &= chip->irq_mask;
  
  	if (!trigger)
  		return 0;
  
  	cur_stat = status & 0xFF;
  	cur_stat &= chip->irq_mask;
  
  	old_stat = cur_stat ^ trigger;
  
  	pending = (old_stat & chip->irq_trig_fall) |
  		  (cur_stat & chip->irq_trig_raise);
  	pending &= trigger;
  
  	return pending;
  }
  
  static irqreturn_t max732x_irq_handler(int irq, void *devid)
  {
  	struct max732x_chip *chip = devid;
  	uint8_t pending;
  	uint8_t level;
  
  	pending = max732x_irq_pending(chip);
  
  	if (!pending)
  		return IRQ_HANDLED;
  
  	do {
  		level = __ffs(pending);
  		handle_nested_irq(level + chip->irq_base);
  
  		pending &= ~(1 << level);
  	} while (pending);
  
  	return IRQ_HANDLED;
  }
  
  static int max732x_irq_setup(struct max732x_chip *chip,
  			     const struct i2c_device_id *id)
  {
  	struct i2c_client *client = chip->client;
  	struct max732x_platform_data *pdata = client->dev.platform_data;
  	int has_irq = max732x_features[id->driver_data] >> 32;
  	int ret;
  
  	if (pdata->irq_base && has_irq != INT_NONE) {
  		int lvl;
  
  		chip->irq_base = pdata->irq_base;
  		chip->irq_features = has_irq;
  		mutex_init(&chip->irq_lock);
  
  		for (lvl = 0; lvl < chip->gpio_chip.ngpio; lvl++) {
  			int irq = lvl + chip->irq_base;
  
  			if (!(chip->dir_input & (1 << lvl)))
  				continue;
b51804bcf   Thomas Gleixner   gpio: Cleanup gen...
466
467
  			irq_set_chip_data(irq, chip);
  			irq_set_chip_and_handler(irq, &max732x_irq_chip,
a80a0bbee   Marc Zyngier   gpio: add interru...
468
  						 handle_edge_irq);
b51804bcf   Thomas Gleixner   gpio: Cleanup gen...
469
  			irq_set_nested_thread(irq, 1);
a80a0bbee   Marc Zyngier   gpio: add interru...
470
471
472
  #ifdef CONFIG_ARM
  			set_irq_flags(irq, IRQF_VALID);
  #else
b51804bcf   Thomas Gleixner   gpio: Cleanup gen...
473
  			irq_set_noprobe(irq);
a80a0bbee   Marc Zyngier   gpio: add interru...
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
  #endif
  		}
  
  		ret = request_threaded_irq(client->irq,
  					   NULL,
  					   max732x_irq_handler,
  					   IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  					   dev_name(&client->dev), chip);
  		if (ret) {
  			dev_err(&client->dev, "failed to request irq %d
  ",
  				client->irq);
  			goto out_failed;
  		}
  
  		chip->gpio_chip.to_irq = max732x_gpio_to_irq;
  	}
  
  	return 0;
  
  out_failed:
  	chip->irq_base = 0;
  	return ret;
  }
  
  static void max732x_irq_teardown(struct max732x_chip *chip)
  {
  	if (chip->irq_base)
  		free_irq(chip->client->irq, chip);
  }
  #else /* CONFIG_GPIO_MAX732X_IRQ */
  static int max732x_irq_setup(struct max732x_chip *chip,
  			     const struct i2c_device_id *id)
  {
  	struct i2c_client *client = chip->client;
  	struct max732x_platform_data *pdata = client->dev.platform_data;
  	int has_irq = max732x_features[id->driver_data] >> 32;
  
  	if (pdata->irq_base && has_irq != INT_NONE)
  		dev_warn(&client->dev, "interrupt support not compiled in
  ");
  
  	return 0;
  }
  
  static void max732x_irq_teardown(struct max732x_chip *chip)
  {
  }
  #endif
bbcd6d543   Eric Miao   gpio: max732x driver
523
524
525
526
527
  static int __devinit max732x_setup_gpio(struct max732x_chip *chip,
  					const struct i2c_device_id *id,
  					unsigned gpio_start)
  {
  	struct gpio_chip *gc = &chip->gpio_chip;
a80a0bbee   Marc Zyngier   gpio: add interru...
528
  	uint32_t id_data = (uint32_t)max732x_features[id->driver_data];
bbcd6d543   Eric Miao   gpio: max732x driver
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
  	int i, port = 0;
  
  	for (i = 0; i < 16; i++, id_data >>= 2) {
  		unsigned int mask = 1 << port;
  
  		switch (id_data & 0x3) {
  		case PORT_OUTPUT:
  			chip->dir_output |= mask;
  			break;
  		case PORT_INPUT:
  			chip->dir_input |= mask;
  			break;
  		case PORT_OPENDRAIN:
  			chip->dir_output |= mask;
  			chip->dir_input |= mask;
  			break;
  		default:
  			continue;
  		}
  
  		if (i < 8)
  			chip->mask_group_a |= mask;
  		port++;
  	}
  
  	if (chip->dir_input)
  		gc->direction_input = max732x_gpio_direction_input;
  	if (chip->dir_output) {
  		gc->direction_output = max732x_gpio_direction_output;
  		gc->set = max732x_gpio_set_value;
  	}
  	gc->get = max732x_gpio_get_value;
  	gc->can_sleep = 1;
  
  	gc->base = gpio_start;
  	gc->ngpio = port;
  	gc->label = chip->client->name;
  	gc->owner = THIS_MODULE;
  
  	return port;
  }
  
  static int __devinit max732x_probe(struct i2c_client *client,
  				   const struct i2c_device_id *id)
  {
  	struct max732x_platform_data *pdata;
  	struct max732x_chip *chip;
  	struct i2c_client *c;
  	uint16_t addr_a, addr_b;
  	int ret, nr_port;
  
  	pdata = client->dev.platform_data;
a342d215c   Ben Dooks   gpio: fix probe()...
581
582
583
584
585
  	if (pdata == NULL) {
  		dev_dbg(&client->dev, "no platform data
  ");
  		return -EINVAL;
  	}
bbcd6d543   Eric Miao   gpio: max732x driver
586
587
588
589
590
591
592
593
594
595
596
597
598
599
  
  	chip = kzalloc(sizeof(struct max732x_chip), GFP_KERNEL);
  	if (chip == NULL)
  		return -ENOMEM;
  	chip->client = client;
  
  	nr_port = max732x_setup_gpio(chip, id, pdata->gpio_base);
  
  	addr_a = (client->addr & 0x0f) | 0x60;
  	addr_b = (client->addr & 0x0f) | 0x50;
  
  	switch (client->addr & 0x70) {
  	case 0x60:
  		chip->client_group_a = client;
5535cb681   Axel Lin   max732x: correct ...
600
  		if (nr_port > 8) {
bbcd6d543   Eric Miao   gpio: max732x driver
601
602
603
604
605
606
  			c = i2c_new_dummy(client->adapter, addr_b);
  			chip->client_group_b = chip->client_dummy = c;
  		}
  		break;
  	case 0x50:
  		chip->client_group_b = client;
5535cb681   Axel Lin   max732x: correct ...
607
  		if (nr_port > 8) {
bbcd6d543   Eric Miao   gpio: max732x driver
608
609
610
611
612
613
614
615
616
617
618
619
620
  			c = i2c_new_dummy(client->adapter, addr_a);
  			chip->client_group_a = chip->client_dummy = c;
  		}
  		break;
  	default:
  		dev_err(&client->dev, "invalid I2C address specified %02x
  ",
  				client->addr);
  		ret = -EINVAL;
  		goto out_failed;
  	}
  
  	mutex_init(&chip->lock);
a80a0bbee   Marc Zyngier   gpio: add interru...
621
  	max732x_readb(chip, is_group_a(chip, 0), &chip->reg_out[0]);
5535cb681   Axel Lin   max732x: correct ...
622
  	if (nr_port > 8)
a80a0bbee   Marc Zyngier   gpio: add interru...
623
624
625
626
627
  		max732x_readb(chip, is_group_a(chip, 8), &chip->reg_out[1]);
  
  	ret = max732x_irq_setup(chip, id);
  	if (ret)
  		goto out_failed;
bbcd6d543   Eric Miao   gpio: max732x driver
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
  
  	ret = gpiochip_add(&chip->gpio_chip);
  	if (ret)
  		goto out_failed;
  
  	if (pdata->setup) {
  		ret = pdata->setup(client, chip->gpio_chip.base,
  				chip->gpio_chip.ngpio, pdata->context);
  		if (ret < 0)
  			dev_warn(&client->dev, "setup failed, %d
  ", ret);
  	}
  
  	i2c_set_clientdata(client, chip);
  	return 0;
  
  out_failed:
a80a0bbee   Marc Zyngier   gpio: add interru...
645
  	max732x_irq_teardown(chip);
bbcd6d543   Eric Miao   gpio: max732x driver
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
  	kfree(chip);
  	return ret;
  }
  
  static int __devexit max732x_remove(struct i2c_client *client)
  {
  	struct max732x_platform_data *pdata = client->dev.platform_data;
  	struct max732x_chip *chip = i2c_get_clientdata(client);
  	int ret;
  
  	if (pdata->teardown) {
  		ret = pdata->teardown(client, chip->gpio_chip.base,
  				chip->gpio_chip.ngpio, pdata->context);
  		if (ret < 0) {
  			dev_err(&client->dev, "%s failed, %d
  ",
  					"teardown", ret);
  			return ret;
  		}
  	}
  
  	ret = gpiochip_remove(&chip->gpio_chip);
  	if (ret) {
  		dev_err(&client->dev, "%s failed, %d
  ",
  				"gpiochip_remove()", ret);
  		return ret;
  	}
a80a0bbee   Marc Zyngier   gpio: add interru...
674
  	max732x_irq_teardown(chip);
bbcd6d543   Eric Miao   gpio: max732x driver
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
  	/* unregister any dummy i2c_client */
  	if (chip->client_dummy)
  		i2c_unregister_device(chip->client_dummy);
  
  	kfree(chip);
  	return 0;
  }
  
  static struct i2c_driver max732x_driver = {
  	.driver = {
  		.name	= "max732x",
  		.owner	= THIS_MODULE,
  	},
  	.probe		= max732x_probe,
  	.remove		= __devexit_p(max732x_remove),
  	.id_table	= max732x_id,
  };
  
  static int __init max732x_init(void)
  {
  	return i2c_add_driver(&max732x_driver);
  }
2f8d11971   David Brownell   gpio: i2c expande...
697
698
699
700
  /* register after i2c postcore initcall and before
   * subsys initcalls that may rely on these GPIOs
   */
  subsys_initcall(max732x_init);
bbcd6d543   Eric Miao   gpio: max732x driver
701
702
703
704
705
706
707
708
709
710
  
  static void __exit max732x_exit(void)
  {
  	i2c_del_driver(&max732x_driver);
  }
  module_exit(max732x_exit);
  
  MODULE_AUTHOR("Eric Miao <eric.miao@marvell.com>");
  MODULE_DESCRIPTION("GPIO expander driver for MAX732X");
  MODULE_LICENSE("GPL");