i2c-cht-wc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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. /*
  164. * We are an i2c-adapter which itself is part of an i2c-client. This means that
  165. * transfers done through us take adapter->bus_lock twice, once for our parent
  166. * i2c-adapter and once to take our own bus_lock. Lockdep does not like this
  167. * nested locking, to make lockdep happy in the case of busses with muxes, the
  168. * i2c-core's i2c_adapter_lock_bus function calls:
  169. * rt_mutex_lock_nested(&adapter->bus_lock, i2c_adapter_depth(adapter));
  170. *
  171. * But i2c_adapter_depth only works when the direct parent of the adapter is
  172. * another adapter, as it is only meant for muxes. In our case there is an
  173. * i2c-client and MFD instantiated platform_device in the parent->child chain
  174. * between the 2 devices.
  175. *
  176. * So we override the default i2c_lock_operations and pass a hardcoded
  177. * depth of 1 to rt_mutex_lock_nested, to make lockdep happy.
  178. *
  179. * Note that if there were to be a mux attached to our adapter, this would
  180. * break things again since the i2c-mux code expects the root-adapter to have
  181. * a locking depth of 0. But we always have only 1 client directly attached
  182. * in the form of the Charger IC paired with the CHT Whiskey Cove PMIC.
  183. */
  184. static void cht_wc_i2c_adap_lock_bus(struct i2c_adapter *adapter,
  185. unsigned int flags)
  186. {
  187. rt_mutex_lock_nested(&adapter->bus_lock, 1);
  188. }
  189. static int cht_wc_i2c_adap_trylock_bus(struct i2c_adapter *adapter,
  190. unsigned int flags)
  191. {
  192. return rt_mutex_trylock(&adapter->bus_lock);
  193. }
  194. static void cht_wc_i2c_adap_unlock_bus(struct i2c_adapter *adapter,
  195. unsigned int flags)
  196. {
  197. rt_mutex_unlock(&adapter->bus_lock);
  198. }
  199. static const struct i2c_lock_operations cht_wc_i2c_adap_lock_ops = {
  200. .lock_bus = cht_wc_i2c_adap_lock_bus,
  201. .trylock_bus = cht_wc_i2c_adap_trylock_bus,
  202. .unlock_bus = cht_wc_i2c_adap_unlock_bus,
  203. };
  204. /**** irqchip for the client connected to the extchgr i2c adapter ****/
  205. static void cht_wc_i2c_irq_lock(struct irq_data *data)
  206. {
  207. struct cht_wc_i2c_adap *adap = irq_data_get_irq_chip_data(data);
  208. mutex_lock(&adap->irqchip_lock);
  209. }
  210. static void cht_wc_i2c_irq_sync_unlock(struct irq_data *data)
  211. {
  212. struct cht_wc_i2c_adap *adap = irq_data_get_irq_chip_data(data);
  213. int ret;
  214. if (adap->irq_mask != adap->old_irq_mask) {
  215. ret = regmap_write(adap->regmap, CHT_WC_EXTCHGRIRQ_MSK,
  216. adap->irq_mask);
  217. if (ret == 0)
  218. adap->old_irq_mask = adap->irq_mask;
  219. else
  220. dev_err(&adap->adapter.dev, "Error writing EXTCHGRIRQ_MSK\n");
  221. }
  222. mutex_unlock(&adap->irqchip_lock);
  223. }
  224. static void cht_wc_i2c_irq_enable(struct irq_data *data)
  225. {
  226. struct cht_wc_i2c_adap *adap = irq_data_get_irq_chip_data(data);
  227. adap->irq_mask &= ~CHT_WC_EXTCHGRIRQ_CLIENT_IRQ;
  228. }
  229. static void cht_wc_i2c_irq_disable(struct irq_data *data)
  230. {
  231. struct cht_wc_i2c_adap *adap = irq_data_get_irq_chip_data(data);
  232. adap->irq_mask |= CHT_WC_EXTCHGRIRQ_CLIENT_IRQ;
  233. }
  234. static const struct irq_chip cht_wc_i2c_irq_chip = {
  235. .irq_bus_lock = cht_wc_i2c_irq_lock,
  236. .irq_bus_sync_unlock = cht_wc_i2c_irq_sync_unlock,
  237. .irq_disable = cht_wc_i2c_irq_disable,
  238. .irq_enable = cht_wc_i2c_irq_enable,
  239. .name = "cht_wc_ext_chrg_irq_chip",
  240. };
  241. static const char * const bq24190_suppliers[] = {
  242. "tcpm-source-psy-i2c-fusb302" };
  243. static const struct property_entry bq24190_props[] = {
  244. PROPERTY_ENTRY_STRING_ARRAY("supplied-from", bq24190_suppliers),
  245. PROPERTY_ENTRY_BOOL("omit-battery-class"),
  246. PROPERTY_ENTRY_BOOL("disable-reset"),
  247. { }
  248. };
  249. static struct regulator_consumer_supply fusb302_consumer = {
  250. .supply = "vbus",
  251. /* Must match fusb302 dev_name in intel_cht_int33fe.c */
  252. .dev_name = "i2c-fusb302",
  253. };
  254. static const struct regulator_init_data bq24190_vbus_init_data = {
  255. .constraints = {
  256. /* The name is used in intel_cht_int33fe.c do not change. */
  257. .name = "cht_wc_usb_typec_vbus",
  258. .valid_ops_mask = REGULATOR_CHANGE_STATUS,
  259. },
  260. .consumer_supplies = &fusb302_consumer,
  261. .num_consumer_supplies = 1,
  262. };
  263. static struct bq24190_platform_data bq24190_pdata = {
  264. .regulator_init_data = &bq24190_vbus_init_data,
  265. };
  266. static int cht_wc_i2c_adap_i2c_probe(struct platform_device *pdev)
  267. {
  268. struct intel_soc_pmic *pmic = dev_get_drvdata(pdev->dev.parent);
  269. struct cht_wc_i2c_adap *adap;
  270. struct i2c_board_info board_info = {
  271. .type = "bq24190",
  272. .addr = 0x6b,
  273. .dev_name = "bq24190",
  274. .properties = bq24190_props,
  275. .platform_data = &bq24190_pdata,
  276. };
  277. int ret, reg, irq;
  278. irq = platform_get_irq(pdev, 0);
  279. if (irq < 0) {
  280. dev_err(&pdev->dev, "Error missing irq resource\n");
  281. return -EINVAL;
  282. }
  283. adap = devm_kzalloc(&pdev->dev, sizeof(*adap), GFP_KERNEL);
  284. if (!adap)
  285. return -ENOMEM;
  286. init_waitqueue_head(&adap->wait);
  287. mutex_init(&adap->adap_lock);
  288. mutex_init(&adap->irqchip_lock);
  289. adap->irqchip = cht_wc_i2c_irq_chip;
  290. adap->regmap = pmic->regmap;
  291. adap->adapter.owner = THIS_MODULE;
  292. adap->adapter.class = I2C_CLASS_HWMON;
  293. adap->adapter.algo = &cht_wc_i2c_adap_algo;
  294. adap->adapter.lock_ops = &cht_wc_i2c_adap_lock_ops;
  295. strlcpy(adap->adapter.name, "PMIC I2C Adapter",
  296. sizeof(adap->adapter.name));
  297. adap->adapter.dev.parent = &pdev->dev;
  298. /* Clear and activate i2c-adapter interrupts, disable client IRQ */
  299. adap->old_irq_mask = adap->irq_mask = ~CHT_WC_EXTCHGRIRQ_ADAP_IRQMASK;
  300. ret = regmap_read(adap->regmap, CHT_WC_I2C_RDDATA, &reg);
  301. if (ret)
  302. return ret;
  303. ret = regmap_write(adap->regmap, CHT_WC_EXTCHGRIRQ, ~adap->irq_mask);
  304. if (ret)
  305. return ret;
  306. ret = regmap_write(adap->regmap, CHT_WC_EXTCHGRIRQ_MSK, adap->irq_mask);
  307. if (ret)
  308. return ret;
  309. /* Alloc and register client IRQ */
  310. adap->irq_domain = irq_domain_add_linear(pdev->dev.of_node, 1,
  311. &irq_domain_simple_ops, NULL);
  312. if (!adap->irq_domain)
  313. return -ENOMEM;
  314. adap->client_irq = irq_create_mapping(adap->irq_domain, 0);
  315. if (!adap->client_irq) {
  316. ret = -ENOMEM;
  317. goto remove_irq_domain;
  318. }
  319. irq_set_chip_data(adap->client_irq, adap);
  320. irq_set_chip_and_handler(adap->client_irq, &adap->irqchip,
  321. handle_simple_irq);
  322. ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
  323. cht_wc_i2c_adap_thread_handler,
  324. IRQF_ONESHOT, "PMIC I2C Adapter", adap);
  325. if (ret)
  326. goto remove_irq_domain;
  327. i2c_set_adapdata(&adap->adapter, adap);
  328. ret = i2c_add_adapter(&adap->adapter);
  329. if (ret)
  330. goto remove_irq_domain;
  331. /*
  332. * Normally the Whiskey Cove PMIC is paired with a TI bq24292i charger,
  333. * connected to this i2c bus, and a max17047 fuel-gauge and a fusb302
  334. * USB Type-C controller connected to another i2c bus. In this setup
  335. * the max17047 and fusb302 devices are enumerated through an INT33FE
  336. * ACPI device. If this device is present register an i2c-client for
  337. * the TI bq24292i charger.
  338. */
  339. if (acpi_dev_present("INT33FE", NULL, -1)) {
  340. board_info.irq = adap->client_irq;
  341. adap->client = i2c_new_device(&adap->adapter, &board_info);
  342. if (!adap->client) {
  343. ret = -ENOMEM;
  344. goto del_adapter;
  345. }
  346. }
  347. platform_set_drvdata(pdev, adap);
  348. return 0;
  349. del_adapter:
  350. i2c_del_adapter(&adap->adapter);
  351. remove_irq_domain:
  352. irq_domain_remove(adap->irq_domain);
  353. return ret;
  354. }
  355. static int cht_wc_i2c_adap_i2c_remove(struct platform_device *pdev)
  356. {
  357. struct cht_wc_i2c_adap *adap = platform_get_drvdata(pdev);
  358. if (adap->client)
  359. i2c_unregister_device(adap->client);
  360. i2c_del_adapter(&adap->adapter);
  361. irq_domain_remove(adap->irq_domain);
  362. return 0;
  363. }
  364. static const struct platform_device_id cht_wc_i2c_adap_id_table[] = {
  365. { .name = "cht_wcove_ext_chgr" },
  366. {},
  367. };
  368. MODULE_DEVICE_TABLE(platform, cht_wc_i2c_adap_id_table);
  369. static struct platform_driver cht_wc_i2c_adap_driver = {
  370. .probe = cht_wc_i2c_adap_i2c_probe,
  371. .remove = cht_wc_i2c_adap_i2c_remove,
  372. .driver = {
  373. .name = "cht_wcove_ext_chgr",
  374. },
  375. .id_table = cht_wc_i2c_adap_id_table,
  376. };
  377. module_platform_driver(cht_wc_i2c_adap_driver);
  378. MODULE_DESCRIPTION("Intel CHT Whiskey Cove PMIC I2C Master driver");
  379. MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
  380. MODULE_LICENSE("GPL");