1a0df3ef0SJulien Panis // SPDX-License-Identifier: GPL-2.0
2a0df3ef0SJulien Panis /*
391020aecSBhargav Raviprakash * PFSM (Pre-configurable Finite State Machine) driver for TI TPS65224/TPS6594/TPS6593/LP8764 PMICs
4a0df3ef0SJulien Panis *
5a0df3ef0SJulien Panis * Copyright (C) 2023 BayLibre Incorporated - https://www.baylibre.com/
6a0df3ef0SJulien Panis */
7a0df3ef0SJulien Panis
8a0df3ef0SJulien Panis #include <linux/errno.h>
9a0df3ef0SJulien Panis #include <linux/fs.h>
10a0df3ef0SJulien Panis #include <linux/interrupt.h>
11a0df3ef0SJulien Panis #include <linux/ioctl.h>
12a0df3ef0SJulien Panis #include <linux/miscdevice.h>
13a0df3ef0SJulien Panis #include <linux/module.h>
14a0df3ef0SJulien Panis #include <linux/platform_device.h>
15a0df3ef0SJulien Panis #include <linux/regmap.h>
16a0df3ef0SJulien Panis
17a0df3ef0SJulien Panis #include <linux/mfd/tps6594.h>
18a0df3ef0SJulien Panis
19a0df3ef0SJulien Panis #include <linux/tps6594_pfsm.h>
20a0df3ef0SJulien Panis
21a0df3ef0SJulien Panis #define TPS6594_STARTUP_DEST_MCU_ONLY_VAL 2
22a0df3ef0SJulien Panis #define TPS6594_STARTUP_DEST_ACTIVE_VAL 3
23a0df3ef0SJulien Panis #define TPS6594_STARTUP_DEST_SHIFT 5
24a0df3ef0SJulien Panis #define TPS6594_STARTUP_DEST_MCU_ONLY (TPS6594_STARTUP_DEST_MCU_ONLY_VAL \
25a0df3ef0SJulien Panis << TPS6594_STARTUP_DEST_SHIFT)
26a0df3ef0SJulien Panis #define TPS6594_STARTUP_DEST_ACTIVE (TPS6594_STARTUP_DEST_ACTIVE_VAL \
27a0df3ef0SJulien Panis << TPS6594_STARTUP_DEST_SHIFT)
28a0df3ef0SJulien Panis
29a0df3ef0SJulien Panis /*
30a0df3ef0SJulien Panis * To update the PMIC firmware, the user must be able to access
31a0df3ef0SJulien Panis * page 0 (user registers) and page 1 (NVM control and configuration).
32a0df3ef0SJulien Panis */
33a0df3ef0SJulien Panis #define TPS6594_PMIC_MAX_POS 0x200
34a0df3ef0SJulien Panis
35a0df3ef0SJulien Panis #define TPS6594_FILE_TO_PFSM(f) container_of((f)->private_data, struct tps6594_pfsm, miscdev)
36a0df3ef0SJulien Panis
37a0df3ef0SJulien Panis /**
38a0df3ef0SJulien Panis * struct tps6594_pfsm - device private data structure
39a0df3ef0SJulien Panis *
40a0df3ef0SJulien Panis * @miscdev: misc device infos
41a0df3ef0SJulien Panis * @regmap: regmap for accessing the device registers
4291020aecSBhargav Raviprakash * @chip_id: chip identifier of the device
43a0df3ef0SJulien Panis */
44a0df3ef0SJulien Panis struct tps6594_pfsm {
45a0df3ef0SJulien Panis struct miscdevice miscdev;
46a0df3ef0SJulien Panis struct regmap *regmap;
4791020aecSBhargav Raviprakash unsigned long chip_id;
48a0df3ef0SJulien Panis };
49a0df3ef0SJulien Panis
tps6594_pfsm_read(struct file * f,char __user * buf,size_t count,loff_t * ppos)50a0df3ef0SJulien Panis static ssize_t tps6594_pfsm_read(struct file *f, char __user *buf,
51a0df3ef0SJulien Panis size_t count, loff_t *ppos)
52a0df3ef0SJulien Panis {
53a0df3ef0SJulien Panis struct tps6594_pfsm *pfsm = TPS6594_FILE_TO_PFSM(f);
54a0df3ef0SJulien Panis loff_t pos = *ppos;
55a0df3ef0SJulien Panis unsigned int val;
56a0df3ef0SJulien Panis int ret;
57a0df3ef0SJulien Panis int i;
58a0df3ef0SJulien Panis
59a0df3ef0SJulien Panis if (pos < 0)
60a0df3ef0SJulien Panis return -EINVAL;
61a0df3ef0SJulien Panis if (pos >= TPS6594_PMIC_MAX_POS)
62a0df3ef0SJulien Panis return 0;
63a0df3ef0SJulien Panis if (count > TPS6594_PMIC_MAX_POS - pos)
64a0df3ef0SJulien Panis count = TPS6594_PMIC_MAX_POS - pos;
65a0df3ef0SJulien Panis
66a0df3ef0SJulien Panis for (i = 0 ; i < count ; i++) {
67a0df3ef0SJulien Panis ret = regmap_read(pfsm->regmap, pos + i, &val);
68a0df3ef0SJulien Panis if (ret)
69a0df3ef0SJulien Panis return ret;
70a0df3ef0SJulien Panis
71a0df3ef0SJulien Panis if (put_user(val, buf + i))
72a0df3ef0SJulien Panis return -EFAULT;
73a0df3ef0SJulien Panis }
74a0df3ef0SJulien Panis
75a0df3ef0SJulien Panis *ppos = pos + count;
76a0df3ef0SJulien Panis
77a0df3ef0SJulien Panis return count;
78a0df3ef0SJulien Panis }
79a0df3ef0SJulien Panis
tps6594_pfsm_write(struct file * f,const char __user * buf,size_t count,loff_t * ppos)80a0df3ef0SJulien Panis static ssize_t tps6594_pfsm_write(struct file *f, const char __user *buf,
81a0df3ef0SJulien Panis size_t count, loff_t *ppos)
82a0df3ef0SJulien Panis {
83a0df3ef0SJulien Panis struct tps6594_pfsm *pfsm = TPS6594_FILE_TO_PFSM(f);
84a0df3ef0SJulien Panis loff_t pos = *ppos;
85a0df3ef0SJulien Panis char val;
86a0df3ef0SJulien Panis int ret;
87a0df3ef0SJulien Panis int i;
88a0df3ef0SJulien Panis
89a0df3ef0SJulien Panis if (pos < 0)
90a0df3ef0SJulien Panis return -EINVAL;
91a0df3ef0SJulien Panis if (pos >= TPS6594_PMIC_MAX_POS || !count)
92a0df3ef0SJulien Panis return 0;
93a0df3ef0SJulien Panis if (count > TPS6594_PMIC_MAX_POS - pos)
94a0df3ef0SJulien Panis count = TPS6594_PMIC_MAX_POS - pos;
95a0df3ef0SJulien Panis
96a0df3ef0SJulien Panis for (i = 0 ; i < count ; i++) {
97a0df3ef0SJulien Panis if (get_user(val, buf + i))
98a0df3ef0SJulien Panis return -EFAULT;
99a0df3ef0SJulien Panis
100a0df3ef0SJulien Panis ret = regmap_write(pfsm->regmap, pos + i, val);
101a0df3ef0SJulien Panis if (ret)
102a0df3ef0SJulien Panis return ret;
103a0df3ef0SJulien Panis }
104a0df3ef0SJulien Panis
105a0df3ef0SJulien Panis *ppos = pos + count;
106a0df3ef0SJulien Panis
107a0df3ef0SJulien Panis return count;
108a0df3ef0SJulien Panis }
109a0df3ef0SJulien Panis
tps6594_pfsm_configure_ret_trig(struct regmap * regmap,u8 gpio_ret,u8 ddr_ret)110a0df3ef0SJulien Panis static int tps6594_pfsm_configure_ret_trig(struct regmap *regmap, u8 gpio_ret, u8 ddr_ret)
111a0df3ef0SJulien Panis {
112a0df3ef0SJulien Panis int ret;
113a0df3ef0SJulien Panis
114a0df3ef0SJulien Panis if (gpio_ret)
115a0df3ef0SJulien Panis ret = regmap_set_bits(regmap, TPS6594_REG_FSM_I2C_TRIGGERS,
116a0df3ef0SJulien Panis TPS6594_BIT_TRIGGER_I2C(5) | TPS6594_BIT_TRIGGER_I2C(6));
117a0df3ef0SJulien Panis else
118a0df3ef0SJulien Panis ret = regmap_clear_bits(regmap, TPS6594_REG_FSM_I2C_TRIGGERS,
119a0df3ef0SJulien Panis TPS6594_BIT_TRIGGER_I2C(5) | TPS6594_BIT_TRIGGER_I2C(6));
120a0df3ef0SJulien Panis if (ret)
121a0df3ef0SJulien Panis return ret;
122a0df3ef0SJulien Panis
123a0df3ef0SJulien Panis if (ddr_ret)
124a0df3ef0SJulien Panis ret = regmap_set_bits(regmap, TPS6594_REG_FSM_I2C_TRIGGERS,
125a0df3ef0SJulien Panis TPS6594_BIT_TRIGGER_I2C(7));
126a0df3ef0SJulien Panis else
127a0df3ef0SJulien Panis ret = regmap_clear_bits(regmap, TPS6594_REG_FSM_I2C_TRIGGERS,
128a0df3ef0SJulien Panis TPS6594_BIT_TRIGGER_I2C(7));
129a0df3ef0SJulien Panis
130a0df3ef0SJulien Panis return ret;
131a0df3ef0SJulien Panis }
132a0df3ef0SJulien Panis
tps6594_pfsm_ioctl(struct file * f,unsigned int cmd,unsigned long arg)133a0df3ef0SJulien Panis static long tps6594_pfsm_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
134a0df3ef0SJulien Panis {
135a0df3ef0SJulien Panis struct tps6594_pfsm *pfsm = TPS6594_FILE_TO_PFSM(f);
136a0df3ef0SJulien Panis struct pmic_state_opt state_opt;
137a0df3ef0SJulien Panis void __user *argp = (void __user *)arg;
13891020aecSBhargav Raviprakash unsigned int regmap_reg, mask;
139a0df3ef0SJulien Panis int ret = -ENOIOCTLCMD;
140a0df3ef0SJulien Panis
141a0df3ef0SJulien Panis switch (cmd) {
142a0df3ef0SJulien Panis case PMIC_GOTO_STANDBY:
14391020aecSBhargav Raviprakash /* Disable LP mode on TPS6594 Family PMIC */
14491020aecSBhargav Raviprakash if (pfsm->chip_id != TPS65224) {
145a0df3ef0SJulien Panis ret = regmap_clear_bits(pfsm->regmap, TPS6594_REG_RTC_CTRL_2,
146a0df3ef0SJulien Panis TPS6594_BIT_LP_STANDBY_SEL);
14791020aecSBhargav Raviprakash
148a0df3ef0SJulien Panis if (ret)
149a0df3ef0SJulien Panis return ret;
15091020aecSBhargav Raviprakash }
151a0df3ef0SJulien Panis
152a0df3ef0SJulien Panis /* Force trigger */
153a0df3ef0SJulien Panis ret = regmap_write_bits(pfsm->regmap, TPS6594_REG_FSM_I2C_TRIGGERS,
154a0df3ef0SJulien Panis TPS6594_BIT_TRIGGER_I2C(0), TPS6594_BIT_TRIGGER_I2C(0));
155a0df3ef0SJulien Panis break;
156a0df3ef0SJulien Panis case PMIC_GOTO_LP_STANDBY:
15791020aecSBhargav Raviprakash /* TPS65224 does not support LP STANDBY */
15891020aecSBhargav Raviprakash if (pfsm->chip_id == TPS65224)
15991020aecSBhargav Raviprakash return ret;
16091020aecSBhargav Raviprakash
161a0df3ef0SJulien Panis /* Enable LP mode */
162a0df3ef0SJulien Panis ret = regmap_set_bits(pfsm->regmap, TPS6594_REG_RTC_CTRL_2,
163a0df3ef0SJulien Panis TPS6594_BIT_LP_STANDBY_SEL);
164a0df3ef0SJulien Panis if (ret)
165a0df3ef0SJulien Panis return ret;
166a0df3ef0SJulien Panis
167a0df3ef0SJulien Panis /* Force trigger */
168a0df3ef0SJulien Panis ret = regmap_write_bits(pfsm->regmap, TPS6594_REG_FSM_I2C_TRIGGERS,
169a0df3ef0SJulien Panis TPS6594_BIT_TRIGGER_I2C(0), TPS6594_BIT_TRIGGER_I2C(0));
170a0df3ef0SJulien Panis break;
171a0df3ef0SJulien Panis case PMIC_UPDATE_PGM:
172a0df3ef0SJulien Panis /* Force trigger */
173a0df3ef0SJulien Panis ret = regmap_write_bits(pfsm->regmap, TPS6594_REG_FSM_I2C_TRIGGERS,
174a0df3ef0SJulien Panis TPS6594_BIT_TRIGGER_I2C(3), TPS6594_BIT_TRIGGER_I2C(3));
175a0df3ef0SJulien Panis break;
176a0df3ef0SJulien Panis case PMIC_SET_ACTIVE_STATE:
177a0df3ef0SJulien Panis /* Modify NSLEEP1-2 bits */
178a0df3ef0SJulien Panis ret = regmap_set_bits(pfsm->regmap, TPS6594_REG_FSM_NSLEEP_TRIGGERS,
179a0df3ef0SJulien Panis TPS6594_BIT_NSLEEP1B | TPS6594_BIT_NSLEEP2B);
180a0df3ef0SJulien Panis break;
181a0df3ef0SJulien Panis case PMIC_SET_MCU_ONLY_STATE:
18291020aecSBhargav Raviprakash /* TPS65224 does not support MCU_ONLY_STATE */
18391020aecSBhargav Raviprakash if (pfsm->chip_id == TPS65224)
18491020aecSBhargav Raviprakash return ret;
18591020aecSBhargav Raviprakash
186a0df3ef0SJulien Panis if (copy_from_user(&state_opt, argp, sizeof(state_opt)))
187a0df3ef0SJulien Panis return -EFAULT;
188a0df3ef0SJulien Panis
189a0df3ef0SJulien Panis /* Configure retention triggers */
190a0df3ef0SJulien Panis ret = tps6594_pfsm_configure_ret_trig(pfsm->regmap, state_opt.gpio_retention,
191a0df3ef0SJulien Panis state_opt.ddr_retention);
192a0df3ef0SJulien Panis if (ret)
193a0df3ef0SJulien Panis return ret;
194a0df3ef0SJulien Panis
195a0df3ef0SJulien Panis /* Modify NSLEEP1-2 bits */
196a0df3ef0SJulien Panis ret = regmap_clear_bits(pfsm->regmap, TPS6594_REG_FSM_NSLEEP_TRIGGERS,
197a0df3ef0SJulien Panis TPS6594_BIT_NSLEEP1B);
198a0df3ef0SJulien Panis if (ret)
199a0df3ef0SJulien Panis return ret;
200a0df3ef0SJulien Panis
201a0df3ef0SJulien Panis ret = regmap_set_bits(pfsm->regmap, TPS6594_REG_FSM_NSLEEP_TRIGGERS,
202a0df3ef0SJulien Panis TPS6594_BIT_NSLEEP2B);
203a0df3ef0SJulien Panis break;
204a0df3ef0SJulien Panis case PMIC_SET_RETENTION_STATE:
205a0df3ef0SJulien Panis if (copy_from_user(&state_opt, argp, sizeof(state_opt)))
206a0df3ef0SJulien Panis return -EFAULT;
207a0df3ef0SJulien Panis
208a0df3ef0SJulien Panis /* Configure wake-up destination */
20991020aecSBhargav Raviprakash if (pfsm->chip_id == TPS65224) {
21091020aecSBhargav Raviprakash regmap_reg = TPS65224_REG_STARTUP_CTRL;
21191020aecSBhargav Raviprakash mask = TPS65224_MASK_STARTUP_DEST;
21291020aecSBhargav Raviprakash } else {
21391020aecSBhargav Raviprakash regmap_reg = TPS6594_REG_RTC_CTRL_2;
21491020aecSBhargav Raviprakash mask = TPS6594_MASK_STARTUP_DEST;
21591020aecSBhargav Raviprakash }
21691020aecSBhargav Raviprakash
217a0df3ef0SJulien Panis if (state_opt.mcu_only_startup_dest)
21891020aecSBhargav Raviprakash ret = regmap_write_bits(pfsm->regmap, regmap_reg,
21991020aecSBhargav Raviprakash mask, TPS6594_STARTUP_DEST_MCU_ONLY);
220a0df3ef0SJulien Panis else
22191020aecSBhargav Raviprakash ret = regmap_write_bits(pfsm->regmap, regmap_reg,
22291020aecSBhargav Raviprakash mask, TPS6594_STARTUP_DEST_ACTIVE);
223a0df3ef0SJulien Panis if (ret)
224a0df3ef0SJulien Panis return ret;
225a0df3ef0SJulien Panis
226a0df3ef0SJulien Panis /* Configure retention triggers */
227a0df3ef0SJulien Panis ret = tps6594_pfsm_configure_ret_trig(pfsm->regmap, state_opt.gpio_retention,
228a0df3ef0SJulien Panis state_opt.ddr_retention);
229a0df3ef0SJulien Panis if (ret)
230a0df3ef0SJulien Panis return ret;
231a0df3ef0SJulien Panis
232a0df3ef0SJulien Panis /* Modify NSLEEP1-2 bits */
233a0df3ef0SJulien Panis ret = regmap_clear_bits(pfsm->regmap, TPS6594_REG_FSM_NSLEEP_TRIGGERS,
23491020aecSBhargav Raviprakash pfsm->chip_id == TPS65224 ?
23591020aecSBhargav Raviprakash TPS6594_BIT_NSLEEP1B : TPS6594_BIT_NSLEEP2B);
236a0df3ef0SJulien Panis break;
237a0df3ef0SJulien Panis }
238a0df3ef0SJulien Panis
239a0df3ef0SJulien Panis return ret;
240a0df3ef0SJulien Panis }
241a0df3ef0SJulien Panis
242a0df3ef0SJulien Panis static const struct file_operations tps6594_pfsm_fops = {
243a0df3ef0SJulien Panis .owner = THIS_MODULE,
244a0df3ef0SJulien Panis .llseek = generic_file_llseek,
245a0df3ef0SJulien Panis .read = tps6594_pfsm_read,
246a0df3ef0SJulien Panis .write = tps6594_pfsm_write,
247a0df3ef0SJulien Panis .unlocked_ioctl = tps6594_pfsm_ioctl,
248a0df3ef0SJulien Panis .compat_ioctl = compat_ptr_ioctl,
249a0df3ef0SJulien Panis };
250a0df3ef0SJulien Panis
tps6594_pfsm_isr(int irq,void * dev_id)251a0df3ef0SJulien Panis static irqreturn_t tps6594_pfsm_isr(int irq, void *dev_id)
252a0df3ef0SJulien Panis {
253a0df3ef0SJulien Panis struct platform_device *pdev = dev_id;
254a0df3ef0SJulien Panis int i;
255a0df3ef0SJulien Panis
256a0df3ef0SJulien Panis for (i = 0 ; i < pdev->num_resources ; i++) {
257a0df3ef0SJulien Panis if (irq == platform_get_irq_byname(pdev, pdev->resource[i].name)) {
258a0df3ef0SJulien Panis dev_err(pdev->dev.parent, "%s event detected\n", pdev->resource[i].name);
259a0df3ef0SJulien Panis return IRQ_HANDLED;
260a0df3ef0SJulien Panis }
261a0df3ef0SJulien Panis }
262a0df3ef0SJulien Panis
263a0df3ef0SJulien Panis return IRQ_NONE;
264a0df3ef0SJulien Panis }
265a0df3ef0SJulien Panis
tps6594_pfsm_probe(struct platform_device * pdev)266a0df3ef0SJulien Panis static int tps6594_pfsm_probe(struct platform_device *pdev)
267a0df3ef0SJulien Panis {
268a0df3ef0SJulien Panis struct tps6594_pfsm *pfsm;
269a0df3ef0SJulien Panis struct tps6594 *tps = dev_get_drvdata(pdev->dev.parent);
270a0df3ef0SJulien Panis struct device *dev = &pdev->dev;
271a0df3ef0SJulien Panis int irq;
272a0df3ef0SJulien Panis int ret;
273a0df3ef0SJulien Panis int i;
274a0df3ef0SJulien Panis
275a0df3ef0SJulien Panis pfsm = devm_kzalloc(dev, sizeof(struct tps6594_pfsm), GFP_KERNEL);
276a0df3ef0SJulien Panis if (!pfsm)
277a0df3ef0SJulien Panis return -ENOMEM;
278a0df3ef0SJulien Panis
279a0df3ef0SJulien Panis pfsm->regmap = tps->regmap;
280a0df3ef0SJulien Panis
281a0df3ef0SJulien Panis pfsm->miscdev.minor = MISC_DYNAMIC_MINOR;
282a0df3ef0SJulien Panis pfsm->miscdev.name = devm_kasprintf(dev, GFP_KERNEL, "pfsm-%ld-0x%02x",
283a0df3ef0SJulien Panis tps->chip_id, tps->reg);
284a0df3ef0SJulien Panis pfsm->miscdev.fops = &tps6594_pfsm_fops;
285a0df3ef0SJulien Panis pfsm->miscdev.parent = dev->parent;
28691020aecSBhargav Raviprakash pfsm->chip_id = tps->chip_id;
287a0df3ef0SJulien Panis
288a0df3ef0SJulien Panis for (i = 0 ; i < pdev->num_resources ; i++) {
289a0df3ef0SJulien Panis irq = platform_get_irq_byname(pdev, pdev->resource[i].name);
290a0df3ef0SJulien Panis if (irq < 0)
2911314e122SRuan Jinjie return irq;
292a0df3ef0SJulien Panis
293a0df3ef0SJulien Panis ret = devm_request_threaded_irq(dev, irq, NULL,
294a0df3ef0SJulien Panis tps6594_pfsm_isr, IRQF_ONESHOT,
295a0df3ef0SJulien Panis pdev->resource[i].name, pdev);
296a0df3ef0SJulien Panis if (ret)
297a0df3ef0SJulien Panis return dev_err_probe(dev, ret, "Failed to request irq\n");
298a0df3ef0SJulien Panis }
299a0df3ef0SJulien Panis
300a0df3ef0SJulien Panis platform_set_drvdata(pdev, pfsm);
301a0df3ef0SJulien Panis
302a0df3ef0SJulien Panis return misc_register(&pfsm->miscdev);
303a0df3ef0SJulien Panis }
304a0df3ef0SJulien Panis
tps6594_pfsm_remove(struct platform_device * pdev)3056dab711dSUwe Kleine-König static void tps6594_pfsm_remove(struct platform_device *pdev)
306a0df3ef0SJulien Panis {
307a0df3ef0SJulien Panis struct tps6594_pfsm *pfsm = platform_get_drvdata(pdev);
308a0df3ef0SJulien Panis
309a0df3ef0SJulien Panis misc_deregister(&pfsm->miscdev);
310a0df3ef0SJulien Panis }
311a0df3ef0SJulien Panis
312a0df3ef0SJulien Panis static struct platform_driver tps6594_pfsm_driver = {
313a0df3ef0SJulien Panis .driver = {
314a0df3ef0SJulien Panis .name = "tps6594-pfsm",
315a0df3ef0SJulien Panis },
316a0df3ef0SJulien Panis .probe = tps6594_pfsm_probe,
317*e70140baSLinus Torvalds .remove = tps6594_pfsm_remove,
318a0df3ef0SJulien Panis };
319a0df3ef0SJulien Panis
320a0df3ef0SJulien Panis module_platform_driver(tps6594_pfsm_driver);
321a0df3ef0SJulien Panis
322a0df3ef0SJulien Panis MODULE_ALIAS("platform:tps6594-pfsm");
323a0df3ef0SJulien Panis MODULE_AUTHOR("Julien Panis <jpanis@baylibre.com>");
324a0df3ef0SJulien Panis MODULE_DESCRIPTION("TPS6594 Pre-configurable Finite State Machine Driver");
325a0df3ef0SJulien Panis MODULE_LICENSE("GPL");
326