blob: 3f9eee5f8fb9b60035252aea68d73f122d2d3437 [file] [log] [blame]
Cory Maccarrone6048a3d2010-01-19 11:22:45 +01001/*
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/slab.h>
Cory Maccarrone6048a3d2010-01-19 11:22:45 +010039
40struct 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
Thomas Gleixner9eaee992011-03-25 11:12:29 +000061 unsigned int flow_type;
Cory Maccarrone6048a3d2010-01-19 11:22:45 +010062 /*
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
69struct 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. */
Mark Browne6a4c4a2010-12-11 16:44:11 +000086static void htcpld_mask(struct irq_data *data)
Cory Maccarrone6048a3d2010-01-19 11:22:45 +010087{
Mark Browne6a4c4a2010-12-11 16:44:11 +000088 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);
Cory Maccarrone6048a3d2010-01-19 11:22:45 +010091}
Mark Browne6a4c4a2010-12-11 16:44:11 +000092static void htcpld_unmask(struct irq_data *data)
Cory Maccarrone6048a3d2010-01-19 11:22:45 +010093{
Mark Browne6a4c4a2010-12-11 16:44:11 +000094 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);
Cory Maccarrone6048a3d2010-01-19 11:22:45 +010097}
98
Mark Browne6a4c4a2010-12-11 16:44:11 +000099static int htcpld_set_type(struct irq_data *data, unsigned int flags)
Cory Maccarrone6048a3d2010-01-19 11:22:45 +0100100{
Thomas Gleixner9eaee992011-03-25 11:12:29 +0000101 struct htcpld_chip *chip = irq_data_get_irq_chip_data(data);
Cory Maccarrone6048a3d2010-01-19 11:22:45 +0100102
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
Thomas Gleixner9eaee992011-03-25 11:12:29 +0000110 chip->flow_type = flags;
Cory Maccarrone6048a3d2010-01-19 11:22:45 +0100111 return 0;
112}
113
114static struct irq_chip htcpld_muxed_chip = {
Mark Browne6a4c4a2010-12-11 16:44:11 +0000115 .name = "htcpld",
116 .irq_mask = htcpld_mask,
117 .irq_unmask = htcpld_unmask,
118 .irq_set_type = htcpld_set_type,
Cory Maccarrone6048a3d2010-01-19 11:22:45 +0100119};
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 */
126static 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;
Cory Maccarrone6048a3d2010-01-19 11:22:45 +0100132
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++) {
Thomas Gleixner9eaee992011-03-25 11:12:29 +0000191 unsigned oldb, newb, type = chip->flow_type;
Cory Maccarrone6048a3d2010-01-19 11:22:45 +0100192
193 irq = chip->irq_start + irqpin;
Cory Maccarrone6048a3d2010-01-19 11:22:45 +0100194
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
Thomas Gleixner9eaee992011-03-25 11:12:29 +0000200 if ((!oldb && newb && (type & IRQ_TYPE_EDGE_RISING)) ||
201 (oldb && !newb && (type & IRQ_TYPE_EDGE_FALLING))) {
Cory Maccarrone6048a3d2010-01-19 11:22:45 +0100202 pr_debug("fire IRQ %d\n", irqpin);
Thomas Gleixner9eaee992011-03-25 11:12:29 +0000203 generic_handle_irq(irq);
Cory Maccarrone6048a3d2010-01-19 11:22:45 +0100204 }
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 */
Mark Brown8d2d3a32010-12-11 16:44:49 +0000227static void htcpld_chip_set(struct gpio_chip *chip, unsigned offset, int val)
Cory Maccarrone6048a3d2010-01-19 11:22:45 +0100228{
229 struct i2c_client *client;
Linus Walleijed1c9b32016-03-30 10:48:04 +0200230 struct htcpld_chip *chip_data = gpiochip_get_data(chip);
Cory Maccarrone6048a3d2010-01-19 11:22:45 +0100231 unsigned long flags;
232
Cory Maccarrone6048a3d2010-01-19 11:22:45 +0100233 client = chip_data->client;
Lee Jones41cc08e2014-08-13 13:52:27 +0100234 if (!client)
Cory Maccarrone6048a3d2010-01-19 11:22:45 +0100235 return;
236
237 spin_lock_irqsave(&chip_data->lock, flags);
238 if (val)
239 chip_data->cache_out |= (1 << offset);
240 else
241 chip_data->cache_out &= ~(1 << offset);
242 spin_unlock_irqrestore(&chip_data->lock, flags);
243
244 schedule_work(&(chip_data->set_val_work));
245}
246
Mark Brown8d2d3a32010-12-11 16:44:49 +0000247static void htcpld_chip_set_ni(struct work_struct *work)
Cory Maccarrone6048a3d2010-01-19 11:22:45 +0100248{
249 struct htcpld_chip *chip_data;
250 struct i2c_client *client;
251
252 chip_data = container_of(work, struct htcpld_chip, set_val_work);
253 client = chip_data->client;
254 i2c_smbus_read_byte_data(client, chip_data->cache_out);
255}
256
Mark Brown8d2d3a32010-12-11 16:44:49 +0000257static int htcpld_chip_get(struct gpio_chip *chip, unsigned offset)
Cory Maccarrone6048a3d2010-01-19 11:22:45 +0100258{
Linus Walleijed1c9b32016-03-30 10:48:04 +0200259 struct htcpld_chip *chip_data = gpiochip_get_data(chip);
Lee Jones9b6a5ad2014-08-18 13:10:20 +0100260 u8 cache;
Cory Maccarrone6048a3d2010-01-19 11:22:45 +0100261
Lee Jones9b6a5ad2014-08-18 13:10:20 +0100262 if (!strncmp(chip->label, "htcpld-out", 10)) {
Lee Jones9b6a5ad2014-08-18 13:10:20 +0100263 cache = chip_data->cache_out;
264 } else if (!strncmp(chip->label, "htcpld-in", 9)) {
Lee Jones9b6a5ad2014-08-18 13:10:20 +0100265 cache = chip_data->cache_in;
266 } else
267 return -EINVAL;
Cory Maccarrone6048a3d2010-01-19 11:22:45 +0100268
Lee Jones9b6a5ad2014-08-18 13:10:20 +0100269 return (cache >> offset) & 1;
Cory Maccarrone6048a3d2010-01-19 11:22:45 +0100270}
271
272static int htcpld_direction_output(struct gpio_chip *chip,
273 unsigned offset, int value)
274{
275 htcpld_chip_set(chip, offset, value);
276 return 0;
277}
278
279static int htcpld_direction_input(struct gpio_chip *chip,
280 unsigned offset)
281{
282 /*
283 * No-op: this function can only be called on the input chip.
284 * We do however make sure the offset is within range.
285 */
286 return (offset < chip->ngpio) ? 0 : -EINVAL;
287}
288
Mark Brown8d2d3a32010-12-11 16:44:49 +0000289static int htcpld_chip_to_irq(struct gpio_chip *chip, unsigned offset)
Cory Maccarrone6048a3d2010-01-19 11:22:45 +0100290{
Linus Walleijed1c9b32016-03-30 10:48:04 +0200291 struct htcpld_chip *chip_data = gpiochip_get_data(chip);
Cory Maccarrone6048a3d2010-01-19 11:22:45 +0100292
293 if (offset < chip_data->nirqs)
294 return chip_data->irq_start + offset;
295 else
296 return -EINVAL;
297}
298
Mark Brown8d2d3a32010-12-11 16:44:49 +0000299static void htcpld_chip_reset(struct i2c_client *client)
Cory Maccarrone6048a3d2010-01-19 11:22:45 +0100300{
301 struct htcpld_chip *chip_data = i2c_get_clientdata(client);
302 if (!chip_data)
303 return;
304
305 i2c_smbus_read_byte_data(
306 client, (chip_data->cache_out = chip_data->reset));
307}
308
Bill Pembertonf791be42012-11-19 13:23:04 -0500309static int htcpld_setup_chip_irq(
Cory Maccarrone6048a3d2010-01-19 11:22:45 +0100310 struct platform_device *pdev,
311 int chip_index)
312{
313 struct htcpld_data *htcpld;
Cory Maccarrone6048a3d2010-01-19 11:22:45 +0100314 struct htcpld_chip *chip;
Cory Maccarrone6048a3d2010-01-19 11:22:45 +0100315 unsigned int irq, irq_end;
Cory Maccarrone6048a3d2010-01-19 11:22:45 +0100316
317 /* Get the platform and driver data */
Cory Maccarrone6048a3d2010-01-19 11:22:45 +0100318 htcpld = platform_get_drvdata(pdev);
319 chip = &htcpld->chip[chip_index];
Cory Maccarrone6048a3d2010-01-19 11:22:45 +0100320
321 /* Setup irq handlers */
322 irq_end = chip->irq_start + chip->nirqs;
323 for (irq = chip->irq_start; irq < irq_end; irq++) {
Thomas Gleixnerd6f7ce9f2011-03-25 11:12:35 +0000324 irq_set_chip_and_handler(irq, &htcpld_muxed_chip,
325 handle_simple_irq);
Thomas Gleixnerd5bb1222011-03-25 11:12:32 +0000326 irq_set_chip_data(irq, chip);
Rob Herring9bd09f32015-07-27 15:55:20 -0500327 irq_clear_status_flags(irq, IRQ_NOREQUEST | IRQ_NOPROBE);
Cory Maccarrone6048a3d2010-01-19 11:22:45 +0100328 }
329
Javier Martinez Canillasa48baac2015-09-29 13:26:01 +0200330 return 0;
Cory Maccarrone6048a3d2010-01-19 11:22:45 +0100331}
332
Bill Pembertonf791be42012-11-19 13:23:04 -0500333static int htcpld_register_chip_i2c(
Cory Maccarrone6048a3d2010-01-19 11:22:45 +0100334 struct platform_device *pdev,
335 int chip_index)
336{
337 struct htcpld_data *htcpld;
338 struct device *dev = &pdev->dev;
339 struct htcpld_core_platform_data *pdata;
340 struct htcpld_chip *chip;
341 struct htcpld_chip_platform_data *plat_chip_data;
342 struct i2c_adapter *adapter;
343 struct i2c_client *client;
344 struct i2c_board_info info;
345
346 /* Get the platform and driver data */
Jingoo Han334a41ce2013-07-30 17:10:05 +0900347 pdata = dev_get_platdata(dev);
Cory Maccarrone6048a3d2010-01-19 11:22:45 +0100348 htcpld = platform_get_drvdata(pdev);
349 chip = &htcpld->chip[chip_index];
350 plat_chip_data = &pdata->chip[chip_index];
351
352 adapter = i2c_get_adapter(pdata->i2c_adapter_id);
Lee Jones41cc08e2014-08-13 13:52:27 +0100353 if (!adapter) {
Cory Maccarrone6048a3d2010-01-19 11:22:45 +0100354 /* Eek, no such I2C adapter! Bail out. */
355 dev_warn(dev, "Chip at i2c address 0x%x: Invalid i2c adapter %d\n",
356 plat_chip_data->addr, pdata->i2c_adapter_id);
357 return -ENODEV;
358 }
359
360 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_READ_BYTE_DATA)) {
361 dev_warn(dev, "i2c adapter %d non-functional\n",
362 pdata->i2c_adapter_id);
363 return -EINVAL;
364 }
365
366 memset(&info, 0, sizeof(struct i2c_board_info));
367 info.addr = plat_chip_data->addr;
368 strlcpy(info.type, "htcpld-chip", I2C_NAME_SIZE);
369 info.platform_data = chip;
370
371 /* Add the I2C device. This calls the probe() function. */
372 client = i2c_new_device(adapter, &info);
373 if (!client) {
374 /* I2C device registration failed, contineu with the next */
375 dev_warn(dev, "Unable to add I2C device for 0x%x\n",
376 plat_chip_data->addr);
377 return -ENODEV;
378 }
379
380 i2c_set_clientdata(client, chip);
Hans Wennborg6065c9a2014-08-03 17:19:15 -0700381 snprintf(client->name, I2C_NAME_SIZE, "Chip_0x%x", client->addr);
Cory Maccarrone6048a3d2010-01-19 11:22:45 +0100382 chip->client = client;
383
384 /* Reset the chip */
385 htcpld_chip_reset(client);
386 chip->cache_in = i2c_smbus_read_byte_data(client, chip->cache_out);
387
388 return 0;
389}
390
Bill Pembertonf791be42012-11-19 13:23:04 -0500391static void htcpld_unregister_chip_i2c(
Cory Maccarrone6048a3d2010-01-19 11:22:45 +0100392 struct platform_device *pdev,
393 int chip_index)
394{
395 struct htcpld_data *htcpld;
396 struct htcpld_chip *chip;
397
398 /* Get the platform and driver data */
399 htcpld = platform_get_drvdata(pdev);
400 chip = &htcpld->chip[chip_index];
401
402 if (chip->client)
403 i2c_unregister_device(chip->client);
404}
405
Bill Pembertonf791be42012-11-19 13:23:04 -0500406static int htcpld_register_chip_gpio(
Cory Maccarrone6048a3d2010-01-19 11:22:45 +0100407 struct platform_device *pdev,
408 int chip_index)
409{
410 struct htcpld_data *htcpld;
411 struct device *dev = &pdev->dev;
412 struct htcpld_core_platform_data *pdata;
413 struct htcpld_chip *chip;
414 struct htcpld_chip_platform_data *plat_chip_data;
415 struct gpio_chip *gpio_chip;
416 int ret = 0;
417
418 /* Get the platform and driver data */
Jingoo Han334a41ce2013-07-30 17:10:05 +0900419 pdata = dev_get_platdata(dev);
Cory Maccarrone6048a3d2010-01-19 11:22:45 +0100420 htcpld = platform_get_drvdata(pdev);
421 chip = &htcpld->chip[chip_index];
422 plat_chip_data = &pdata->chip[chip_index];
423
424 /* Setup the GPIO chips */
425 gpio_chip = &(chip->chip_out);
426 gpio_chip->label = "htcpld-out";
Linus Walleij58383c782015-11-04 09:56:26 +0100427 gpio_chip->parent = dev;
Cory Maccarrone6048a3d2010-01-19 11:22:45 +0100428 gpio_chip->owner = THIS_MODULE;
429 gpio_chip->get = htcpld_chip_get;
430 gpio_chip->set = htcpld_chip_set;
431 gpio_chip->direction_input = NULL;
432 gpio_chip->direction_output = htcpld_direction_output;
433 gpio_chip->base = plat_chip_data->gpio_out_base;
434 gpio_chip->ngpio = plat_chip_data->num_gpios;
435
436 gpio_chip = &(chip->chip_in);
437 gpio_chip->label = "htcpld-in";
Linus Walleij58383c782015-11-04 09:56:26 +0100438 gpio_chip->parent = dev;
Cory Maccarrone6048a3d2010-01-19 11:22:45 +0100439 gpio_chip->owner = THIS_MODULE;
440 gpio_chip->get = htcpld_chip_get;
441 gpio_chip->set = NULL;
442 gpio_chip->direction_input = htcpld_direction_input;
443 gpio_chip->direction_output = NULL;
444 gpio_chip->to_irq = htcpld_chip_to_irq;
445 gpio_chip->base = plat_chip_data->gpio_in_base;
446 gpio_chip->ngpio = plat_chip_data->num_gpios;
447
448 /* Add the GPIO chips */
Linus Walleijed1c9b32016-03-30 10:48:04 +0200449 ret = gpiochip_add_data(&(chip->chip_out), chip);
Cory Maccarrone6048a3d2010-01-19 11:22:45 +0100450 if (ret) {
451 dev_warn(dev, "Unable to register output GPIOs for 0x%x: %d\n",
452 plat_chip_data->addr, ret);
453 return ret;
454 }
455
Linus Walleijed1c9b32016-03-30 10:48:04 +0200456 ret = gpiochip_add_data(&(chip->chip_in), chip);
Cory Maccarrone6048a3d2010-01-19 11:22:45 +0100457 if (ret) {
Cory Maccarrone6048a3d2010-01-19 11:22:45 +0100458 dev_warn(dev, "Unable to register input GPIOs for 0x%x: %d\n",
459 plat_chip_data->addr, ret);
abdoulaye berthe88d5e522014-07-12 22:30:14 +0200460 gpiochip_remove(&(chip->chip_out));
Cory Maccarrone6048a3d2010-01-19 11:22:45 +0100461 return ret;
462 }
463
464 return 0;
465}
466
Bill Pembertonf791be42012-11-19 13:23:04 -0500467static int htcpld_setup_chips(struct platform_device *pdev)
Cory Maccarrone6048a3d2010-01-19 11:22:45 +0100468{
469 struct htcpld_data *htcpld;
470 struct device *dev = &pdev->dev;
471 struct htcpld_core_platform_data *pdata;
472 int i;
473
474 /* Get the platform and driver data */
Jingoo Han334a41ce2013-07-30 17:10:05 +0900475 pdata = dev_get_platdata(dev);
Cory Maccarrone6048a3d2010-01-19 11:22:45 +0100476 htcpld = platform_get_drvdata(pdev);
477
478 /* Setup each chip's output GPIOs */
479 htcpld->nchips = pdata->num_chip;
Lee Jones2b0b5e22013-05-23 16:25:13 +0100480 htcpld->chip = devm_kzalloc(dev, sizeof(struct htcpld_chip) * htcpld->nchips,
481 GFP_KERNEL);
Cory Maccarrone6048a3d2010-01-19 11:22:45 +0100482 if (!htcpld->chip) {
483 dev_warn(dev, "Unable to allocate memory for chips\n");
484 return -ENOMEM;
485 }
486
487 /* Add the chips as best we can */
488 for (i = 0; i < htcpld->nchips; i++) {
489 int ret;
490
491 /* Setup the HTCPLD chips */
492 htcpld->chip[i].reset = pdata->chip[i].reset;
493 htcpld->chip[i].cache_out = pdata->chip[i].reset;
494 htcpld->chip[i].cache_in = 0;
495 htcpld->chip[i].dev = dev;
496 htcpld->chip[i].irq_start = pdata->chip[i].irq_base;
497 htcpld->chip[i].nirqs = pdata->chip[i].num_irqs;
498
499 INIT_WORK(&(htcpld->chip[i].set_val_work), &htcpld_chip_set_ni);
500 spin_lock_init(&(htcpld->chip[i].lock));
501
502 /* Setup the interrupts for the chip */
503 if (htcpld->chained_irq) {
504 ret = htcpld_setup_chip_irq(pdev, i);
505 if (ret)
506 continue;
507 }
508
509 /* Register the chip with I2C */
510 ret = htcpld_register_chip_i2c(pdev, i);
511 if (ret)
512 continue;
513
514
515 /* Register the chips with the GPIO subsystem */
516 ret = htcpld_register_chip_gpio(pdev, i);
517 if (ret) {
518 /* Unregister the chip from i2c and continue */
519 htcpld_unregister_chip_i2c(pdev, i);
520 continue;
521 }
522
523 dev_info(dev, "Registered chip at 0x%x\n", pdata->chip[i].addr);
524 }
525
526 return 0;
527}
528
Bill Pembertonf791be42012-11-19 13:23:04 -0500529static int htcpld_core_probe(struct platform_device *pdev)
Cory Maccarrone6048a3d2010-01-19 11:22:45 +0100530{
531 struct htcpld_data *htcpld;
532 struct device *dev = &pdev->dev;
533 struct htcpld_core_platform_data *pdata;
534 struct resource *res;
535 int ret = 0;
536
537 if (!dev)
538 return -ENODEV;
539
Jingoo Han334a41ce2013-07-30 17:10:05 +0900540 pdata = dev_get_platdata(dev);
Cory Maccarrone6048a3d2010-01-19 11:22:45 +0100541 if (!pdata) {
542 dev_warn(dev, "Platform data not found for htcpld core!\n");
543 return -ENXIO;
544 }
545
Lee Jones2b0b5e22013-05-23 16:25:13 +0100546 htcpld = devm_kzalloc(dev, sizeof(struct htcpld_data), GFP_KERNEL);
Cory Maccarrone6048a3d2010-01-19 11:22:45 +0100547 if (!htcpld)
548 return -ENOMEM;
549
550 /* Find chained irq */
Cory Maccarrone6048a3d2010-01-19 11:22:45 +0100551 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
552 if (res) {
553 int flags;
554 htcpld->chained_irq = res->start;
555
556 /* Setup the chained interrupt handler */
Fabio Estevamcea2cc72015-05-16 15:42:13 -0300557 flags = IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING |
558 IRQF_ONESHOT;
Cory Maccarrone6048a3d2010-01-19 11:22:45 +0100559 ret = request_threaded_irq(htcpld->chained_irq,
560 NULL, htcpld_handler,
561 flags, pdev->name, htcpld);
562 if (ret) {
563 dev_warn(dev, "Unable to setup chained irq handler: %d\n", ret);
Lee Jones2b0b5e22013-05-23 16:25:13 +0100564 return ret;
Cory Maccarrone6048a3d2010-01-19 11:22:45 +0100565 } else
566 device_init_wakeup(dev, 0);
567 }
568
569 /* Set the driver data */
570 platform_set_drvdata(pdev, htcpld);
571
572 /* Setup the htcpld chips */
573 ret = htcpld_setup_chips(pdev);
574 if (ret)
Lee Jones2b0b5e22013-05-23 16:25:13 +0100575 return ret;
Cory Maccarrone6048a3d2010-01-19 11:22:45 +0100576
577 /* Request the GPIO(s) for the int reset and set them up */
578 if (pdata->int_reset_gpio_hi) {
579 ret = gpio_request(pdata->int_reset_gpio_hi, "htcpld-core");
580 if (ret) {
581 /*
582 * If it failed, that sucks, but we can probably
583 * continue on without it.
584 */
585 dev_warn(dev, "Unable to request int_reset_gpio_hi -- interrupts may not work\n");
586 htcpld->int_reset_gpio_hi = 0;
587 } else {
588 htcpld->int_reset_gpio_hi = pdata->int_reset_gpio_hi;
589 gpio_set_value(htcpld->int_reset_gpio_hi, 1);
590 }
591 }
592
593 if (pdata->int_reset_gpio_lo) {
594 ret = gpio_request(pdata->int_reset_gpio_lo, "htcpld-core");
595 if (ret) {
596 /*
597 * If it failed, that sucks, but we can probably
598 * continue on without it.
599 */
600 dev_warn(dev, "Unable to request int_reset_gpio_lo -- interrupts may not work\n");
601 htcpld->int_reset_gpio_lo = 0;
602 } else {
603 htcpld->int_reset_gpio_lo = pdata->int_reset_gpio_lo;
604 gpio_set_value(htcpld->int_reset_gpio_lo, 0);
605 }
606 }
607
608 dev_info(dev, "Initialized successfully\n");
609 return 0;
Cory Maccarrone6048a3d2010-01-19 11:22:45 +0100610}
611
612/* The I2C Driver -- used internally */
613static const struct i2c_device_id htcpld_chip_id[] = {
614 { "htcpld-chip", 0 },
615 { }
616};
617MODULE_DEVICE_TABLE(i2c, htcpld_chip_id);
618
619
620static struct i2c_driver htcpld_chip_driver = {
621 .driver = {
622 .name = "htcpld-chip",
623 },
624 .id_table = htcpld_chip_id,
625};
626
627/* The Core Driver */
628static struct platform_driver htcpld_core_driver = {
629 .driver = {
630 .name = "i2c-htcpld",
631 },
632};
633
634static int __init htcpld_core_init(void)
635{
636 int ret;
637
638 /* Register the I2C Chip driver */
639 ret = i2c_add_driver(&htcpld_chip_driver);
640 if (ret)
641 return ret;
642
643 /* Probe for our chips */
644 return platform_driver_probe(&htcpld_core_driver, htcpld_core_probe);
645}
646
647static void __exit htcpld_core_exit(void)
648{
649 i2c_del_driver(&htcpld_chip_driver);
650 platform_driver_unregister(&htcpld_core_driver);
651}
652
653module_init(htcpld_core_init);
654module_exit(htcpld_core_exit);
655
656MODULE_AUTHOR("Cory Maccarrone <darkstar6262@gmail.com>");
657MODULE_DESCRIPTION("I2C HTC PLD Driver");
658MODULE_LICENSE("GPL");
659