stmfx.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Driver for STMicroelectronics Multi-Function eXpander (STMFX) core
  4. *
  5. * Copyright (C) 2019 STMicroelectronics
  6. * Author(s): Amelie Delaunay <[email protected]>.
  7. */
  8. #include <linux/bitfield.h>
  9. #include <linux/i2c.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/irq.h>
  12. #include <linux/mfd/core.h>
  13. #include <linux/mfd/stmfx.h>
  14. #include <linux/module.h>
  15. #include <linux/regulator/consumer.h>
  16. static bool stmfx_reg_volatile(struct device *dev, unsigned int reg)
  17. {
  18. switch (reg) {
  19. case STMFX_REG_SYS_CTRL:
  20. case STMFX_REG_IRQ_SRC_EN:
  21. case STMFX_REG_IRQ_PENDING:
  22. case STMFX_REG_IRQ_GPI_PENDING1:
  23. case STMFX_REG_IRQ_GPI_PENDING2:
  24. case STMFX_REG_IRQ_GPI_PENDING3:
  25. case STMFX_REG_GPIO_STATE1:
  26. case STMFX_REG_GPIO_STATE2:
  27. case STMFX_REG_GPIO_STATE3:
  28. case STMFX_REG_IRQ_GPI_SRC1:
  29. case STMFX_REG_IRQ_GPI_SRC2:
  30. case STMFX_REG_IRQ_GPI_SRC3:
  31. case STMFX_REG_GPO_SET1:
  32. case STMFX_REG_GPO_SET2:
  33. case STMFX_REG_GPO_SET3:
  34. case STMFX_REG_GPO_CLR1:
  35. case STMFX_REG_GPO_CLR2:
  36. case STMFX_REG_GPO_CLR3:
  37. return true;
  38. default:
  39. return false;
  40. }
  41. }
  42. static bool stmfx_reg_writeable(struct device *dev, unsigned int reg)
  43. {
  44. return (reg >= STMFX_REG_SYS_CTRL);
  45. }
  46. static const struct regmap_config stmfx_regmap_config = {
  47. .reg_bits = 8,
  48. .reg_stride = 1,
  49. .val_bits = 8,
  50. .max_register = STMFX_REG_MAX,
  51. .volatile_reg = stmfx_reg_volatile,
  52. .writeable_reg = stmfx_reg_writeable,
  53. .cache_type = REGCACHE_RBTREE,
  54. };
  55. static const struct resource stmfx_pinctrl_resources[] = {
  56. DEFINE_RES_IRQ(STMFX_REG_IRQ_SRC_EN_GPIO),
  57. };
  58. static const struct resource stmfx_idd_resources[] = {
  59. DEFINE_RES_IRQ(STMFX_REG_IRQ_SRC_EN_IDD),
  60. DEFINE_RES_IRQ(STMFX_REG_IRQ_SRC_EN_ERROR),
  61. };
  62. static const struct resource stmfx_ts_resources[] = {
  63. DEFINE_RES_IRQ(STMFX_REG_IRQ_SRC_EN_TS_DET),
  64. DEFINE_RES_IRQ(STMFX_REG_IRQ_SRC_EN_TS_NE),
  65. DEFINE_RES_IRQ(STMFX_REG_IRQ_SRC_EN_TS_TH),
  66. DEFINE_RES_IRQ(STMFX_REG_IRQ_SRC_EN_TS_FULL),
  67. DEFINE_RES_IRQ(STMFX_REG_IRQ_SRC_EN_TS_OVF),
  68. };
  69. static struct mfd_cell stmfx_cells[] = {
  70. {
  71. .of_compatible = "st,stmfx-0300-pinctrl",
  72. .name = "stmfx-pinctrl",
  73. .resources = stmfx_pinctrl_resources,
  74. .num_resources = ARRAY_SIZE(stmfx_pinctrl_resources),
  75. },
  76. {
  77. .of_compatible = "st,stmfx-0300-idd",
  78. .name = "stmfx-idd",
  79. .resources = stmfx_idd_resources,
  80. .num_resources = ARRAY_SIZE(stmfx_idd_resources),
  81. },
  82. {
  83. .of_compatible = "st,stmfx-0300-ts",
  84. .name = "stmfx-ts",
  85. .resources = stmfx_ts_resources,
  86. .num_resources = ARRAY_SIZE(stmfx_ts_resources),
  87. },
  88. };
  89. static u8 stmfx_func_to_mask(u32 func)
  90. {
  91. u8 mask = 0;
  92. if (func & STMFX_FUNC_GPIO)
  93. mask |= STMFX_REG_SYS_CTRL_GPIO_EN;
  94. if ((func & STMFX_FUNC_ALTGPIO_LOW) || (func & STMFX_FUNC_ALTGPIO_HIGH))
  95. mask |= STMFX_REG_SYS_CTRL_ALTGPIO_EN;
  96. if (func & STMFX_FUNC_TS)
  97. mask |= STMFX_REG_SYS_CTRL_TS_EN;
  98. if (func & STMFX_FUNC_IDD)
  99. mask |= STMFX_REG_SYS_CTRL_IDD_EN;
  100. return mask;
  101. }
  102. int stmfx_function_enable(struct stmfx *stmfx, u32 func)
  103. {
  104. u32 sys_ctrl;
  105. u8 mask;
  106. int ret;
  107. ret = regmap_read(stmfx->map, STMFX_REG_SYS_CTRL, &sys_ctrl);
  108. if (ret)
  109. return ret;
  110. /*
  111. * IDD and TS have priority in STMFX FW, so if IDD and TS are enabled,
  112. * ALTGPIO function is disabled by STMFX FW. If IDD or TS is enabled,
  113. * the number of aGPIO available decreases. To avoid GPIO management
  114. * disturbance, abort IDD or TS function enable in this case.
  115. */
  116. if (((func & STMFX_FUNC_IDD) || (func & STMFX_FUNC_TS)) &&
  117. (sys_ctrl & STMFX_REG_SYS_CTRL_ALTGPIO_EN)) {
  118. dev_err(stmfx->dev, "ALTGPIO function already enabled\n");
  119. return -EBUSY;
  120. }
  121. /* If TS is enabled, aGPIO[3:0] cannot be used */
  122. if ((func & STMFX_FUNC_ALTGPIO_LOW) &&
  123. (sys_ctrl & STMFX_REG_SYS_CTRL_TS_EN)) {
  124. dev_err(stmfx->dev, "TS in use, aGPIO[3:0] unavailable\n");
  125. return -EBUSY;
  126. }
  127. /* If IDD is enabled, aGPIO[7:4] cannot be used */
  128. if ((func & STMFX_FUNC_ALTGPIO_HIGH) &&
  129. (sys_ctrl & STMFX_REG_SYS_CTRL_IDD_EN)) {
  130. dev_err(stmfx->dev, "IDD in use, aGPIO[7:4] unavailable\n");
  131. return -EBUSY;
  132. }
  133. mask = stmfx_func_to_mask(func);
  134. return regmap_update_bits(stmfx->map, STMFX_REG_SYS_CTRL, mask, mask);
  135. }
  136. EXPORT_SYMBOL_GPL(stmfx_function_enable);
  137. int stmfx_function_disable(struct stmfx *stmfx, u32 func)
  138. {
  139. u8 mask = stmfx_func_to_mask(func);
  140. return regmap_update_bits(stmfx->map, STMFX_REG_SYS_CTRL, mask, 0);
  141. }
  142. EXPORT_SYMBOL_GPL(stmfx_function_disable);
  143. static void stmfx_irq_bus_lock(struct irq_data *data)
  144. {
  145. struct stmfx *stmfx = irq_data_get_irq_chip_data(data);
  146. mutex_lock(&stmfx->lock);
  147. }
  148. static void stmfx_irq_bus_sync_unlock(struct irq_data *data)
  149. {
  150. struct stmfx *stmfx = irq_data_get_irq_chip_data(data);
  151. regmap_write(stmfx->map, STMFX_REG_IRQ_SRC_EN, stmfx->irq_src);
  152. mutex_unlock(&stmfx->lock);
  153. }
  154. static void stmfx_irq_mask(struct irq_data *data)
  155. {
  156. struct stmfx *stmfx = irq_data_get_irq_chip_data(data);
  157. stmfx->irq_src &= ~BIT(data->hwirq % 8);
  158. }
  159. static void stmfx_irq_unmask(struct irq_data *data)
  160. {
  161. struct stmfx *stmfx = irq_data_get_irq_chip_data(data);
  162. stmfx->irq_src |= BIT(data->hwirq % 8);
  163. }
  164. static struct irq_chip stmfx_irq_chip = {
  165. .name = "stmfx-core",
  166. .irq_bus_lock = stmfx_irq_bus_lock,
  167. .irq_bus_sync_unlock = stmfx_irq_bus_sync_unlock,
  168. .irq_mask = stmfx_irq_mask,
  169. .irq_unmask = stmfx_irq_unmask,
  170. };
  171. static irqreturn_t stmfx_irq_handler(int irq, void *data)
  172. {
  173. struct stmfx *stmfx = data;
  174. unsigned long bits;
  175. u32 pending, ack;
  176. int n, ret;
  177. ret = regmap_read(stmfx->map, STMFX_REG_IRQ_PENDING, &pending);
  178. if (ret)
  179. return IRQ_NONE;
  180. /*
  181. * There is no ACK for GPIO, MFX_REG_IRQ_PENDING_GPIO is a logical OR
  182. * of MFX_REG_IRQ_GPI _PENDING1/_PENDING2/_PENDING3
  183. */
  184. ack = pending & ~BIT(STMFX_REG_IRQ_SRC_EN_GPIO);
  185. if (ack) {
  186. ret = regmap_write(stmfx->map, STMFX_REG_IRQ_ACK, ack);
  187. if (ret)
  188. return IRQ_NONE;
  189. }
  190. bits = pending;
  191. for_each_set_bit(n, &bits, STMFX_REG_IRQ_SRC_MAX)
  192. handle_nested_irq(irq_find_mapping(stmfx->irq_domain, n));
  193. return IRQ_HANDLED;
  194. }
  195. static int stmfx_irq_map(struct irq_domain *d, unsigned int virq,
  196. irq_hw_number_t hwirq)
  197. {
  198. irq_set_chip_data(virq, d->host_data);
  199. irq_set_chip_and_handler(virq, &stmfx_irq_chip, handle_simple_irq);
  200. irq_set_nested_thread(virq, 1);
  201. irq_set_noprobe(virq);
  202. return 0;
  203. }
  204. static void stmfx_irq_unmap(struct irq_domain *d, unsigned int virq)
  205. {
  206. irq_set_chip_and_handler(virq, NULL, NULL);
  207. irq_set_chip_data(virq, NULL);
  208. }
  209. static const struct irq_domain_ops stmfx_irq_ops = {
  210. .map = stmfx_irq_map,
  211. .unmap = stmfx_irq_unmap,
  212. };
  213. static void stmfx_irq_exit(struct i2c_client *client)
  214. {
  215. struct stmfx *stmfx = i2c_get_clientdata(client);
  216. int hwirq;
  217. for (hwirq = 0; hwirq < STMFX_REG_IRQ_SRC_MAX; hwirq++)
  218. irq_dispose_mapping(irq_find_mapping(stmfx->irq_domain, hwirq));
  219. irq_domain_remove(stmfx->irq_domain);
  220. }
  221. static int stmfx_irq_init(struct i2c_client *client)
  222. {
  223. struct stmfx *stmfx = i2c_get_clientdata(client);
  224. u32 irqoutpin = 0, irqtrigger;
  225. int ret;
  226. stmfx->irq_domain = irq_domain_add_simple(stmfx->dev->of_node,
  227. STMFX_REG_IRQ_SRC_MAX, 0,
  228. &stmfx_irq_ops, stmfx);
  229. if (!stmfx->irq_domain) {
  230. dev_err(stmfx->dev, "Failed to create IRQ domain\n");
  231. return -EINVAL;
  232. }
  233. if (!of_property_read_bool(stmfx->dev->of_node, "drive-open-drain"))
  234. irqoutpin |= STMFX_REG_IRQ_OUT_PIN_TYPE;
  235. irqtrigger = irq_get_trigger_type(client->irq);
  236. if ((irqtrigger & IRQ_TYPE_EDGE_RISING) ||
  237. (irqtrigger & IRQ_TYPE_LEVEL_HIGH))
  238. irqoutpin |= STMFX_REG_IRQ_OUT_PIN_POL;
  239. ret = regmap_write(stmfx->map, STMFX_REG_IRQ_OUT_PIN, irqoutpin);
  240. if (ret)
  241. goto irq_exit;
  242. ret = devm_request_threaded_irq(stmfx->dev, client->irq,
  243. NULL, stmfx_irq_handler,
  244. irqtrigger | IRQF_ONESHOT,
  245. "stmfx", stmfx);
  246. if (ret)
  247. goto irq_exit;
  248. stmfx->irq = client->irq;
  249. return 0;
  250. irq_exit:
  251. stmfx_irq_exit(client);
  252. return ret;
  253. }
  254. static int stmfx_chip_reset(struct stmfx *stmfx)
  255. {
  256. int ret;
  257. ret = regmap_write(stmfx->map, STMFX_REG_SYS_CTRL,
  258. STMFX_REG_SYS_CTRL_SWRST);
  259. if (ret)
  260. return ret;
  261. msleep(STMFX_BOOT_TIME_MS);
  262. return ret;
  263. }
  264. static int stmfx_chip_init(struct i2c_client *client)
  265. {
  266. struct stmfx *stmfx = i2c_get_clientdata(client);
  267. u32 id;
  268. u8 version[2];
  269. int ret;
  270. stmfx->vdd = devm_regulator_get_optional(&client->dev, "vdd");
  271. ret = PTR_ERR_OR_ZERO(stmfx->vdd);
  272. if (ret) {
  273. stmfx->vdd = NULL;
  274. if (ret != -ENODEV)
  275. return dev_err_probe(&client->dev, ret, "Failed to get VDD regulator\n");
  276. }
  277. if (stmfx->vdd) {
  278. ret = regulator_enable(stmfx->vdd);
  279. if (ret) {
  280. dev_err(&client->dev, "VDD enable failed: %d\n", ret);
  281. return ret;
  282. }
  283. }
  284. ret = regmap_read(stmfx->map, STMFX_REG_CHIP_ID, &id);
  285. if (ret) {
  286. dev_err(&client->dev, "Error reading chip ID: %d\n", ret);
  287. goto err;
  288. }
  289. /*
  290. * Check that ID is the complement of the I2C address:
  291. * STMFX I2C address follows the 7-bit format (MSB), that's why
  292. * client->addr is shifted.
  293. *
  294. * STMFX_I2C_ADDR| STMFX | Linux
  295. * input pin | I2C device address | I2C device address
  296. *---------------------------------------------------------
  297. * 0 | b: 1000 010x h:0x84 | 0x42
  298. * 1 | b: 1000 011x h:0x86 | 0x43
  299. */
  300. if (FIELD_GET(STMFX_REG_CHIP_ID_MASK, ~id) != (client->addr << 1)) {
  301. dev_err(&client->dev, "Unknown chip ID: %#x\n", id);
  302. ret = -EINVAL;
  303. goto err;
  304. }
  305. ret = regmap_bulk_read(stmfx->map, STMFX_REG_FW_VERSION_MSB,
  306. version, ARRAY_SIZE(version));
  307. if (ret) {
  308. dev_err(&client->dev, "Error reading FW version: %d\n", ret);
  309. goto err;
  310. }
  311. dev_info(&client->dev, "STMFX id: %#x, fw version: %x.%02x\n",
  312. id, version[0], version[1]);
  313. ret = stmfx_chip_reset(stmfx);
  314. if (ret) {
  315. dev_err(&client->dev, "Failed to reset chip: %d\n", ret);
  316. goto err;
  317. }
  318. return 0;
  319. err:
  320. if (stmfx->vdd)
  321. regulator_disable(stmfx->vdd);
  322. return ret;
  323. }
  324. static void stmfx_chip_exit(struct i2c_client *client)
  325. {
  326. struct stmfx *stmfx = i2c_get_clientdata(client);
  327. regmap_write(stmfx->map, STMFX_REG_IRQ_SRC_EN, 0);
  328. regmap_write(stmfx->map, STMFX_REG_SYS_CTRL, 0);
  329. if (stmfx->vdd) {
  330. int ret;
  331. ret = regulator_disable(stmfx->vdd);
  332. if (ret)
  333. dev_err(&client->dev,
  334. "Failed to disable vdd regulator: %pe\n",
  335. ERR_PTR(ret));
  336. }
  337. }
  338. static int stmfx_probe(struct i2c_client *client,
  339. const struct i2c_device_id *id)
  340. {
  341. struct device *dev = &client->dev;
  342. struct stmfx *stmfx;
  343. int ret;
  344. stmfx = devm_kzalloc(dev, sizeof(*stmfx), GFP_KERNEL);
  345. if (!stmfx)
  346. return -ENOMEM;
  347. i2c_set_clientdata(client, stmfx);
  348. stmfx->dev = dev;
  349. stmfx->map = devm_regmap_init_i2c(client, &stmfx_regmap_config);
  350. if (IS_ERR(stmfx->map)) {
  351. ret = PTR_ERR(stmfx->map);
  352. dev_err(dev, "Failed to allocate register map: %d\n", ret);
  353. return ret;
  354. }
  355. mutex_init(&stmfx->lock);
  356. ret = stmfx_chip_init(client);
  357. if (ret) {
  358. if (ret == -ETIMEDOUT)
  359. return -EPROBE_DEFER;
  360. return ret;
  361. }
  362. if (client->irq < 0) {
  363. dev_err(dev, "Failed to get IRQ: %d\n", client->irq);
  364. ret = client->irq;
  365. goto err_chip_exit;
  366. }
  367. ret = stmfx_irq_init(client);
  368. if (ret)
  369. goto err_chip_exit;
  370. ret = devm_mfd_add_devices(dev, PLATFORM_DEVID_NONE,
  371. stmfx_cells, ARRAY_SIZE(stmfx_cells), NULL,
  372. 0, stmfx->irq_domain);
  373. if (ret)
  374. goto err_irq_exit;
  375. return 0;
  376. err_irq_exit:
  377. stmfx_irq_exit(client);
  378. err_chip_exit:
  379. stmfx_chip_exit(client);
  380. return ret;
  381. }
  382. static void stmfx_remove(struct i2c_client *client)
  383. {
  384. stmfx_irq_exit(client);
  385. stmfx_chip_exit(client);
  386. }
  387. #ifdef CONFIG_PM_SLEEP
  388. static int stmfx_suspend(struct device *dev)
  389. {
  390. struct stmfx *stmfx = dev_get_drvdata(dev);
  391. int ret;
  392. ret = regmap_raw_read(stmfx->map, STMFX_REG_SYS_CTRL,
  393. &stmfx->bkp_sysctrl, sizeof(stmfx->bkp_sysctrl));
  394. if (ret)
  395. return ret;
  396. ret = regmap_raw_read(stmfx->map, STMFX_REG_IRQ_OUT_PIN,
  397. &stmfx->bkp_irqoutpin,
  398. sizeof(stmfx->bkp_irqoutpin));
  399. if (ret)
  400. return ret;
  401. disable_irq(stmfx->irq);
  402. if (stmfx->vdd)
  403. return regulator_disable(stmfx->vdd);
  404. return 0;
  405. }
  406. static int stmfx_resume(struct device *dev)
  407. {
  408. struct stmfx *stmfx = dev_get_drvdata(dev);
  409. int ret;
  410. if (stmfx->vdd) {
  411. ret = regulator_enable(stmfx->vdd);
  412. if (ret) {
  413. dev_err(stmfx->dev,
  414. "VDD enable failed: %d\n", ret);
  415. return ret;
  416. }
  417. }
  418. /* Reset STMFX - supply has been stopped during suspend */
  419. ret = stmfx_chip_reset(stmfx);
  420. if (ret) {
  421. dev_err(stmfx->dev, "Failed to reset chip: %d\n", ret);
  422. return ret;
  423. }
  424. ret = regmap_raw_write(stmfx->map, STMFX_REG_SYS_CTRL,
  425. &stmfx->bkp_sysctrl, sizeof(stmfx->bkp_sysctrl));
  426. if (ret)
  427. return ret;
  428. ret = regmap_raw_write(stmfx->map, STMFX_REG_IRQ_OUT_PIN,
  429. &stmfx->bkp_irqoutpin,
  430. sizeof(stmfx->bkp_irqoutpin));
  431. if (ret)
  432. return ret;
  433. ret = regmap_raw_write(stmfx->map, STMFX_REG_IRQ_SRC_EN,
  434. &stmfx->irq_src, sizeof(stmfx->irq_src));
  435. if (ret)
  436. return ret;
  437. enable_irq(stmfx->irq);
  438. return 0;
  439. }
  440. #endif
  441. static SIMPLE_DEV_PM_OPS(stmfx_dev_pm_ops, stmfx_suspend, stmfx_resume);
  442. static const struct of_device_id stmfx_of_match[] = {
  443. { .compatible = "st,stmfx-0300", },
  444. {},
  445. };
  446. MODULE_DEVICE_TABLE(of, stmfx_of_match);
  447. static struct i2c_driver stmfx_driver = {
  448. .driver = {
  449. .name = "stmfx-core",
  450. .of_match_table = stmfx_of_match,
  451. .pm = &stmfx_dev_pm_ops,
  452. },
  453. .probe = stmfx_probe,
  454. .remove = stmfx_remove,
  455. };
  456. module_i2c_driver(stmfx_driver);
  457. MODULE_DESCRIPTION("STMFX core driver");
  458. MODULE_AUTHOR("Amelie Delaunay <[email protected]>");
  459. MODULE_LICENSE("GPL v2");