Linux 5.1.15
[linux/fpc-iii.git] / drivers / mfd / htc-i2cpld.c
blobaf3c66355270cef1b1f88a121c4fd9ee3277df1a
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/interrupt.h>
31 #include <linux/platform_device.h>
32 #include <linux/i2c.h>
33 #include <linux/irq.h>
34 #include <linux/spinlock.h>
35 #include <linux/htcpld.h>
36 #include <linux/gpio.h>
37 #include <linux/slab.h>
39 struct htcpld_chip {
40 spinlock_t lock;
42 /* chip info */
43 u8 reset;
44 u8 addr;
45 struct device *dev;
46 struct i2c_client *client;
48 /* Output details */
49 u8 cache_out;
50 struct gpio_chip chip_out;
52 /* Input details */
53 u8 cache_in;
54 struct gpio_chip chip_in;
56 u16 irqs_enabled;
57 uint irq_start;
58 int nirqs;
60 unsigned int flow_type;
62 * Work structure to allow for setting values outside of any
63 * possible interrupt context
65 struct work_struct set_val_work;
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;
77 /* htcpld info */
78 struct htcpld_chip *chip;
79 unsigned int nchips;
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. */
85 static void htcpld_mask(struct irq_data *data)
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);
91 static void htcpld_unmask(struct irq_data *data)
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);
98 static int htcpld_set_type(struct irq_data *data, unsigned int flags)
100 struct htcpld_chip *chip = irq_data_get_irq_chip_data(data);
102 if (flags & ~IRQ_TYPE_SENSE_MASK)
103 return -EINVAL;
105 /* We only allow edge triggering */
106 if (flags & (IRQ_TYPE_LEVEL_LOW|IRQ_TYPE_LEVEL_HIGH))
107 return -EINVAL;
109 chip->flow_type = flags;
110 return 0;
113 static struct irq_chip htcpld_muxed_chip = {
114 .name = "htcpld",
115 .irq_mask = htcpld_mask,
116 .irq_unmask = htcpld_unmask,
117 .irq_set_type = htcpld_set_type,
120 /* To properly dispatch IRQ events, we need to read from the
121 * chip. This is an I2C action that could possibly sleep
122 * (which is bad in interrupt context) -- so we use a threaded
123 * interrupt handler to get around that.
125 static irqreturn_t htcpld_handler(int irq, void *dev)
127 struct htcpld_data *htcpld = dev;
128 unsigned int i;
129 unsigned long flags;
130 int irqpin;
132 if (!htcpld) {
133 pr_debug("htcpld is null in ISR\n");
134 return IRQ_HANDLED;
138 * For each chip, do a read of the chip and trigger any interrupts
139 * desired. The interrupts will be triggered from LSB to MSB (i.e.
140 * bit 0 first, then bit 1, etc.)
142 * For chips that have no interrupt range specified, just skip 'em.
144 for (i = 0; i < htcpld->nchips; i++) {
145 struct htcpld_chip *chip = &htcpld->chip[i];
146 struct i2c_client *client;
147 int val;
148 unsigned long uval, old_val;
150 if (!chip) {
151 pr_debug("chip %d is null in ISR\n", i);
152 continue;
155 if (chip->nirqs == 0)
156 continue;
158 client = chip->client;
159 if (!client) {
160 pr_debug("client %d is null in ISR\n", i);
161 continue;
164 /* Scan the chip */
165 val = i2c_smbus_read_byte_data(client, chip->cache_out);
166 if (val < 0) {
167 /* Throw a warning and skip this chip */
168 dev_warn(chip->dev, "Unable to read from chip: %d\n",
169 val);
170 continue;
173 uval = (unsigned long)val;
175 spin_lock_irqsave(&chip->lock, flags);
177 /* Save away the old value so we can compare it */
178 old_val = chip->cache_in;
180 /* Write the new value */
181 chip->cache_in = uval;
183 spin_unlock_irqrestore(&chip->lock, flags);
186 * For each bit in the data (starting at bit 0), trigger
187 * associated interrupts.
189 for (irqpin = 0; irqpin < chip->nirqs; irqpin++) {
190 unsigned oldb, newb, type = chip->flow_type;
192 irq = chip->irq_start + irqpin;
194 /* Run the IRQ handler, but only if the bit value
195 * changed, and the proper flags are set */
196 oldb = (old_val >> irqpin) & 1;
197 newb = (uval >> irqpin) & 1;
199 if ((!oldb && newb && (type & IRQ_TYPE_EDGE_RISING)) ||
200 (oldb && !newb && (type & IRQ_TYPE_EDGE_FALLING))) {
201 pr_debug("fire IRQ %d\n", irqpin);
202 generic_handle_irq(irq);
208 * In order to continue receiving interrupts, the int_reset_gpio must
209 * be asserted.
211 if (htcpld->int_reset_gpio_hi)
212 gpio_set_value(htcpld->int_reset_gpio_hi, 1);
213 if (htcpld->int_reset_gpio_lo)
214 gpio_set_value(htcpld->int_reset_gpio_lo, 0);
216 return IRQ_HANDLED;
220 * The GPIO set routines can be called from interrupt context, especially if,
221 * for example they're attached to the led-gpio framework and a trigger is
222 * enabled. As such, we declared work above in the htcpld_chip structure,
223 * and that work is scheduled in the set routine. The kernel can then run
224 * the I2C functions, which will sleep, in process context.
226 static void htcpld_chip_set(struct gpio_chip *chip, unsigned offset, int val)
228 struct i2c_client *client;
229 struct htcpld_chip *chip_data = gpiochip_get_data(chip);
230 unsigned long flags;
232 client = chip_data->client;
233 if (!client)
234 return;
236 spin_lock_irqsave(&chip_data->lock, flags);
237 if (val)
238 chip_data->cache_out |= (1 << offset);
239 else
240 chip_data->cache_out &= ~(1 << offset);
241 spin_unlock_irqrestore(&chip_data->lock, flags);
243 schedule_work(&(chip_data->set_val_work));
246 static void htcpld_chip_set_ni(struct work_struct *work)
248 struct htcpld_chip *chip_data;
249 struct i2c_client *client;
251 chip_data = container_of(work, struct htcpld_chip, set_val_work);
252 client = chip_data->client;
253 i2c_smbus_read_byte_data(client, chip_data->cache_out);
256 static int htcpld_chip_get(struct gpio_chip *chip, unsigned offset)
258 struct htcpld_chip *chip_data = gpiochip_get_data(chip);
259 u8 cache;
261 if (!strncmp(chip->label, "htcpld-out", 10)) {
262 cache = chip_data->cache_out;
263 } else if (!strncmp(chip->label, "htcpld-in", 9)) {
264 cache = chip_data->cache_in;
265 } else
266 return -EINVAL;
268 return (cache >> offset) & 1;
271 static int htcpld_direction_output(struct gpio_chip *chip,
272 unsigned offset, int value)
274 htcpld_chip_set(chip, offset, value);
275 return 0;
278 static int htcpld_direction_input(struct gpio_chip *chip,
279 unsigned offset)
282 * No-op: this function can only be called on the input chip.
283 * We do however make sure the offset is within range.
285 return (offset < chip->ngpio) ? 0 : -EINVAL;
288 static int htcpld_chip_to_irq(struct gpio_chip *chip, unsigned offset)
290 struct htcpld_chip *chip_data = gpiochip_get_data(chip);
292 if (offset < chip_data->nirqs)
293 return chip_data->irq_start + offset;
294 else
295 return -EINVAL;
298 static void htcpld_chip_reset(struct i2c_client *client)
300 struct htcpld_chip *chip_data = i2c_get_clientdata(client);
301 if (!chip_data)
302 return;
304 i2c_smbus_read_byte_data(
305 client, (chip_data->cache_out = chip_data->reset));
308 static int htcpld_setup_chip_irq(
309 struct platform_device *pdev,
310 int chip_index)
312 struct htcpld_data *htcpld;
313 struct htcpld_chip *chip;
314 unsigned int irq, irq_end;
316 /* Get the platform and driver data */
317 htcpld = platform_get_drvdata(pdev);
318 chip = &htcpld->chip[chip_index];
320 /* Setup irq handlers */
321 irq_end = chip->irq_start + chip->nirqs;
322 for (irq = chip->irq_start; irq < irq_end; irq++) {
323 irq_set_chip_and_handler(irq, &htcpld_muxed_chip,
324 handle_simple_irq);
325 irq_set_chip_data(irq, chip);
326 irq_clear_status_flags(irq, IRQ_NOREQUEST | IRQ_NOPROBE);
329 return 0;
332 static int htcpld_register_chip_i2c(
333 struct platform_device *pdev,
334 int chip_index)
336 struct htcpld_data *htcpld;
337 struct device *dev = &pdev->dev;
338 struct htcpld_core_platform_data *pdata;
339 struct htcpld_chip *chip;
340 struct htcpld_chip_platform_data *plat_chip_data;
341 struct i2c_adapter *adapter;
342 struct i2c_client *client;
343 struct i2c_board_info info;
345 /* Get the platform and driver data */
346 pdata = dev_get_platdata(dev);
347 htcpld = platform_get_drvdata(pdev);
348 chip = &htcpld->chip[chip_index];
349 plat_chip_data = &pdata->chip[chip_index];
351 adapter = i2c_get_adapter(pdata->i2c_adapter_id);
352 if (!adapter) {
353 /* Eek, no such I2C adapter! Bail out. */
354 dev_warn(dev, "Chip at i2c address 0x%x: Invalid i2c adapter %d\n",
355 plat_chip_data->addr, pdata->i2c_adapter_id);
356 return -ENODEV;
359 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_READ_BYTE_DATA)) {
360 dev_warn(dev, "i2c adapter %d non-functional\n",
361 pdata->i2c_adapter_id);
362 return -EINVAL;
365 memset(&info, 0, sizeof(struct i2c_board_info));
366 info.addr = plat_chip_data->addr;
367 strlcpy(info.type, "htcpld-chip", I2C_NAME_SIZE);
368 info.platform_data = chip;
370 /* Add the I2C device. This calls the probe() function. */
371 client = i2c_new_device(adapter, &info);
372 if (!client) {
373 /* I2C device registration failed, contineu with the next */
374 dev_warn(dev, "Unable to add I2C device for 0x%x\n",
375 plat_chip_data->addr);
376 return -ENODEV;
379 i2c_set_clientdata(client, chip);
380 snprintf(client->name, I2C_NAME_SIZE, "Chip_0x%x", client->addr);
381 chip->client = client;
383 /* Reset the chip */
384 htcpld_chip_reset(client);
385 chip->cache_in = i2c_smbus_read_byte_data(client, chip->cache_out);
387 return 0;
390 static void htcpld_unregister_chip_i2c(
391 struct platform_device *pdev,
392 int chip_index)
394 struct htcpld_data *htcpld;
395 struct htcpld_chip *chip;
397 /* Get the platform and driver data */
398 htcpld = platform_get_drvdata(pdev);
399 chip = &htcpld->chip[chip_index];
401 if (chip->client)
402 i2c_unregister_device(chip->client);
405 static int htcpld_register_chip_gpio(
406 struct platform_device *pdev,
407 int chip_index)
409 struct htcpld_data *htcpld;
410 struct device *dev = &pdev->dev;
411 struct htcpld_core_platform_data *pdata;
412 struct htcpld_chip *chip;
413 struct htcpld_chip_platform_data *plat_chip_data;
414 struct gpio_chip *gpio_chip;
415 int ret = 0;
417 /* Get the platform and driver data */
418 pdata = dev_get_platdata(dev);
419 htcpld = platform_get_drvdata(pdev);
420 chip = &htcpld->chip[chip_index];
421 plat_chip_data = &pdata->chip[chip_index];
423 /* Setup the GPIO chips */
424 gpio_chip = &(chip->chip_out);
425 gpio_chip->label = "htcpld-out";
426 gpio_chip->parent = dev;
427 gpio_chip->owner = THIS_MODULE;
428 gpio_chip->get = htcpld_chip_get;
429 gpio_chip->set = htcpld_chip_set;
430 gpio_chip->direction_input = NULL;
431 gpio_chip->direction_output = htcpld_direction_output;
432 gpio_chip->base = plat_chip_data->gpio_out_base;
433 gpio_chip->ngpio = plat_chip_data->num_gpios;
435 gpio_chip = &(chip->chip_in);
436 gpio_chip->label = "htcpld-in";
437 gpio_chip->parent = dev;
438 gpio_chip->owner = THIS_MODULE;
439 gpio_chip->get = htcpld_chip_get;
440 gpio_chip->set = NULL;
441 gpio_chip->direction_input = htcpld_direction_input;
442 gpio_chip->direction_output = NULL;
443 gpio_chip->to_irq = htcpld_chip_to_irq;
444 gpio_chip->base = plat_chip_data->gpio_in_base;
445 gpio_chip->ngpio = plat_chip_data->num_gpios;
447 /* Add the GPIO chips */
448 ret = gpiochip_add_data(&(chip->chip_out), chip);
449 if (ret) {
450 dev_warn(dev, "Unable to register output GPIOs for 0x%x: %d\n",
451 plat_chip_data->addr, ret);
452 return ret;
455 ret = gpiochip_add_data(&(chip->chip_in), chip);
456 if (ret) {
457 dev_warn(dev, "Unable to register input GPIOs for 0x%x: %d\n",
458 plat_chip_data->addr, ret);
459 gpiochip_remove(&(chip->chip_out));
460 return ret;
463 return 0;
466 static int htcpld_setup_chips(struct platform_device *pdev)
468 struct htcpld_data *htcpld;
469 struct device *dev = &pdev->dev;
470 struct htcpld_core_platform_data *pdata;
471 int i;
473 /* Get the platform and driver data */
474 pdata = dev_get_platdata(dev);
475 htcpld = platform_get_drvdata(pdev);
477 /* Setup each chip's output GPIOs */
478 htcpld->nchips = pdata->num_chip;
479 htcpld->chip = devm_kcalloc(dev,
480 htcpld->nchips,
481 sizeof(struct htcpld_chip),
482 GFP_KERNEL);
483 if (!htcpld->chip)
484 return -ENOMEM;
486 /* Add the chips as best we can */
487 for (i = 0; i < htcpld->nchips; i++) {
488 int ret;
490 /* Setup the HTCPLD chips */
491 htcpld->chip[i].reset = pdata->chip[i].reset;
492 htcpld->chip[i].cache_out = pdata->chip[i].reset;
493 htcpld->chip[i].cache_in = 0;
494 htcpld->chip[i].dev = dev;
495 htcpld->chip[i].irq_start = pdata->chip[i].irq_base;
496 htcpld->chip[i].nirqs = pdata->chip[i].num_irqs;
498 INIT_WORK(&(htcpld->chip[i].set_val_work), &htcpld_chip_set_ni);
499 spin_lock_init(&(htcpld->chip[i].lock));
501 /* Setup the interrupts for the chip */
502 if (htcpld->chained_irq) {
503 ret = htcpld_setup_chip_irq(pdev, i);
504 if (ret)
505 continue;
508 /* Register the chip with I2C */
509 ret = htcpld_register_chip_i2c(pdev, i);
510 if (ret)
511 continue;
514 /* Register the chips with the GPIO subsystem */
515 ret = htcpld_register_chip_gpio(pdev, i);
516 if (ret) {
517 /* Unregister the chip from i2c and continue */
518 htcpld_unregister_chip_i2c(pdev, i);
519 continue;
522 dev_info(dev, "Registered chip at 0x%x\n", pdata->chip[i].addr);
525 return 0;
528 static int htcpld_core_probe(struct platform_device *pdev)
530 struct htcpld_data *htcpld;
531 struct device *dev = &pdev->dev;
532 struct htcpld_core_platform_data *pdata;
533 struct resource *res;
534 int ret = 0;
536 if (!dev)
537 return -ENODEV;
539 pdata = dev_get_platdata(dev);
540 if (!pdata) {
541 dev_warn(dev, "Platform data not found for htcpld core!\n");
542 return -ENXIO;
545 htcpld = devm_kzalloc(dev, sizeof(struct htcpld_data), GFP_KERNEL);
546 if (!htcpld)
547 return -ENOMEM;
549 /* Find chained irq */
550 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
551 if (res) {
552 int flags;
553 htcpld->chained_irq = res->start;
555 /* Setup the chained interrupt handler */
556 flags = IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING |
557 IRQF_ONESHOT;
558 ret = request_threaded_irq(htcpld->chained_irq,
559 NULL, htcpld_handler,
560 flags, pdev->name, htcpld);
561 if (ret) {
562 dev_warn(dev, "Unable to setup chained irq handler: %d\n", ret);
563 return ret;
564 } else
565 device_init_wakeup(dev, 0);
568 /* Set the driver data */
569 platform_set_drvdata(pdev, htcpld);
571 /* Setup the htcpld chips */
572 ret = htcpld_setup_chips(pdev);
573 if (ret)
574 return ret;
576 /* Request the GPIO(s) for the int reset and set them up */
577 if (pdata->int_reset_gpio_hi) {
578 ret = gpio_request(pdata->int_reset_gpio_hi, "htcpld-core");
579 if (ret) {
581 * If it failed, that sucks, but we can probably
582 * continue on without it.
584 dev_warn(dev, "Unable to request int_reset_gpio_hi -- interrupts may not work\n");
585 htcpld->int_reset_gpio_hi = 0;
586 } else {
587 htcpld->int_reset_gpio_hi = pdata->int_reset_gpio_hi;
588 gpio_set_value(htcpld->int_reset_gpio_hi, 1);
592 if (pdata->int_reset_gpio_lo) {
593 ret = gpio_request(pdata->int_reset_gpio_lo, "htcpld-core");
594 if (ret) {
596 * If it failed, that sucks, but we can probably
597 * continue on without it.
599 dev_warn(dev, "Unable to request int_reset_gpio_lo -- interrupts may not work\n");
600 htcpld->int_reset_gpio_lo = 0;
601 } else {
602 htcpld->int_reset_gpio_lo = pdata->int_reset_gpio_lo;
603 gpio_set_value(htcpld->int_reset_gpio_lo, 0);
607 dev_info(dev, "Initialized successfully\n");
608 return 0;
611 /* The I2C Driver -- used internally */
612 static const struct i2c_device_id htcpld_chip_id[] = {
613 { "htcpld-chip", 0 },
617 static struct i2c_driver htcpld_chip_driver = {
618 .driver = {
619 .name = "htcpld-chip",
621 .id_table = htcpld_chip_id,
624 /* The Core Driver */
625 static struct platform_driver htcpld_core_driver = {
626 .driver = {
627 .name = "i2c-htcpld",
631 static int __init htcpld_core_init(void)
633 int ret;
635 /* Register the I2C Chip driver */
636 ret = i2c_add_driver(&htcpld_chip_driver);
637 if (ret)
638 return ret;
640 /* Probe for our chips */
641 return platform_driver_probe(&htcpld_core_driver, htcpld_core_probe);
643 device_initcall(htcpld_core_init);