Staging: rdma: amso1100: Remove unnecessary cast on void pointer
[linux/fpc-iii.git] / drivers / mfd / htc-i2cpld.c
blob1bd5b042c8b33b363845eb49ce5e0e161b402601
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.
8 * Copyright (C) 2009 Cory Maccarrone <darkstar6262@gmail.com>
10 * Based on work done in the linwizard project
11 * Copyright (C) 2008-2009 Angelo Arrifano <miknix@gmail.com>
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.
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.
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.
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>
38 #include <linux/slab.h>
40 struct htcpld_chip {
41 spinlock_t lock;
43 /* chip info */
44 u8 reset;
45 u8 addr;
46 struct device *dev;
47 struct i2c_client *client;
49 /* Output details */
50 u8 cache_out;
51 struct gpio_chip chip_out;
53 /* Input details */
54 u8 cache_in;
55 struct gpio_chip chip_in;
57 u16 irqs_enabled;
58 uint irq_start;
59 int nirqs;
61 unsigned int flow_type;
63 * Work structure to allow for setting values outside of any
64 * possible interrupt context
66 struct work_struct set_val_work;
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;
78 /* htcpld info */
79 struct htcpld_chip *chip;
80 unsigned int nchips;
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. */
86 static void htcpld_mask(struct irq_data *data)
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);
92 static void htcpld_unmask(struct irq_data *data)
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);
99 static int htcpld_set_type(struct irq_data *data, unsigned int flags)
101 struct htcpld_chip *chip = irq_data_get_irq_chip_data(data);
103 if (flags & ~IRQ_TYPE_SENSE_MASK)
104 return -EINVAL;
106 /* We only allow edge triggering */
107 if (flags & (IRQ_TYPE_LEVEL_LOW|IRQ_TYPE_LEVEL_HIGH))
108 return -EINVAL;
110 chip->flow_type = flags;
111 return 0;
114 static struct irq_chip htcpld_muxed_chip = {
115 .name = "htcpld",
116 .irq_mask = htcpld_mask,
117 .irq_unmask = htcpld_unmask,
118 .irq_set_type = htcpld_set_type,
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.
126 static irqreturn_t htcpld_handler(int irq, void *dev)
128 struct htcpld_data *htcpld = dev;
129 unsigned int i;
130 unsigned long flags;
131 int irqpin;
133 if (!htcpld) {
134 pr_debug("htcpld is null in ISR\n");
135 return IRQ_HANDLED;
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.)
143 * For chips that have no interrupt range specified, just skip 'em.
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;
151 if (!chip) {
152 pr_debug("chip %d is null in ISR\n", i);
153 continue;
156 if (chip->nirqs == 0)
157 continue;
159 client = chip->client;
160 if (!client) {
161 pr_debug("client %d is null in ISR\n", i);
162 continue;
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;
174 uval = (unsigned long)val;
176 spin_lock_irqsave(&chip->lock, flags);
178 /* Save away the old value so we can compare it */
179 old_val = chip->cache_in;
181 /* Write the new value */
182 chip->cache_in = uval;
184 spin_unlock_irqrestore(&chip->lock, flags);
187 * For each bit in the data (starting at bit 0), trigger
188 * associated interrupts.
190 for (irqpin = 0; irqpin < chip->nirqs; irqpin++) {
191 unsigned oldb, newb, type = chip->flow_type;
193 irq = chip->irq_start + irqpin;
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;
200 if ((!oldb && newb && (type & IRQ_TYPE_EDGE_RISING)) ||
201 (oldb && !newb && (type & IRQ_TYPE_EDGE_FALLING))) {
202 pr_debug("fire IRQ %d\n", irqpin);
203 generic_handle_irq(irq);
209 * In order to continue receiving interrupts, the int_reset_gpio must
210 * be asserted.
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);
217 return IRQ_HANDLED;
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.
227 static void htcpld_chip_set(struct gpio_chip *chip, unsigned offset, int val)
229 struct i2c_client *client;
230 struct htcpld_chip *chip_data =
231 container_of(chip, struct htcpld_chip, chip_out);
232 unsigned long flags;
234 client = chip_data->client;
235 if (!client)
236 return;
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);
245 schedule_work(&(chip_data->set_val_work));
248 static void htcpld_chip_set_ni(struct work_struct *work)
250 struct htcpld_chip *chip_data;
251 struct i2c_client *client;
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);
258 static int htcpld_chip_get(struct gpio_chip *chip, unsigned offset)
260 struct htcpld_chip *chip_data;
261 u8 cache;
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;
272 return (cache >> offset) & 1;
275 static int htcpld_direction_output(struct gpio_chip *chip,
276 unsigned offset, int value)
278 htcpld_chip_set(chip, offset, value);
279 return 0;
282 static int htcpld_direction_input(struct gpio_chip *chip,
283 unsigned offset)
286 * No-op: this function can only be called on the input chip.
287 * We do however make sure the offset is within range.
289 return (offset < chip->ngpio) ? 0 : -EINVAL;
292 static int htcpld_chip_to_irq(struct gpio_chip *chip, unsigned offset)
294 struct htcpld_chip *chip_data;
296 chip_data = container_of(chip, struct htcpld_chip, chip_in);
298 if (offset < chip_data->nirqs)
299 return chip_data->irq_start + offset;
300 else
301 return -EINVAL;
304 static void htcpld_chip_reset(struct i2c_client *client)
306 struct htcpld_chip *chip_data = i2c_get_clientdata(client);
307 if (!chip_data)
308 return;
310 i2c_smbus_read_byte_data(
311 client, (chip_data->cache_out = chip_data->reset));
314 static int htcpld_setup_chip_irq(
315 struct platform_device *pdev,
316 int chip_index)
318 struct htcpld_data *htcpld;
319 struct htcpld_chip *chip;
320 unsigned int irq, irq_end;
321 int ret = 0;
323 /* Get the platform and driver data */
324 htcpld = platform_get_drvdata(pdev);
325 chip = &htcpld->chip[chip_index];
327 /* Setup irq handlers */
328 irq_end = chip->irq_start + chip->nirqs;
329 for (irq = chip->irq_start; irq < irq_end; irq++) {
330 irq_set_chip_and_handler(irq, &htcpld_muxed_chip,
331 handle_simple_irq);
332 irq_set_chip_data(irq, chip);
333 irq_clear_status_flags(irq, IRQ_NOREQUEST | IRQ_NOPROBE);
336 return ret;
339 static int htcpld_register_chip_i2c(
340 struct platform_device *pdev,
341 int chip_index)
343 struct htcpld_data *htcpld;
344 struct device *dev = &pdev->dev;
345 struct htcpld_core_platform_data *pdata;
346 struct htcpld_chip *chip;
347 struct htcpld_chip_platform_data *plat_chip_data;
348 struct i2c_adapter *adapter;
349 struct i2c_client *client;
350 struct i2c_board_info info;
352 /* Get the platform and driver data */
353 pdata = dev_get_platdata(dev);
354 htcpld = platform_get_drvdata(pdev);
355 chip = &htcpld->chip[chip_index];
356 plat_chip_data = &pdata->chip[chip_index];
358 adapter = i2c_get_adapter(pdata->i2c_adapter_id);
359 if (!adapter) {
360 /* Eek, no such I2C adapter! Bail out. */
361 dev_warn(dev, "Chip at i2c address 0x%x: Invalid i2c adapter %d\n",
362 plat_chip_data->addr, pdata->i2c_adapter_id);
363 return -ENODEV;
366 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_READ_BYTE_DATA)) {
367 dev_warn(dev, "i2c adapter %d non-functional\n",
368 pdata->i2c_adapter_id);
369 return -EINVAL;
372 memset(&info, 0, sizeof(struct i2c_board_info));
373 info.addr = plat_chip_data->addr;
374 strlcpy(info.type, "htcpld-chip", I2C_NAME_SIZE);
375 info.platform_data = chip;
377 /* Add the I2C device. This calls the probe() function. */
378 client = i2c_new_device(adapter, &info);
379 if (!client) {
380 /* I2C device registration failed, contineu with the next */
381 dev_warn(dev, "Unable to add I2C device for 0x%x\n",
382 plat_chip_data->addr);
383 return -ENODEV;
386 i2c_set_clientdata(client, chip);
387 snprintf(client->name, I2C_NAME_SIZE, "Chip_0x%x", client->addr);
388 chip->client = client;
390 /* Reset the chip */
391 htcpld_chip_reset(client);
392 chip->cache_in = i2c_smbus_read_byte_data(client, chip->cache_out);
394 return 0;
397 static void htcpld_unregister_chip_i2c(
398 struct platform_device *pdev,
399 int chip_index)
401 struct htcpld_data *htcpld;
402 struct htcpld_chip *chip;
404 /* Get the platform and driver data */
405 htcpld = platform_get_drvdata(pdev);
406 chip = &htcpld->chip[chip_index];
408 if (chip->client)
409 i2c_unregister_device(chip->client);
412 static int htcpld_register_chip_gpio(
413 struct platform_device *pdev,
414 int chip_index)
416 struct htcpld_data *htcpld;
417 struct device *dev = &pdev->dev;
418 struct htcpld_core_platform_data *pdata;
419 struct htcpld_chip *chip;
420 struct htcpld_chip_platform_data *plat_chip_data;
421 struct gpio_chip *gpio_chip;
422 int ret = 0;
424 /* Get the platform and driver data */
425 pdata = dev_get_platdata(dev);
426 htcpld = platform_get_drvdata(pdev);
427 chip = &htcpld->chip[chip_index];
428 plat_chip_data = &pdata->chip[chip_index];
430 /* Setup the GPIO chips */
431 gpio_chip = &(chip->chip_out);
432 gpio_chip->label = "htcpld-out";
433 gpio_chip->dev = dev;
434 gpio_chip->owner = THIS_MODULE;
435 gpio_chip->get = htcpld_chip_get;
436 gpio_chip->set = htcpld_chip_set;
437 gpio_chip->direction_input = NULL;
438 gpio_chip->direction_output = htcpld_direction_output;
439 gpio_chip->base = plat_chip_data->gpio_out_base;
440 gpio_chip->ngpio = plat_chip_data->num_gpios;
442 gpio_chip = &(chip->chip_in);
443 gpio_chip->label = "htcpld-in";
444 gpio_chip->dev = dev;
445 gpio_chip->owner = THIS_MODULE;
446 gpio_chip->get = htcpld_chip_get;
447 gpio_chip->set = NULL;
448 gpio_chip->direction_input = htcpld_direction_input;
449 gpio_chip->direction_output = NULL;
450 gpio_chip->to_irq = htcpld_chip_to_irq;
451 gpio_chip->base = plat_chip_data->gpio_in_base;
452 gpio_chip->ngpio = plat_chip_data->num_gpios;
454 /* Add the GPIO chips */
455 ret = gpiochip_add(&(chip->chip_out));
456 if (ret) {
457 dev_warn(dev, "Unable to register output GPIOs for 0x%x: %d\n",
458 plat_chip_data->addr, ret);
459 return ret;
462 ret = gpiochip_add(&(chip->chip_in));
463 if (ret) {
464 dev_warn(dev, "Unable to register input GPIOs for 0x%x: %d\n",
465 plat_chip_data->addr, ret);
466 gpiochip_remove(&(chip->chip_out));
467 return ret;
470 return 0;
473 static int htcpld_setup_chips(struct platform_device *pdev)
475 struct htcpld_data *htcpld;
476 struct device *dev = &pdev->dev;
477 struct htcpld_core_platform_data *pdata;
478 int i;
480 /* Get the platform and driver data */
481 pdata = dev_get_platdata(dev);
482 htcpld = platform_get_drvdata(pdev);
484 /* Setup each chip's output GPIOs */
485 htcpld->nchips = pdata->num_chip;
486 htcpld->chip = devm_kzalloc(dev, sizeof(struct htcpld_chip) * htcpld->nchips,
487 GFP_KERNEL);
488 if (!htcpld->chip) {
489 dev_warn(dev, "Unable to allocate memory for chips\n");
490 return -ENOMEM;
493 /* Add the chips as best we can */
494 for (i = 0; i < htcpld->nchips; i++) {
495 int ret;
497 /* Setup the HTCPLD chips */
498 htcpld->chip[i].reset = pdata->chip[i].reset;
499 htcpld->chip[i].cache_out = pdata->chip[i].reset;
500 htcpld->chip[i].cache_in = 0;
501 htcpld->chip[i].dev = dev;
502 htcpld->chip[i].irq_start = pdata->chip[i].irq_base;
503 htcpld->chip[i].nirqs = pdata->chip[i].num_irqs;
505 INIT_WORK(&(htcpld->chip[i].set_val_work), &htcpld_chip_set_ni);
506 spin_lock_init(&(htcpld->chip[i].lock));
508 /* Setup the interrupts for the chip */
509 if (htcpld->chained_irq) {
510 ret = htcpld_setup_chip_irq(pdev, i);
511 if (ret)
512 continue;
515 /* Register the chip with I2C */
516 ret = htcpld_register_chip_i2c(pdev, i);
517 if (ret)
518 continue;
521 /* Register the chips with the GPIO subsystem */
522 ret = htcpld_register_chip_gpio(pdev, i);
523 if (ret) {
524 /* Unregister the chip from i2c and continue */
525 htcpld_unregister_chip_i2c(pdev, i);
526 continue;
529 dev_info(dev, "Registered chip at 0x%x\n", pdata->chip[i].addr);
532 return 0;
535 static int htcpld_core_probe(struct platform_device *pdev)
537 struct htcpld_data *htcpld;
538 struct device *dev = &pdev->dev;
539 struct htcpld_core_platform_data *pdata;
540 struct resource *res;
541 int ret = 0;
543 if (!dev)
544 return -ENODEV;
546 pdata = dev_get_platdata(dev);
547 if (!pdata) {
548 dev_warn(dev, "Platform data not found for htcpld core!\n");
549 return -ENXIO;
552 htcpld = devm_kzalloc(dev, sizeof(struct htcpld_data), GFP_KERNEL);
553 if (!htcpld)
554 return -ENOMEM;
556 /* Find chained irq */
557 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
558 if (res) {
559 int flags;
560 htcpld->chained_irq = res->start;
562 /* Setup the chained interrupt handler */
563 flags = IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING |
564 IRQF_ONESHOT;
565 ret = request_threaded_irq(htcpld->chained_irq,
566 NULL, htcpld_handler,
567 flags, pdev->name, htcpld);
568 if (ret) {
569 dev_warn(dev, "Unable to setup chained irq handler: %d\n", ret);
570 return ret;
571 } else
572 device_init_wakeup(dev, 0);
575 /* Set the driver data */
576 platform_set_drvdata(pdev, htcpld);
578 /* Setup the htcpld chips */
579 ret = htcpld_setup_chips(pdev);
580 if (ret)
581 return ret;
583 /* Request the GPIO(s) for the int reset and set them up */
584 if (pdata->int_reset_gpio_hi) {
585 ret = gpio_request(pdata->int_reset_gpio_hi, "htcpld-core");
586 if (ret) {
588 * If it failed, that sucks, but we can probably
589 * continue on without it.
591 dev_warn(dev, "Unable to request int_reset_gpio_hi -- interrupts may not work\n");
592 htcpld->int_reset_gpio_hi = 0;
593 } else {
594 htcpld->int_reset_gpio_hi = pdata->int_reset_gpio_hi;
595 gpio_set_value(htcpld->int_reset_gpio_hi, 1);
599 if (pdata->int_reset_gpio_lo) {
600 ret = gpio_request(pdata->int_reset_gpio_lo, "htcpld-core");
601 if (ret) {
603 * If it failed, that sucks, but we can probably
604 * continue on without it.
606 dev_warn(dev, "Unable to request int_reset_gpio_lo -- interrupts may not work\n");
607 htcpld->int_reset_gpio_lo = 0;
608 } else {
609 htcpld->int_reset_gpio_lo = pdata->int_reset_gpio_lo;
610 gpio_set_value(htcpld->int_reset_gpio_lo, 0);
614 dev_info(dev, "Initialized successfully\n");
615 return 0;
618 /* The I2C Driver -- used internally */
619 static const struct i2c_device_id htcpld_chip_id[] = {
620 { "htcpld-chip", 0 },
623 MODULE_DEVICE_TABLE(i2c, htcpld_chip_id);
626 static struct i2c_driver htcpld_chip_driver = {
627 .driver = {
628 .name = "htcpld-chip",
630 .id_table = htcpld_chip_id,
633 /* The Core Driver */
634 static struct platform_driver htcpld_core_driver = {
635 .driver = {
636 .name = "i2c-htcpld",
640 static int __init htcpld_core_init(void)
642 int ret;
644 /* Register the I2C Chip driver */
645 ret = i2c_add_driver(&htcpld_chip_driver);
646 if (ret)
647 return ret;
649 /* Probe for our chips */
650 return platform_driver_probe(&htcpld_core_driver, htcpld_core_probe);
653 static void __exit htcpld_core_exit(void)
655 i2c_del_driver(&htcpld_chip_driver);
656 platform_driver_unregister(&htcpld_core_driver);
659 module_init(htcpld_core_init);
660 module_exit(htcpld_core_exit);
662 MODULE_AUTHOR("Cory Maccarrone <darkstar6262@gmail.com>");
663 MODULE_DESCRIPTION("I2C HTC PLD Driver");
664 MODULE_LICENSE("GPL");