htc-i2cpld.c 16 KB

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