htc-i2cpld.c 16 KB

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