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