i2c-cht-wc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /*
  2. * Intel CHT Whiskey Cove PMIC I2C Master driver
  3. * Copyright (C) 2017 Hans de Goede <hdegoede@redhat.com>
  4. *
  5. * Based on various non upstream patches to support the CHT Whiskey Cove PMIC:
  6. * Copyright (C) 2011 - 2014 Intel Corporation. All rights reserved.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License version
  10. * 2 as published by the Free Software Foundation, or (at your option)
  11. * any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. */
  18. #include <linux/acpi.h>
  19. #include <linux/completion.h>
  20. #include <linux/delay.h>
  21. #include <linux/i2c.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/irq.h>
  24. #include <linux/irqdomain.h>
  25. #include <linux/mfd/intel_soc_pmic.h>
  26. #include <linux/module.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/power/bq24190_charger.h>
  29. #include <linux/slab.h>
  30. #define CHT_WC_I2C_CTRL 0x5e24
  31. #define CHT_WC_I2C_CTRL_WR BIT(0)
  32. #define CHT_WC_I2C_CTRL_RD BIT(1)
  33. #define CHT_WC_I2C_CLIENT_ADDR 0x5e25
  34. #define CHT_WC_I2C_REG_OFFSET 0x5e26
  35. #define CHT_WC_I2C_WRDATA 0x5e27
  36. #define CHT_WC_I2C_RDDATA 0x5e28
  37. #define CHT_WC_EXTCHGRIRQ 0x6e0a
  38. #define CHT_WC_EXTCHGRIRQ_CLIENT_IRQ BIT(0)
  39. #define CHT_WC_EXTCHGRIRQ_WRITE_IRQ BIT(1)
  40. #define CHT_WC_EXTCHGRIRQ_READ_IRQ BIT(2)
  41. #define CHT_WC_EXTCHGRIRQ_NACK_IRQ BIT(3)
  42. #define CHT_WC_EXTCHGRIRQ_ADAP_IRQMASK ((u8)GENMASK(3, 1))
  43. #define CHT_WC_EXTCHGRIRQ_MSK 0x6e17
  44. struct cht_wc_i2c_adap {
  45. struct i2c_adapter adapter;
  46. wait_queue_head_t wait;
  47. struct irq_chip irqchip;
  48. struct mutex adap_lock;
  49. struct mutex irqchip_lock;
  50. struct regmap *regmap;
  51. struct irq_domain *irq_domain;
  52. struct i2c_client *client;
  53. int client_irq;
  54. u8 irq_mask;
  55. u8 old_irq_mask;
  56. int read_data;
  57. bool io_error;
  58. bool done;
  59. };
  60. static irqreturn_t cht_wc_i2c_adap_thread_handler(int id, void *data)
  61. {
  62. struct cht_wc_i2c_adap *adap = data;
  63. int ret, reg;
  64. mutex_lock(&adap->adap_lock);
  65. /* Read IRQs */
  66. ret = regmap_read(adap->regmap, CHT_WC_EXTCHGRIRQ, &reg);
  67. if (ret) {
  68. dev_err(&adap->adapter.dev, "Error reading extchgrirq reg\n");
  69. mutex_unlock(&adap->adap_lock);
  70. return IRQ_NONE;
  71. }
  72. reg &= ~adap->irq_mask;
  73. /* Reads must be acked after reading the received data. */
  74. ret = regmap_read(adap->regmap, CHT_WC_I2C_RDDATA, &adap->read_data);
  75. if (ret)
  76. adap->io_error = true;
  77. /*
  78. * Immediately ack IRQs, so that if new IRQs arrives while we're
  79. * handling the previous ones our irq will re-trigger when we're done.
  80. */
  81. ret = regmap_write(adap->regmap, CHT_WC_EXTCHGRIRQ, reg);
  82. if (ret)
  83. dev_err(&adap->adapter.dev, "Error writing extchgrirq reg\n");
  84. if (reg & CHT_WC_EXTCHGRIRQ_ADAP_IRQMASK) {
  85. adap->io_error |= !!(reg & CHT_WC_EXTCHGRIRQ_NACK_IRQ);
  86. adap->done = true;
  87. }
  88. mutex_unlock(&adap->adap_lock);
  89. if (reg & CHT_WC_EXTCHGRIRQ_ADAP_IRQMASK)
  90. wake_up(&adap->wait);
  91. /*
  92. * Do NOT use handle_nested_irq here, the client irq handler will
  93. * likely want to do i2c transfers and the i2c controller uses this
  94. * interrupt handler as well, so running the client irq handler from
  95. * this thread will cause things to lock up.
  96. */
  97. if (reg & CHT_WC_EXTCHGRIRQ_CLIENT_IRQ) {
  98. /*
  99. * generic_handle_irq expects local IRQs to be disabled
  100. * as normally it is called from interrupt context.
  101. */
  102. local_irq_disable();
  103. generic_handle_irq(adap->client_irq);
  104. local_irq_enable();
  105. }
  106. return IRQ_HANDLED;
  107. }
  108. static u32 cht_wc_i2c_adap_master_func(struct i2c_adapter *adap)
  109. {
  110. /* This i2c adapter only supports SMBUS byte transfers */
  111. return I2C_FUNC_SMBUS_BYTE_DATA;
  112. }
  113. static int cht_wc_i2c_adap_smbus_xfer(struct i2c_adapter *_adap, u16 addr,
  114. unsigned short flags, char read_write,
  115. u8 command, int size,
  116. union i2c_smbus_data *data)
  117. {
  118. struct cht_wc_i2c_adap *adap = i2c_get_adapdata(_adap);
  119. int ret;
  120. mutex_lock(&adap->adap_lock);
  121. adap->io_error = false;
  122. adap->done = false;
  123. mutex_unlock(&adap->adap_lock);
  124. ret = regmap_write(adap->regmap, CHT_WC_I2C_CLIENT_ADDR, addr);
  125. if (ret)
  126. return ret;
  127. if (read_write == I2C_SMBUS_WRITE) {
  128. ret = regmap_write(adap->regmap, CHT_WC_I2C_WRDATA, data->byte);
  129. if (ret)
  130. return ret;
  131. }
  132. ret = regmap_write(adap->regmap, CHT_WC_I2C_REG_OFFSET, command);
  133. if (ret)
  134. return ret;
  135. ret = regmap_write(adap->regmap, CHT_WC_I2C_CTRL,
  136. (read_write == I2C_SMBUS_WRITE) ?
  137. CHT_WC_I2C_CTRL_WR : CHT_WC_I2C_CTRL_RD);
  138. if (ret)
  139. return ret;
  140. ret = wait_event_timeout(adap->wait, adap->done, msecs_to_jiffies(30));
  141. if (ret == 0) {
  142. /*
  143. * The CHT GPIO controller serializes all IRQs, sometimes
  144. * causing significant delays, check status manually.
  145. */
  146. cht_wc_i2c_adap_thread_handler(0, adap);
  147. if (!adap->done)
  148. return -ETIMEDOUT;
  149. }
  150. ret = 0;
  151. mutex_lock(&adap->adap_lock);
  152. if (adap->io_error)
  153. ret = -EIO;
  154. else if (read_write == I2C_SMBUS_READ)
  155. data->byte = adap->read_data;
  156. mutex_unlock(&adap->adap_lock);
  157. return ret;
  158. }
  159. static const struct i2c_algorithm cht_wc_i2c_adap_algo = {
  160. .functionality = cht_wc_i2c_adap_master_func,
  161. .smbus_xfer = cht_wc_i2c_adap_smbus_xfer,
  162. };
  163. /**** irqchip for the client connected to the extchgr i2c adapter ****/
  164. static void cht_wc_i2c_irq_lock(struct irq_data *data)
  165. {
  166. struct cht_wc_i2c_adap *adap = irq_data_get_irq_chip_data(data);
  167. mutex_lock(&adap->irqchip_lock);
  168. }
  169. static void cht_wc_i2c_irq_sync_unlock(struct irq_data *data)
  170. {
  171. struct cht_wc_i2c_adap *adap = irq_data_get_irq_chip_data(data);
  172. int ret;
  173. if (adap->irq_mask != adap->old_irq_mask) {
  174. ret = regmap_write(adap->regmap, CHT_WC_EXTCHGRIRQ_MSK,
  175. adap->irq_mask);
  176. if (ret == 0)
  177. adap->old_irq_mask = adap->irq_mask;
  178. else
  179. dev_err(&adap->adapter.dev, "Error writing EXTCHGRIRQ_MSK\n");
  180. }
  181. mutex_unlock(&adap->irqchip_lock);
  182. }
  183. static void cht_wc_i2c_irq_enable(struct irq_data *data)
  184. {
  185. struct cht_wc_i2c_adap *adap = irq_data_get_irq_chip_data(data);
  186. adap->irq_mask &= ~CHT_WC_EXTCHGRIRQ_CLIENT_IRQ;
  187. }
  188. static void cht_wc_i2c_irq_disable(struct irq_data *data)
  189. {
  190. struct cht_wc_i2c_adap *adap = irq_data_get_irq_chip_data(data);
  191. adap->irq_mask |= CHT_WC_EXTCHGRIRQ_CLIENT_IRQ;
  192. }
  193. static const struct irq_chip cht_wc_i2c_irq_chip = {
  194. .irq_bus_lock = cht_wc_i2c_irq_lock,
  195. .irq_bus_sync_unlock = cht_wc_i2c_irq_sync_unlock,
  196. .irq_disable = cht_wc_i2c_irq_disable,
  197. .irq_enable = cht_wc_i2c_irq_enable,
  198. .name = "cht_wc_ext_chrg_irq_chip",
  199. };
  200. static const char * const bq24190_suppliers[] = {
  201. "tcpm-source-psy-i2c-fusb302" };
  202. static const struct property_entry bq24190_props[] = {
  203. PROPERTY_ENTRY_STRING_ARRAY("supplied-from", bq24190_suppliers),
  204. PROPERTY_ENTRY_BOOL("omit-battery-class"),
  205. PROPERTY_ENTRY_BOOL("disable-reset"),
  206. { }
  207. };
  208. static struct regulator_consumer_supply fusb302_consumer = {
  209. .supply = "vbus",
  210. /* Must match fusb302 dev_name in intel_cht_int33fe.c */
  211. .dev_name = "i2c-fusb302",
  212. };
  213. static const struct regulator_init_data bq24190_vbus_init_data = {
  214. .constraints = {
  215. /* The name is used in intel_cht_int33fe.c do not change. */
  216. .name = "cht_wc_usb_typec_vbus",
  217. .valid_ops_mask = REGULATOR_CHANGE_STATUS,
  218. },
  219. .consumer_supplies = &fusb302_consumer,
  220. .num_consumer_supplies = 1,
  221. };
  222. static struct bq24190_platform_data bq24190_pdata = {
  223. .regulator_init_data = &bq24190_vbus_init_data,
  224. };
  225. static int cht_wc_i2c_adap_i2c_probe(struct platform_device *pdev)
  226. {
  227. struct intel_soc_pmic *pmic = dev_get_drvdata(pdev->dev.parent);
  228. struct cht_wc_i2c_adap *adap;
  229. struct i2c_board_info board_info = {
  230. .type = "bq24190",
  231. .addr = 0x6b,
  232. .dev_name = "bq24190",
  233. .properties = bq24190_props,
  234. .platform_data = &bq24190_pdata,
  235. };
  236. int ret, reg, irq;
  237. irq = platform_get_irq(pdev, 0);
  238. if (irq < 0) {
  239. dev_err(&pdev->dev, "Error missing irq resource\n");
  240. return -EINVAL;
  241. }
  242. adap = devm_kzalloc(&pdev->dev, sizeof(*adap), GFP_KERNEL);
  243. if (!adap)
  244. return -ENOMEM;
  245. init_waitqueue_head(&adap->wait);
  246. mutex_init(&adap->adap_lock);
  247. mutex_init(&adap->irqchip_lock);
  248. adap->irqchip = cht_wc_i2c_irq_chip;
  249. adap->regmap = pmic->regmap;
  250. adap->adapter.owner = THIS_MODULE;
  251. adap->adapter.class = I2C_CLASS_HWMON;
  252. adap->adapter.algo = &cht_wc_i2c_adap_algo;
  253. strlcpy(adap->adapter.name, "PMIC I2C Adapter",
  254. sizeof(adap->adapter.name));
  255. adap->adapter.dev.parent = &pdev->dev;
  256. /* Clear and activate i2c-adapter interrupts, disable client IRQ */
  257. adap->old_irq_mask = adap->irq_mask = ~CHT_WC_EXTCHGRIRQ_ADAP_IRQMASK;
  258. ret = regmap_read(adap->regmap, CHT_WC_I2C_RDDATA, &reg);
  259. if (ret)
  260. return ret;
  261. ret = regmap_write(adap->regmap, CHT_WC_EXTCHGRIRQ, ~adap->irq_mask);
  262. if (ret)
  263. return ret;
  264. ret = regmap_write(adap->regmap, CHT_WC_EXTCHGRIRQ_MSK, adap->irq_mask);
  265. if (ret)
  266. return ret;
  267. /* Alloc and register client IRQ */
  268. adap->irq_domain = irq_domain_add_linear(pdev->dev.of_node, 1,
  269. &irq_domain_simple_ops, NULL);
  270. if (!adap->irq_domain)
  271. return -ENOMEM;
  272. adap->client_irq = irq_create_mapping(adap->irq_domain, 0);
  273. if (!adap->client_irq) {
  274. ret = -ENOMEM;
  275. goto remove_irq_domain;
  276. }
  277. irq_set_chip_data(adap->client_irq, adap);
  278. irq_set_chip_and_handler(adap->client_irq, &adap->irqchip,
  279. handle_simple_irq);
  280. ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
  281. cht_wc_i2c_adap_thread_handler,
  282. IRQF_ONESHOT, "PMIC I2C Adapter", adap);
  283. if (ret)
  284. goto remove_irq_domain;
  285. i2c_set_adapdata(&adap->adapter, adap);
  286. ret = i2c_add_adapter(&adap->adapter);
  287. if (ret)
  288. goto remove_irq_domain;
  289. /*
  290. * Normally the Whiskey Cove PMIC is paired with a TI bq24292i charger,
  291. * connected to this i2c bus, and a max17047 fuel-gauge and a fusb302
  292. * USB Type-C controller connected to another i2c bus. In this setup
  293. * the max17047 and fusb302 devices are enumerated through an INT33FE
  294. * ACPI device. If this device is present register an i2c-client for
  295. * the TI bq24292i charger.
  296. */
  297. if (acpi_dev_present("INT33FE", NULL, -1)) {
  298. board_info.irq = adap->client_irq;
  299. adap->client = i2c_new_device(&adap->adapter, &board_info);
  300. if (!adap->client) {
  301. ret = -ENOMEM;
  302. goto del_adapter;
  303. }
  304. }
  305. platform_set_drvdata(pdev, adap);
  306. return 0;
  307. del_adapter:
  308. i2c_del_adapter(&adap->adapter);
  309. remove_irq_domain:
  310. irq_domain_remove(adap->irq_domain);
  311. return ret;
  312. }
  313. static int cht_wc_i2c_adap_i2c_remove(struct platform_device *pdev)
  314. {
  315. struct cht_wc_i2c_adap *adap = platform_get_drvdata(pdev);
  316. if (adap->client)
  317. i2c_unregister_device(adap->client);
  318. i2c_del_adapter(&adap->adapter);
  319. irq_domain_remove(adap->irq_domain);
  320. return 0;
  321. }
  322. static const struct platform_device_id cht_wc_i2c_adap_id_table[] = {
  323. { .name = "cht_wcove_ext_chgr" },
  324. {},
  325. };
  326. MODULE_DEVICE_TABLE(platform, cht_wc_i2c_adap_id_table);
  327. static struct platform_driver cht_wc_i2c_adap_driver = {
  328. .probe = cht_wc_i2c_adap_i2c_probe,
  329. .remove = cht_wc_i2c_adap_i2c_remove,
  330. .driver = {
  331. .name = "cht_wcove_ext_chgr",
  332. },
  333. .id_table = cht_wc_i2c_adap_id_table,
  334. };
  335. module_platform_driver(cht_wc_i2c_adap_driver);
  336. MODULE_DESCRIPTION("Intel CHT Whiskey Cove PMIC I2C Master driver");
  337. MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
  338. MODULE_LICENSE("GPL");