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