Commit | Line | Data |
---|---|---|
6048a3dd CM |
1 | /* |
2 | * htc-i2cpld.c | |
3 | * Chip driver for an unknown CPLD chip found on omap850 HTC devices like | |
4 | * the HTC Wizard and HTC Herald. | |
5 | * The cpld is located on the i2c bus and acts as an input/output GPIO | |
6 | * extender. | |
7 | * | |
8 | * Copyright (C) 2009 Cory Maccarrone <darkstar6262@gmail.com> | |
9 | * | |
10 | * Based on work done in the linwizard project | |
11 | * Copyright (C) 2008-2009 Angelo Arrifano <miknix@gmail.com> | |
12 | * | |
13 | * This program is free software; you can redistribute it and/or modify | |
14 | * it under the terms of the GNU General Public License as published by | |
15 | * the Free Software Foundation; either version 2 of the License, or | |
16 | * (at your option) any later version. | |
17 | * | |
18 | * This program is distributed in the hope that it will be useful, | |
19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
21 | * GNU General Public License for more details. | |
22 | * | |
23 | * You should have received a copy of the GNU General Public License | |
24 | * along with this program; if not, write to the Free Software | |
25 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
26 | */ | |
27 | ||
28 | #include <linux/kernel.h> | |
29 | #include <linux/init.h> | |
30 | #include <linux/module.h> | |
31 | #include <linux/interrupt.h> | |
32 | #include <linux/platform_device.h> | |
33 | #include <linux/i2c.h> | |
34 | #include <linux/irq.h> | |
35 | #include <linux/spinlock.h> | |
36 | #include <linux/htcpld.h> | |
37 | #include <linux/gpio.h> | |
5a0e3ad6 | 38 | #include <linux/slab.h> |
6048a3dd CM |
39 | |
40 | struct htcpld_chip { | |
41 | spinlock_t lock; | |
42 | ||
43 | /* chip info */ | |
44 | u8 reset; | |
45 | u8 addr; | |
46 | struct device *dev; | |
47 | struct i2c_client *client; | |
48 | ||
49 | /* Output details */ | |
50 | u8 cache_out; | |
51 | struct gpio_chip chip_out; | |
52 | ||
53 | /* Input details */ | |
54 | u8 cache_in; | |
55 | struct gpio_chip chip_in; | |
56 | ||
57 | u16 irqs_enabled; | |
58 | uint irq_start; | |
59 | int nirqs; | |
60 | ||
61 | /* | |
62 | * Work structure to allow for setting values outside of any | |
63 | * possible interrupt context | |
64 | */ | |
65 | struct work_struct set_val_work; | |
66 | }; | |
67 | ||
68 | struct htcpld_data { | |
69 | /* irq info */ | |
70 | u16 irqs_enabled; | |
71 | uint irq_start; | |
72 | int nirqs; | |
73 | uint chained_irq; | |
74 | unsigned int int_reset_gpio_hi; | |
75 | unsigned int int_reset_gpio_lo; | |
76 | ||
77 | /* htcpld info */ | |
78 | struct htcpld_chip *chip; | |
79 | unsigned int nchips; | |
80 | }; | |
81 | ||
82 | /* There does not appear to be a way to proactively mask interrupts | |
83 | * on the htcpld chip itself. So, we simply ignore interrupts that | |
84 | * aren't desired. */ | |
e6a4c4a4 | 85 | static void htcpld_mask(struct irq_data *data) |
6048a3dd | 86 | { |
e6a4c4a4 MB |
87 | struct htcpld_chip *chip = irq_data_get_irq_chip_data(data); |
88 | chip->irqs_enabled &= ~(1 << (data->irq - chip->irq_start)); | |
89 | pr_debug("HTCPLD mask %d %04x\n", data->irq, chip->irqs_enabled); | |
6048a3dd | 90 | } |
e6a4c4a4 | 91 | static void htcpld_unmask(struct irq_data *data) |
6048a3dd | 92 | { |
e6a4c4a4 MB |
93 | struct htcpld_chip *chip = irq_data_get_irq_chip_data(data); |
94 | chip->irqs_enabled |= 1 << (data->irq - chip->irq_start); | |
95 | pr_debug("HTCPLD unmask %d %04x\n", data->irq, chip->irqs_enabled); | |
6048a3dd CM |
96 | } |
97 | ||
e6a4c4a4 | 98 | static int htcpld_set_type(struct irq_data *data, unsigned int flags) |
6048a3dd | 99 | { |
e6a4c4a4 | 100 | struct irq_desc *d = irq_to_desc(data->irq); |
6048a3dd CM |
101 | |
102 | if (!d) { | |
e6a4c4a4 | 103 | pr_err("HTCPLD invalid IRQ: %d\n", data->irq); |
6048a3dd CM |
104 | return -EINVAL; |
105 | } | |
106 | ||
107 | if (flags & ~IRQ_TYPE_SENSE_MASK) | |
108 | return -EINVAL; | |
109 | ||
110 | /* We only allow edge triggering */ | |
111 | if (flags & (IRQ_TYPE_LEVEL_LOW|IRQ_TYPE_LEVEL_HIGH)) | |
112 | return -EINVAL; | |
113 | ||
114 | d->status &= ~IRQ_TYPE_SENSE_MASK; | |
115 | d->status |= flags; | |
116 | ||
117 | return 0; | |
118 | } | |
119 | ||
120 | static struct irq_chip htcpld_muxed_chip = { | |
e6a4c4a4 MB |
121 | .name = "htcpld", |
122 | .irq_mask = htcpld_mask, | |
123 | .irq_unmask = htcpld_unmask, | |
124 | .irq_set_type = htcpld_set_type, | |
6048a3dd CM |
125 | }; |
126 | ||
127 | /* To properly dispatch IRQ events, we need to read from the | |
128 | * chip. This is an I2C action that could possibly sleep | |
129 | * (which is bad in interrupt context) -- so we use a threaded | |
130 | * interrupt handler to get around that. | |
131 | */ | |
132 | static irqreturn_t htcpld_handler(int irq, void *dev) | |
133 | { | |
134 | struct htcpld_data *htcpld = dev; | |
135 | unsigned int i; | |
136 | unsigned long flags; | |
137 | int irqpin; | |
138 | struct irq_desc *desc; | |
139 | ||
140 | if (!htcpld) { | |
141 | pr_debug("htcpld is null in ISR\n"); | |
142 | return IRQ_HANDLED; | |
143 | } | |
144 | ||
145 | /* | |
146 | * For each chip, do a read of the chip and trigger any interrupts | |
147 | * desired. The interrupts will be triggered from LSB to MSB (i.e. | |
148 | * bit 0 first, then bit 1, etc.) | |
149 | * | |
150 | * For chips that have no interrupt range specified, just skip 'em. | |
151 | */ | |
152 | for (i = 0; i < htcpld->nchips; i++) { | |
153 | struct htcpld_chip *chip = &htcpld->chip[i]; | |
154 | struct i2c_client *client; | |
155 | int val; | |
156 | unsigned long uval, old_val; | |
157 | ||
158 | if (!chip) { | |
159 | pr_debug("chip %d is null in ISR\n", i); | |
160 | continue; | |
161 | } | |
162 | ||
163 | if (chip->nirqs == 0) | |
164 | continue; | |
165 | ||
166 | client = chip->client; | |
167 | if (!client) { | |
168 | pr_debug("client %d is null in ISR\n", i); | |
169 | continue; | |
170 | } | |
171 | ||
172 | /* Scan the chip */ | |
173 | val = i2c_smbus_read_byte_data(client, chip->cache_out); | |
174 | if (val < 0) { | |
175 | /* Throw a warning and skip this chip */ | |
176 | dev_warn(chip->dev, "Unable to read from chip: %d\n", | |
177 | val); | |
178 | continue; | |
179 | } | |
180 | ||
181 | uval = (unsigned long)val; | |
182 | ||
183 | spin_lock_irqsave(&chip->lock, flags); | |
184 | ||
185 | /* Save away the old value so we can compare it */ | |
186 | old_val = chip->cache_in; | |
187 | ||
188 | /* Write the new value */ | |
189 | chip->cache_in = uval; | |
190 | ||
191 | spin_unlock_irqrestore(&chip->lock, flags); | |
192 | ||
193 | /* | |
194 | * For each bit in the data (starting at bit 0), trigger | |
195 | * associated interrupts. | |
196 | */ | |
197 | for (irqpin = 0; irqpin < chip->nirqs; irqpin++) { | |
198 | unsigned oldb, newb; | |
199 | int flags; | |
200 | ||
201 | irq = chip->irq_start + irqpin; | |
202 | desc = irq_to_desc(irq); | |
203 | flags = desc->status; | |
204 | ||
205 | /* Run the IRQ handler, but only if the bit value | |
206 | * changed, and the proper flags are set */ | |
207 | oldb = (old_val >> irqpin) & 1; | |
208 | newb = (uval >> irqpin) & 1; | |
209 | ||
210 | if ((!oldb && newb && (flags & IRQ_TYPE_EDGE_RISING)) || | |
211 | (oldb && !newb && | |
212 | (flags & IRQ_TYPE_EDGE_FALLING))) { | |
213 | pr_debug("fire IRQ %d\n", irqpin); | |
214 | desc->handle_irq(irq, desc); | |
215 | } | |
216 | } | |
217 | } | |
218 | ||
219 | /* | |
220 | * In order to continue receiving interrupts, the int_reset_gpio must | |
221 | * be asserted. | |
222 | */ | |
223 | if (htcpld->int_reset_gpio_hi) | |
224 | gpio_set_value(htcpld->int_reset_gpio_hi, 1); | |
225 | if (htcpld->int_reset_gpio_lo) | |
226 | gpio_set_value(htcpld->int_reset_gpio_lo, 0); | |
227 | ||
228 | return IRQ_HANDLED; | |
229 | } | |
230 | ||
231 | /* | |
232 | * The GPIO set routines can be called from interrupt context, especially if, | |
233 | * for example they're attached to the led-gpio framework and a trigger is | |
234 | * enabled. As such, we declared work above in the htcpld_chip structure, | |
235 | * and that work is scheduled in the set routine. The kernel can then run | |
236 | * the I2C functions, which will sleep, in process context. | |
237 | */ | |
8d2d3a3a | 238 | static void htcpld_chip_set(struct gpio_chip *chip, unsigned offset, int val) |
6048a3dd CM |
239 | { |
240 | struct i2c_client *client; | |
241 | struct htcpld_chip *chip_data; | |
242 | unsigned long flags; | |
243 | ||
244 | chip_data = container_of(chip, struct htcpld_chip, chip_out); | |
245 | if (!chip_data) | |
246 | return; | |
247 | ||
248 | client = chip_data->client; | |
249 | if (client == NULL) | |
250 | return; | |
251 | ||
252 | spin_lock_irqsave(&chip_data->lock, flags); | |
253 | if (val) | |
254 | chip_data->cache_out |= (1 << offset); | |
255 | else | |
256 | chip_data->cache_out &= ~(1 << offset); | |
257 | spin_unlock_irqrestore(&chip_data->lock, flags); | |
258 | ||
259 | schedule_work(&(chip_data->set_val_work)); | |
260 | } | |
261 | ||
8d2d3a3a | 262 | static void htcpld_chip_set_ni(struct work_struct *work) |
6048a3dd CM |
263 | { |
264 | struct htcpld_chip *chip_data; | |
265 | struct i2c_client *client; | |
266 | ||
267 | chip_data = container_of(work, struct htcpld_chip, set_val_work); | |
268 | client = chip_data->client; | |
269 | i2c_smbus_read_byte_data(client, chip_data->cache_out); | |
270 | } | |
271 | ||
8d2d3a3a | 272 | static int htcpld_chip_get(struct gpio_chip *chip, unsigned offset) |
6048a3dd CM |
273 | { |
274 | struct htcpld_chip *chip_data; | |
275 | int val = 0; | |
276 | int is_input = 0; | |
277 | ||
278 | /* Try out first */ | |
279 | chip_data = container_of(chip, struct htcpld_chip, chip_out); | |
280 | if (!chip_data) { | |
281 | /* Try in */ | |
282 | is_input = 1; | |
283 | chip_data = container_of(chip, struct htcpld_chip, chip_in); | |
284 | if (!chip_data) | |
285 | return -EINVAL; | |
286 | } | |
287 | ||
288 | /* Determine if this is an input or output GPIO */ | |
289 | if (!is_input) | |
290 | /* Use the output cache */ | |
291 | val = (chip_data->cache_out >> offset) & 1; | |
292 | else | |
293 | /* Use the input cache */ | |
294 | val = (chip_data->cache_in >> offset) & 1; | |
295 | ||
296 | if (val) | |
297 | return 1; | |
298 | else | |
299 | return 0; | |
300 | } | |
301 | ||
302 | static int htcpld_direction_output(struct gpio_chip *chip, | |
303 | unsigned offset, int value) | |
304 | { | |
305 | htcpld_chip_set(chip, offset, value); | |
306 | return 0; | |
307 | } | |
308 | ||
309 | static int htcpld_direction_input(struct gpio_chip *chip, | |
310 | unsigned offset) | |
311 | { | |
312 | /* | |
313 | * No-op: this function can only be called on the input chip. | |
314 | * We do however make sure the offset is within range. | |
315 | */ | |
316 | return (offset < chip->ngpio) ? 0 : -EINVAL; | |
317 | } | |
318 | ||
8d2d3a3a | 319 | static int htcpld_chip_to_irq(struct gpio_chip *chip, unsigned offset) |
6048a3dd CM |
320 | { |
321 | struct htcpld_chip *chip_data; | |
322 | ||
323 | chip_data = container_of(chip, struct htcpld_chip, chip_in); | |
324 | ||
325 | if (offset < chip_data->nirqs) | |
326 | return chip_data->irq_start + offset; | |
327 | else | |
328 | return -EINVAL; | |
329 | } | |
330 | ||
8d2d3a3a | 331 | static void htcpld_chip_reset(struct i2c_client *client) |
6048a3dd CM |
332 | { |
333 | struct htcpld_chip *chip_data = i2c_get_clientdata(client); | |
334 | if (!chip_data) | |
335 | return; | |
336 | ||
337 | i2c_smbus_read_byte_data( | |
338 | client, (chip_data->cache_out = chip_data->reset)); | |
339 | } | |
340 | ||
341 | static int __devinit htcpld_setup_chip_irq( | |
342 | struct platform_device *pdev, | |
343 | int chip_index) | |
344 | { | |
345 | struct htcpld_data *htcpld; | |
346 | struct device *dev = &pdev->dev; | |
347 | struct htcpld_core_platform_data *pdata; | |
348 | struct htcpld_chip *chip; | |
349 | struct htcpld_chip_platform_data *plat_chip_data; | |
350 | unsigned int irq, irq_end; | |
351 | int ret = 0; | |
352 | ||
353 | /* Get the platform and driver data */ | |
354 | pdata = dev->platform_data; | |
355 | htcpld = platform_get_drvdata(pdev); | |
356 | chip = &htcpld->chip[chip_index]; | |
357 | plat_chip_data = &pdata->chip[chip_index]; | |
358 | ||
359 | /* Setup irq handlers */ | |
360 | irq_end = chip->irq_start + chip->nirqs; | |
361 | for (irq = chip->irq_start; irq < irq_end; irq++) { | |
362 | set_irq_chip(irq, &htcpld_muxed_chip); | |
363 | set_irq_chip_data(irq, chip); | |
364 | set_irq_handler(irq, handle_simple_irq); | |
365 | #ifdef CONFIG_ARM | |
366 | set_irq_flags(irq, IRQF_VALID | IRQF_PROBE); | |
367 | #else | |
368 | set_irq_probe(irq); | |
369 | #endif | |
370 | } | |
371 | ||
372 | return ret; | |
373 | } | |
374 | ||
375 | static int __devinit htcpld_register_chip_i2c( | |
376 | struct platform_device *pdev, | |
377 | int chip_index) | |
378 | { | |
379 | struct htcpld_data *htcpld; | |
380 | struct device *dev = &pdev->dev; | |
381 | struct htcpld_core_platform_data *pdata; | |
382 | struct htcpld_chip *chip; | |
383 | struct htcpld_chip_platform_data *plat_chip_data; | |
384 | struct i2c_adapter *adapter; | |
385 | struct i2c_client *client; | |
386 | struct i2c_board_info info; | |
387 | ||
388 | /* Get the platform and driver data */ | |
389 | pdata = dev->platform_data; | |
390 | htcpld = platform_get_drvdata(pdev); | |
391 | chip = &htcpld->chip[chip_index]; | |
392 | plat_chip_data = &pdata->chip[chip_index]; | |
393 | ||
394 | adapter = i2c_get_adapter(pdata->i2c_adapter_id); | |
395 | if (adapter == NULL) { | |
396 | /* Eek, no such I2C adapter! Bail out. */ | |
397 | dev_warn(dev, "Chip at i2c address 0x%x: Invalid i2c adapter %d\n", | |
398 | plat_chip_data->addr, pdata->i2c_adapter_id); | |
399 | return -ENODEV; | |
400 | } | |
401 | ||
402 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_READ_BYTE_DATA)) { | |
403 | dev_warn(dev, "i2c adapter %d non-functional\n", | |
404 | pdata->i2c_adapter_id); | |
405 | return -EINVAL; | |
406 | } | |
407 | ||
408 | memset(&info, 0, sizeof(struct i2c_board_info)); | |
409 | info.addr = plat_chip_data->addr; | |
410 | strlcpy(info.type, "htcpld-chip", I2C_NAME_SIZE); | |
411 | info.platform_data = chip; | |
412 | ||
413 | /* Add the I2C device. This calls the probe() function. */ | |
414 | client = i2c_new_device(adapter, &info); | |
415 | if (!client) { | |
416 | /* I2C device registration failed, contineu with the next */ | |
417 | dev_warn(dev, "Unable to add I2C device for 0x%x\n", | |
418 | plat_chip_data->addr); | |
419 | return -ENODEV; | |
420 | } | |
421 | ||
422 | i2c_set_clientdata(client, chip); | |
423 | snprintf(client->name, I2C_NAME_SIZE, "Chip_0x%d", client->addr); | |
424 | chip->client = client; | |
425 | ||
426 | /* Reset the chip */ | |
427 | htcpld_chip_reset(client); | |
428 | chip->cache_in = i2c_smbus_read_byte_data(client, chip->cache_out); | |
429 | ||
430 | return 0; | |
431 | } | |
432 | ||
433 | static void __devinit htcpld_unregister_chip_i2c( | |
434 | struct platform_device *pdev, | |
435 | int chip_index) | |
436 | { | |
437 | struct htcpld_data *htcpld; | |
438 | struct htcpld_chip *chip; | |
439 | ||
440 | /* Get the platform and driver data */ | |
441 | htcpld = platform_get_drvdata(pdev); | |
442 | chip = &htcpld->chip[chip_index]; | |
443 | ||
444 | if (chip->client) | |
445 | i2c_unregister_device(chip->client); | |
446 | } | |
447 | ||
448 | static int __devinit htcpld_register_chip_gpio( | |
449 | struct platform_device *pdev, | |
450 | int chip_index) | |
451 | { | |
452 | struct htcpld_data *htcpld; | |
453 | struct device *dev = &pdev->dev; | |
454 | struct htcpld_core_platform_data *pdata; | |
455 | struct htcpld_chip *chip; | |
456 | struct htcpld_chip_platform_data *plat_chip_data; | |
457 | struct gpio_chip *gpio_chip; | |
458 | int ret = 0; | |
459 | ||
460 | /* Get the platform and driver data */ | |
461 | pdata = dev->platform_data; | |
462 | htcpld = platform_get_drvdata(pdev); | |
463 | chip = &htcpld->chip[chip_index]; | |
464 | plat_chip_data = &pdata->chip[chip_index]; | |
465 | ||
466 | /* Setup the GPIO chips */ | |
467 | gpio_chip = &(chip->chip_out); | |
468 | gpio_chip->label = "htcpld-out"; | |
469 | gpio_chip->dev = dev; | |
470 | gpio_chip->owner = THIS_MODULE; | |
471 | gpio_chip->get = htcpld_chip_get; | |
472 | gpio_chip->set = htcpld_chip_set; | |
473 | gpio_chip->direction_input = NULL; | |
474 | gpio_chip->direction_output = htcpld_direction_output; | |
475 | gpio_chip->base = plat_chip_data->gpio_out_base; | |
476 | gpio_chip->ngpio = plat_chip_data->num_gpios; | |
477 | ||
478 | gpio_chip = &(chip->chip_in); | |
479 | gpio_chip->label = "htcpld-in"; | |
480 | gpio_chip->dev = dev; | |
481 | gpio_chip->owner = THIS_MODULE; | |
482 | gpio_chip->get = htcpld_chip_get; | |
483 | gpio_chip->set = NULL; | |
484 | gpio_chip->direction_input = htcpld_direction_input; | |
485 | gpio_chip->direction_output = NULL; | |
486 | gpio_chip->to_irq = htcpld_chip_to_irq; | |
487 | gpio_chip->base = plat_chip_data->gpio_in_base; | |
488 | gpio_chip->ngpio = plat_chip_data->num_gpios; | |
489 | ||
490 | /* Add the GPIO chips */ | |
491 | ret = gpiochip_add(&(chip->chip_out)); | |
492 | if (ret) { | |
493 | dev_warn(dev, "Unable to register output GPIOs for 0x%x: %d\n", | |
494 | plat_chip_data->addr, ret); | |
495 | return ret; | |
496 | } | |
497 | ||
498 | ret = gpiochip_add(&(chip->chip_in)); | |
499 | if (ret) { | |
500 | int error; | |
501 | ||
502 | dev_warn(dev, "Unable to register input GPIOs for 0x%x: %d\n", | |
503 | plat_chip_data->addr, ret); | |
504 | ||
505 | error = gpiochip_remove(&(chip->chip_out)); | |
506 | if (error) | |
507 | dev_warn(dev, "Error while trying to unregister gpio chip: %d\n", error); | |
508 | ||
509 | return ret; | |
510 | } | |
511 | ||
512 | return 0; | |
513 | } | |
514 | ||
515 | static int __devinit htcpld_setup_chips(struct platform_device *pdev) | |
516 | { | |
517 | struct htcpld_data *htcpld; | |
518 | struct device *dev = &pdev->dev; | |
519 | struct htcpld_core_platform_data *pdata; | |
520 | int i; | |
521 | ||
522 | /* Get the platform and driver data */ | |
523 | pdata = dev->platform_data; | |
524 | htcpld = platform_get_drvdata(pdev); | |
525 | ||
526 | /* Setup each chip's output GPIOs */ | |
527 | htcpld->nchips = pdata->num_chip; | |
528 | htcpld->chip = kzalloc(sizeof(struct htcpld_chip) * htcpld->nchips, | |
529 | GFP_KERNEL); | |
530 | if (!htcpld->chip) { | |
531 | dev_warn(dev, "Unable to allocate memory for chips\n"); | |
532 | return -ENOMEM; | |
533 | } | |
534 | ||
535 | /* Add the chips as best we can */ | |
536 | for (i = 0; i < htcpld->nchips; i++) { | |
537 | int ret; | |
538 | ||
539 | /* Setup the HTCPLD chips */ | |
540 | htcpld->chip[i].reset = pdata->chip[i].reset; | |
541 | htcpld->chip[i].cache_out = pdata->chip[i].reset; | |
542 | htcpld->chip[i].cache_in = 0; | |
543 | htcpld->chip[i].dev = dev; | |
544 | htcpld->chip[i].irq_start = pdata->chip[i].irq_base; | |
545 | htcpld->chip[i].nirqs = pdata->chip[i].num_irqs; | |
546 | ||
547 | INIT_WORK(&(htcpld->chip[i].set_val_work), &htcpld_chip_set_ni); | |
548 | spin_lock_init(&(htcpld->chip[i].lock)); | |
549 | ||
550 | /* Setup the interrupts for the chip */ | |
551 | if (htcpld->chained_irq) { | |
552 | ret = htcpld_setup_chip_irq(pdev, i); | |
553 | if (ret) | |
554 | continue; | |
555 | } | |
556 | ||
557 | /* Register the chip with I2C */ | |
558 | ret = htcpld_register_chip_i2c(pdev, i); | |
559 | if (ret) | |
560 | continue; | |
561 | ||
562 | ||
563 | /* Register the chips with the GPIO subsystem */ | |
564 | ret = htcpld_register_chip_gpio(pdev, i); | |
565 | if (ret) { | |
566 | /* Unregister the chip from i2c and continue */ | |
567 | htcpld_unregister_chip_i2c(pdev, i); | |
568 | continue; | |
569 | } | |
570 | ||
571 | dev_info(dev, "Registered chip at 0x%x\n", pdata->chip[i].addr); | |
572 | } | |
573 | ||
574 | return 0; | |
575 | } | |
576 | ||
577 | static int __devinit htcpld_core_probe(struct platform_device *pdev) | |
578 | { | |
579 | struct htcpld_data *htcpld; | |
580 | struct device *dev = &pdev->dev; | |
581 | struct htcpld_core_platform_data *pdata; | |
582 | struct resource *res; | |
583 | int ret = 0; | |
584 | ||
585 | if (!dev) | |
586 | return -ENODEV; | |
587 | ||
588 | pdata = dev->platform_data; | |
589 | if (!pdata) { | |
590 | dev_warn(dev, "Platform data not found for htcpld core!\n"); | |
591 | return -ENXIO; | |
592 | } | |
593 | ||
594 | htcpld = kzalloc(sizeof(struct htcpld_data), GFP_KERNEL); | |
595 | if (!htcpld) | |
596 | return -ENOMEM; | |
597 | ||
598 | /* Find chained irq */ | |
599 | ret = -EINVAL; | |
600 | res = platform_get_resource(pdev, IORESOURCE_IRQ, 0); | |
601 | if (res) { | |
602 | int flags; | |
603 | htcpld->chained_irq = res->start; | |
604 | ||
605 | /* Setup the chained interrupt handler */ | |
606 | flags = IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING; | |
607 | ret = request_threaded_irq(htcpld->chained_irq, | |
608 | NULL, htcpld_handler, | |
609 | flags, pdev->name, htcpld); | |
610 | if (ret) { | |
611 | dev_warn(dev, "Unable to setup chained irq handler: %d\n", ret); | |
612 | goto fail; | |
613 | } else | |
614 | device_init_wakeup(dev, 0); | |
615 | } | |
616 | ||
617 | /* Set the driver data */ | |
618 | platform_set_drvdata(pdev, htcpld); | |
619 | ||
620 | /* Setup the htcpld chips */ | |
621 | ret = htcpld_setup_chips(pdev); | |
622 | if (ret) | |
623 | goto fail; | |
624 | ||
625 | /* Request the GPIO(s) for the int reset and set them up */ | |
626 | if (pdata->int_reset_gpio_hi) { | |
627 | ret = gpio_request(pdata->int_reset_gpio_hi, "htcpld-core"); | |
628 | if (ret) { | |
629 | /* | |
630 | * If it failed, that sucks, but we can probably | |
631 | * continue on without it. | |
632 | */ | |
633 | dev_warn(dev, "Unable to request int_reset_gpio_hi -- interrupts may not work\n"); | |
634 | htcpld->int_reset_gpio_hi = 0; | |
635 | } else { | |
636 | htcpld->int_reset_gpio_hi = pdata->int_reset_gpio_hi; | |
637 | gpio_set_value(htcpld->int_reset_gpio_hi, 1); | |
638 | } | |
639 | } | |
640 | ||
641 | if (pdata->int_reset_gpio_lo) { | |
642 | ret = gpio_request(pdata->int_reset_gpio_lo, "htcpld-core"); | |
643 | if (ret) { | |
644 | /* | |
645 | * If it failed, that sucks, but we can probably | |
646 | * continue on without it. | |
647 | */ | |
648 | dev_warn(dev, "Unable to request int_reset_gpio_lo -- interrupts may not work\n"); | |
649 | htcpld->int_reset_gpio_lo = 0; | |
650 | } else { | |
651 | htcpld->int_reset_gpio_lo = pdata->int_reset_gpio_lo; | |
652 | gpio_set_value(htcpld->int_reset_gpio_lo, 0); | |
653 | } | |
654 | } | |
655 | ||
656 | dev_info(dev, "Initialized successfully\n"); | |
657 | return 0; | |
658 | ||
659 | fail: | |
660 | kfree(htcpld); | |
661 | return ret; | |
662 | } | |
663 | ||
664 | /* The I2C Driver -- used internally */ | |
665 | static const struct i2c_device_id htcpld_chip_id[] = { | |
666 | { "htcpld-chip", 0 }, | |
667 | { } | |
668 | }; | |
669 | MODULE_DEVICE_TABLE(i2c, htcpld_chip_id); | |
670 | ||
671 | ||
672 | static struct i2c_driver htcpld_chip_driver = { | |
673 | .driver = { | |
674 | .name = "htcpld-chip", | |
675 | }, | |
676 | .id_table = htcpld_chip_id, | |
677 | }; | |
678 | ||
679 | /* The Core Driver */ | |
680 | static struct platform_driver htcpld_core_driver = { | |
681 | .driver = { | |
682 | .name = "i2c-htcpld", | |
683 | }, | |
684 | }; | |
685 | ||
686 | static int __init htcpld_core_init(void) | |
687 | { | |
688 | int ret; | |
689 | ||
690 | /* Register the I2C Chip driver */ | |
691 | ret = i2c_add_driver(&htcpld_chip_driver); | |
692 | if (ret) | |
693 | return ret; | |
694 | ||
695 | /* Probe for our chips */ | |
696 | return platform_driver_probe(&htcpld_core_driver, htcpld_core_probe); | |
697 | } | |
698 | ||
699 | static void __exit htcpld_core_exit(void) | |
700 | { | |
701 | i2c_del_driver(&htcpld_chip_driver); | |
702 | platform_driver_unregister(&htcpld_core_driver); | |
703 | } | |
704 | ||
705 | module_init(htcpld_core_init); | |
706 | module_exit(htcpld_core_exit); | |
707 | ||
708 | MODULE_AUTHOR("Cory Maccarrone <darkstar6262@gmail.com>"); | |
709 | MODULE_DESCRIPTION("I2C HTC PLD Driver"); | |
710 | MODULE_LICENSE("GPL"); | |
711 |