Linux Audio

Check our new training course

Real-Time Linux with PREEMPT_RT training

Feb 18-20, 2025
Register
Loading...
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * LP8755 High Performance Power Management Unit : System Interface Driver
  4 * (based on rev. 0.26)
  5 * Copyright 2012 Texas Instruments
  6 *
  7 * Author: Daniel(Geon Si) Jeong <daniel.jeong@ti.com>
  8 */
  9
 10#include <linux/module.h>
 11#include <linux/slab.h>
 12#include <linux/i2c.h>
 13#include <linux/err.h>
 14#include <linux/irq.h>
 15#include <linux/interrupt.h>
 
 16#include <linux/regmap.h>
 17#include <linux/uaccess.h>
 18#include <linux/regulator/driver.h>
 19#include <linux/regulator/machine.h>
 20#include <linux/platform_data/lp8755.h>
 21
 22#define LP8755_REG_BUCK0	0x00
 23#define LP8755_REG_BUCK1	0x03
 24#define LP8755_REG_BUCK2	0x04
 25#define LP8755_REG_BUCK3	0x01
 26#define LP8755_REG_BUCK4	0x05
 27#define LP8755_REG_BUCK5	0x02
 28#define LP8755_REG_MAX		0xFF
 29
 30#define LP8755_BUCK_EN_M	BIT(7)
 31#define LP8755_BUCK_LINEAR_OUT_MAX	0x76
 32#define LP8755_BUCK_VOUT_M	0x7F
 33
 34struct lp8755_mphase {
 35	int nreg;
 36	int buck_num[LP8755_BUCK_MAX];
 37};
 38
 39struct lp8755_chip {
 40	struct device *dev;
 41	struct regmap *regmap;
 42	struct lp8755_platform_data *pdata;
 43
 44	int irq;
 45	unsigned int irqmask;
 46
 47	int mphase;
 48	struct regulator_dev *rdev[LP8755_BUCK_MAX];
 49};
 50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 51static int lp8755_buck_enable_time(struct regulator_dev *rdev)
 52{
 53	int ret;
 54	unsigned int regval;
 55	enum lp8755_bucks id = rdev_get_id(rdev);
 
 56
 57	ret = regmap_read(rdev->regmap, 0x12 + id, &regval);
 58	if (ret < 0) {
 59		dev_err(&rdev->dev, "i2c access error %s\n", __func__);
 60		return ret;
 61	}
 62	return (regval & 0xff) * 100;
 63}
 64
 65static int lp8755_buck_set_mode(struct regulator_dev *rdev, unsigned int mode)
 66{
 67	int ret;
 68	unsigned int regbval = 0x0;
 69	enum lp8755_bucks id = rdev_get_id(rdev);
 70	struct lp8755_chip *pchip = rdev_get_drvdata(rdev);
 71
 72	switch (mode) {
 73	case REGULATOR_MODE_FAST:
 74		/* forced pwm mode */
 75		regbval = (0x01 << id);
 76		break;
 77	case REGULATOR_MODE_NORMAL:
 78		/* enable automatic pwm/pfm mode */
 79		ret = regmap_update_bits(rdev->regmap, 0x08 + id, 0x20, 0x00);
 80		if (ret < 0)
 81			goto err_i2c;
 82		break;
 83	case REGULATOR_MODE_IDLE:
 84		/* enable automatic pwm/pfm/lppfm mode */
 85		ret = regmap_update_bits(rdev->regmap, 0x08 + id, 0x20, 0x20);
 86		if (ret < 0)
 87			goto err_i2c;
 88
 89		ret = regmap_update_bits(rdev->regmap, 0x10, 0x01, 0x01);
 90		if (ret < 0)
 91			goto err_i2c;
 92		break;
 93	default:
 94		dev_err(pchip->dev, "Not supported buck mode %s\n", __func__);
 95		/* forced pwm mode */
 96		regbval = (0x01 << id);
 97	}
 98
 99	ret = regmap_update_bits(rdev->regmap, 0x06, 0x01 << id, regbval);
100	if (ret < 0)
101		goto err_i2c;
102	return ret;
103err_i2c:
104	dev_err(&rdev->dev, "i2c access error %s\n", __func__);
105	return ret;
106}
107
108static unsigned int lp8755_buck_get_mode(struct regulator_dev *rdev)
109{
110	int ret;
111	unsigned int regval;
112	enum lp8755_bucks id = rdev_get_id(rdev);
 
113
114	ret = regmap_read(rdev->regmap, 0x06, &regval);
115	if (ret < 0)
116		goto err_i2c;
117
118	/* mode fast means forced pwm mode */
119	if (regval & (0x01 << id))
120		return REGULATOR_MODE_FAST;
121
122	ret = regmap_read(rdev->regmap, 0x08 + id, &regval);
123	if (ret < 0)
124		goto err_i2c;
125
126	/* mode idle means automatic pwm/pfm/lppfm mode */
127	if (regval & 0x20)
128		return REGULATOR_MODE_IDLE;
129
130	/* mode normal means automatic pwm/pfm mode */
131	return REGULATOR_MODE_NORMAL;
132
133err_i2c:
134	dev_err(&rdev->dev, "i2c access error %s\n", __func__);
135	return 0;
136}
137
138static const unsigned int lp8755_buck_ramp_table[] = {
139	30000, 15000, 7500, 3800, 1900, 940, 470, 230
140};
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
141
142static const struct regulator_ops lp8755_buck_ops = {
143	.map_voltage = regulator_map_voltage_linear,
144	.list_voltage = regulator_list_voltage_linear,
145	.set_voltage_sel = regulator_set_voltage_sel_regmap,
146	.get_voltage_sel = regulator_get_voltage_sel_regmap,
147	.enable = regulator_enable_regmap,
148	.disable = regulator_disable_regmap,
149	.is_enabled = regulator_is_enabled_regmap,
150	.enable_time = lp8755_buck_enable_time,
151	.set_mode = lp8755_buck_set_mode,
152	.get_mode = lp8755_buck_get_mode,
153	.set_ramp_delay = regulator_set_ramp_delay_regmap,
154};
155
156#define lp8755_rail(_id) "lp8755_buck"#_id
157#define lp8755_buck_init(_id)\
158{\
159	.constraints = {\
160		.name = lp8755_rail(_id),\
161		.valid_ops_mask = REGULATOR_CHANGE_VOLTAGE,\
162		.min_uV = 500000,\
163		.max_uV = 1675000,\
164	},\
165}
166
167static struct regulator_init_data lp8755_reg_default[LP8755_BUCK_MAX] = {
168	[LP8755_BUCK0] = lp8755_buck_init(0),
169	[LP8755_BUCK1] = lp8755_buck_init(1),
170	[LP8755_BUCK2] = lp8755_buck_init(2),
171	[LP8755_BUCK3] = lp8755_buck_init(3),
172	[LP8755_BUCK4] = lp8755_buck_init(4),
173	[LP8755_BUCK5] = lp8755_buck_init(5),
174};
175
176static const struct lp8755_mphase mphase_buck[MPHASE_CONF_MAX] = {
177	{ 3, { LP8755_BUCK0, LP8755_BUCK3, LP8755_BUCK5 } },
178	{ 6, { LP8755_BUCK0, LP8755_BUCK1, LP8755_BUCK2, LP8755_BUCK3,
179	       LP8755_BUCK4, LP8755_BUCK5 } },
180	{ 5, { LP8755_BUCK0, LP8755_BUCK2, LP8755_BUCK3, LP8755_BUCK4,
181	       LP8755_BUCK5} },
182	{ 4, { LP8755_BUCK0, LP8755_BUCK3, LP8755_BUCK4, LP8755_BUCK5} },
183	{ 3, { LP8755_BUCK0, LP8755_BUCK4, LP8755_BUCK5} },
184	{ 2, { LP8755_BUCK0, LP8755_BUCK5} },
185	{ 1, { LP8755_BUCK0} },
186	{ 2, { LP8755_BUCK0, LP8755_BUCK3} },
187	{ 4, { LP8755_BUCK0, LP8755_BUCK2, LP8755_BUCK3, LP8755_BUCK5} },
188};
189
190static int lp8755_init_data(struct lp8755_chip *pchip)
191{
192	unsigned int regval;
193	int ret, icnt, buck_num;
194	struct lp8755_platform_data *pdata = pchip->pdata;
195
196	/* read back  muti-phase configuration */
197	ret = regmap_read(pchip->regmap, 0x3D, &regval);
198	if (ret < 0)
199		goto out_i2c_error;
200	pchip->mphase = regval & 0x0F;
201
202	/* set default data based on multi-phase config */
203	for (icnt = 0; icnt < mphase_buck[pchip->mphase].nreg; icnt++) {
204		buck_num = mphase_buck[pchip->mphase].buck_num[icnt];
205		pdata->buck_data[buck_num] = &lp8755_reg_default[buck_num];
206	}
207	return ret;
208
209out_i2c_error:
210	dev_err(pchip->dev, "i2c access error %s\n", __func__);
211	return ret;
212}
213
214#define lp8755_buck_desc(_id)\
215{\
216	.name = lp8755_rail(_id),\
217	.id   = LP8755_BUCK##_id,\
218	.ops  = &lp8755_buck_ops,\
219	.n_voltages = LP8755_BUCK_LINEAR_OUT_MAX+1,\
220	.uV_step = 10000,\
221	.min_uV = 500000,\
222	.type = REGULATOR_VOLTAGE,\
223	.owner = THIS_MODULE,\
224	.enable_reg = LP8755_REG_BUCK##_id,\
225	.enable_mask = LP8755_BUCK_EN_M,\
226	.vsel_reg = LP8755_REG_BUCK##_id,\
227	.vsel_mask = LP8755_BUCK_VOUT_M,\
228	.ramp_reg = (LP8755_BUCK##_id) + 0x7,\
229	.ramp_mask = 0x7,\
230	.ramp_delay_table = lp8755_buck_ramp_table,\
231	.n_ramp_values = ARRAY_SIZE(lp8755_buck_ramp_table),\
232}
233
234static const struct regulator_desc lp8755_regulators[] = {
235	lp8755_buck_desc(0),
236	lp8755_buck_desc(1),
237	lp8755_buck_desc(2),
238	lp8755_buck_desc(3),
239	lp8755_buck_desc(4),
240	lp8755_buck_desc(5),
241};
242
243static int lp8755_regulator_init(struct lp8755_chip *pchip)
244{
245	int ret, icnt, buck_num;
246	struct lp8755_platform_data *pdata = pchip->pdata;
247	struct regulator_config rconfig = { };
248
249	rconfig.regmap = pchip->regmap;
250	rconfig.dev = pchip->dev;
251	rconfig.driver_data = pchip;
252
253	for (icnt = 0; icnt < mphase_buck[pchip->mphase].nreg; icnt++) {
254		buck_num = mphase_buck[pchip->mphase].buck_num[icnt];
255		rconfig.init_data = pdata->buck_data[buck_num];
256		rconfig.of_node = pchip->dev->of_node;
257		pchip->rdev[buck_num] =
258		    devm_regulator_register(pchip->dev,
259				    &lp8755_regulators[buck_num], &rconfig);
260		if (IS_ERR(pchip->rdev[buck_num])) {
261			ret = PTR_ERR(pchip->rdev[buck_num]);
262			pchip->rdev[buck_num] = NULL;
263			dev_err(pchip->dev, "regulator init failed: buck %d\n",
264				buck_num);
265			return ret;
266		}
267	}
268
269	return 0;
270}
271
272static irqreturn_t lp8755_irq_handler(int irq, void *data)
273{
274	int ret, icnt;
275	unsigned int flag0, flag1;
276	struct lp8755_chip *pchip = data;
277
278	/* read flag0 register */
279	ret = regmap_read(pchip->regmap, 0x0D, &flag0);
280	if (ret < 0)
281		goto err_i2c;
282	/* clear flag register to pull up int. pin */
283	ret = regmap_write(pchip->regmap, 0x0D, 0x00);
284	if (ret < 0)
285		goto err_i2c;
286
287	/* sent power fault detection event to specific regulator */
288	for (icnt = 0; icnt < LP8755_BUCK_MAX; icnt++)
289		if ((flag0 & (0x4 << icnt))
290		    && (pchip->irqmask & (0x04 << icnt))
291		    && (pchip->rdev[icnt] != NULL)) {
 
292			regulator_notifier_call_chain(pchip->rdev[icnt],
293						      LP8755_EVENT_PWR_FAULT,
294						      NULL);
 
295		}
296
297	/* read flag1 register */
298	ret = regmap_read(pchip->regmap, 0x0E, &flag1);
299	if (ret < 0)
300		goto err_i2c;
301	/* clear flag register to pull up int. pin */
302	ret = regmap_write(pchip->regmap, 0x0E, 0x00);
303	if (ret < 0)
304		goto err_i2c;
305
306	/* send OCP event to all regulator devices */
307	if ((flag1 & 0x01) && (pchip->irqmask & 0x01))
308		for (icnt = 0; icnt < LP8755_BUCK_MAX; icnt++)
309			if (pchip->rdev[icnt] != NULL) {
 
310				regulator_notifier_call_chain(pchip->rdev[icnt],
311							      LP8755_EVENT_OCP,
312							      NULL);
 
313			}
314
315	/* send OVP event to all regulator devices */
316	if ((flag1 & 0x02) && (pchip->irqmask & 0x02))
317		for (icnt = 0; icnt < LP8755_BUCK_MAX; icnt++)
318			if (pchip->rdev[icnt] != NULL) {
 
319				regulator_notifier_call_chain(pchip->rdev[icnt],
320							      LP8755_EVENT_OVP,
321							      NULL);
 
322			}
323	return IRQ_HANDLED;
324
325err_i2c:
326	dev_err(pchip->dev, "i2c access error %s\n", __func__);
327	return IRQ_NONE;
328}
329
330static int lp8755_int_config(struct lp8755_chip *pchip)
331{
332	int ret;
333	unsigned int regval;
334
335	if (pchip->irq == 0) {
336		dev_warn(pchip->dev, "not use interrupt : %s\n", __func__);
337		return 0;
338	}
339
340	ret = regmap_read(pchip->regmap, 0x0F, &regval);
341	if (ret < 0) {
342		dev_err(pchip->dev, "i2c access error %s\n", __func__);
343		return ret;
344	}
345
346	pchip->irqmask = regval;
347	return devm_request_threaded_irq(pchip->dev, pchip->irq, NULL,
348					 lp8755_irq_handler,
349					 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
350					 "lp8755-irq", pchip);
351}
352
353static const struct regmap_config lp8755_regmap = {
354	.reg_bits = 8,
355	.val_bits = 8,
356	.max_register = LP8755_REG_MAX,
357};
358
359static int lp8755_probe(struct i2c_client *client)
 
360{
361	int ret, icnt;
362	struct lp8755_chip *pchip;
363	struct lp8755_platform_data *pdata = dev_get_platdata(&client->dev);
364
365	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
366		dev_err(&client->dev, "i2c functionality check fail.\n");
367		return -EOPNOTSUPP;
368	}
369
370	pchip = devm_kzalloc(&client->dev,
371			     sizeof(struct lp8755_chip), GFP_KERNEL);
372	if (!pchip)
373		return -ENOMEM;
374
375	pchip->dev = &client->dev;
376	pchip->regmap = devm_regmap_init_i2c(client, &lp8755_regmap);
377	if (IS_ERR(pchip->regmap)) {
378		ret = PTR_ERR(pchip->regmap);
379		dev_err(&client->dev, "fail to allocate regmap %d\n", ret);
380		return ret;
381	}
382	i2c_set_clientdata(client, pchip);
383
384	if (pdata != NULL) {
385		pchip->pdata = pdata;
386		pchip->mphase = pdata->mphase;
387	} else {
388		pchip->pdata = devm_kzalloc(pchip->dev,
389					    sizeof(struct lp8755_platform_data),
390					    GFP_KERNEL);
391		if (!pchip->pdata)
392			return -ENOMEM;
393		ret = lp8755_init_data(pchip);
394		if (ret < 0) {
395			dev_err(&client->dev, "fail to initialize chip\n");
396			return ret;
397		}
398	}
399
400	ret = lp8755_regulator_init(pchip);
401	if (ret < 0) {
402		dev_err(&client->dev, "fail to initialize regulators\n");
403		goto err;
404	}
405
406	pchip->irq = client->irq;
407	ret = lp8755_int_config(pchip);
408	if (ret < 0) {
409		dev_err(&client->dev, "fail to irq config\n");
410		goto err;
411	}
412
413	return ret;
414
415err:
416	/* output disable */
417	for (icnt = 0; icnt < LP8755_BUCK_MAX; icnt++)
418		regmap_write(pchip->regmap, icnt, 0x00);
419
420	return ret;
421}
422
423static void lp8755_remove(struct i2c_client *client)
424{
425	int icnt;
426	struct lp8755_chip *pchip = i2c_get_clientdata(client);
427
428	for (icnt = 0; icnt < LP8755_BUCK_MAX; icnt++)
429		regmap_write(pchip->regmap, icnt, 0x00);
 
 
430}
431
432static const struct i2c_device_id lp8755_id[] = {
433	{ LP8755_NAME },
434	{}
435};
436
437MODULE_DEVICE_TABLE(i2c, lp8755_id);
438
439static struct i2c_driver lp8755_i2c_driver = {
440	.driver = {
441		   .name = LP8755_NAME,
442		   .probe_type = PROBE_PREFER_ASYNCHRONOUS,
443		   },
444	.probe = lp8755_probe,
445	.remove = lp8755_remove,
446	.id_table = lp8755_id,
447};
448
449static int __init lp8755_init(void)
450{
451	return i2c_add_driver(&lp8755_i2c_driver);
452}
453
454subsys_initcall(lp8755_init);
455
456static void __exit lp8755_exit(void)
457{
458	i2c_del_driver(&lp8755_i2c_driver);
459}
460
461module_exit(lp8755_exit);
462
463MODULE_DESCRIPTION("Texas Instruments lp8755 driver");
464MODULE_AUTHOR("Daniel Jeong <daniel.jeong@ti.com>");
465MODULE_LICENSE("GPL v2");
v5.4
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * LP8755 High Performance Power Management Unit : System Interface Driver
  4 * (based on rev. 0.26)
  5 * Copyright 2012 Texas Instruments
  6 *
  7 * Author: Daniel(Geon Si) Jeong <daniel.jeong@ti.com>
  8 */
  9
 10#include <linux/module.h>
 11#include <linux/slab.h>
 12#include <linux/i2c.h>
 13#include <linux/err.h>
 14#include <linux/irq.h>
 15#include <linux/interrupt.h>
 16#include <linux/gpio.h>
 17#include <linux/regmap.h>
 18#include <linux/uaccess.h>
 19#include <linux/regulator/driver.h>
 20#include <linux/regulator/machine.h>
 21#include <linux/platform_data/lp8755.h>
 22
 23#define LP8755_REG_BUCK0	0x00
 24#define LP8755_REG_BUCK1	0x03
 25#define LP8755_REG_BUCK2	0x04
 26#define LP8755_REG_BUCK3	0x01
 27#define LP8755_REG_BUCK4	0x05
 28#define LP8755_REG_BUCK5	0x02
 29#define LP8755_REG_MAX		0xFF
 30
 31#define LP8755_BUCK_EN_M	BIT(7)
 32#define LP8755_BUCK_LINEAR_OUT_MAX	0x76
 33#define LP8755_BUCK_VOUT_M	0x7F
 34
 35struct lp8755_mphase {
 36	int nreg;
 37	int buck_num[LP8755_BUCK_MAX];
 38};
 39
 40struct lp8755_chip {
 41	struct device *dev;
 42	struct regmap *regmap;
 43	struct lp8755_platform_data *pdata;
 44
 45	int irq;
 46	unsigned int irqmask;
 47
 48	int mphase;
 49	struct regulator_dev *rdev[LP8755_BUCK_MAX];
 50};
 51
 52/**
 53 *lp8755_read : read a single register value from lp8755.
 54 *@pchip : device to read from
 55 *@reg   : register to read from
 56 *@val   : pointer to store read value
 57 */
 58static int lp8755_read(struct lp8755_chip *pchip, unsigned int reg,
 59		       unsigned int *val)
 60{
 61	return regmap_read(pchip->regmap, reg, val);
 62}
 63
 64/**
 65 *lp8755_write : write a single register value to lp8755.
 66 *@pchip : device to write to
 67 *@reg   : register to write to
 68 *@val   : value to be written
 69 */
 70static int lp8755_write(struct lp8755_chip *pchip, unsigned int reg,
 71			unsigned int val)
 72{
 73	return regmap_write(pchip->regmap, reg, val);
 74}
 75
 76/**
 77 *lp8755_update_bits : set the values of bit fields in lp8755 register.
 78 *@pchip : device to read from
 79 *@reg   : register to update
 80 *@mask  : bitmask to be changed
 81 *@val   : value for bitmask
 82 */
 83static int lp8755_update_bits(struct lp8755_chip *pchip, unsigned int reg,
 84			      unsigned int mask, unsigned int val)
 85{
 86	return regmap_update_bits(pchip->regmap, reg, mask, val);
 87}
 88
 89static int lp8755_buck_enable_time(struct regulator_dev *rdev)
 90{
 91	int ret;
 92	unsigned int regval;
 93	enum lp8755_bucks id = rdev_get_id(rdev);
 94	struct lp8755_chip *pchip = rdev_get_drvdata(rdev);
 95
 96	ret = lp8755_read(pchip, 0x12 + id, &regval);
 97	if (ret < 0) {
 98		dev_err(pchip->dev, "i2c access error %s\n", __func__);
 99		return ret;
100	}
101	return (regval & 0xff) * 100;
102}
103
104static int lp8755_buck_set_mode(struct regulator_dev *rdev, unsigned int mode)
105{
106	int ret;
107	unsigned int regbval = 0x0;
108	enum lp8755_bucks id = rdev_get_id(rdev);
109	struct lp8755_chip *pchip = rdev_get_drvdata(rdev);
110
111	switch (mode) {
112	case REGULATOR_MODE_FAST:
113		/* forced pwm mode */
114		regbval = (0x01 << id);
115		break;
116	case REGULATOR_MODE_NORMAL:
117		/* enable automatic pwm/pfm mode */
118		ret = lp8755_update_bits(pchip, 0x08 + id, 0x20, 0x00);
119		if (ret < 0)
120			goto err_i2c;
121		break;
122	case REGULATOR_MODE_IDLE:
123		/* enable automatic pwm/pfm/lppfm mode */
124		ret = lp8755_update_bits(pchip, 0x08 + id, 0x20, 0x20);
125		if (ret < 0)
126			goto err_i2c;
127
128		ret = lp8755_update_bits(pchip, 0x10, 0x01, 0x01);
129		if (ret < 0)
130			goto err_i2c;
131		break;
132	default:
133		dev_err(pchip->dev, "Not supported buck mode %s\n", __func__);
134		/* forced pwm mode */
135		regbval = (0x01 << id);
136	}
137
138	ret = lp8755_update_bits(pchip, 0x06, 0x01 << id, regbval);
139	if (ret < 0)
140		goto err_i2c;
141	return ret;
142err_i2c:
143	dev_err(pchip->dev, "i2c access error %s\n", __func__);
144	return ret;
145}
146
147static unsigned int lp8755_buck_get_mode(struct regulator_dev *rdev)
148{
149	int ret;
150	unsigned int regval;
151	enum lp8755_bucks id = rdev_get_id(rdev);
152	struct lp8755_chip *pchip = rdev_get_drvdata(rdev);
153
154	ret = lp8755_read(pchip, 0x06, &regval);
155	if (ret < 0)
156		goto err_i2c;
157
158	/* mode fast means forced pwm mode */
159	if (regval & (0x01 << id))
160		return REGULATOR_MODE_FAST;
161
162	ret = lp8755_read(pchip, 0x08 + id, &regval);
163	if (ret < 0)
164		goto err_i2c;
165
166	/* mode idle means automatic pwm/pfm/lppfm mode */
167	if (regval & 0x20)
168		return REGULATOR_MODE_IDLE;
169
170	/* mode normal means automatic pwm/pfm mode */
171	return REGULATOR_MODE_NORMAL;
172
173err_i2c:
174	dev_err(pchip->dev, "i2c access error %s\n", __func__);
175	return 0;
176}
177
178static int lp8755_buck_set_ramp(struct regulator_dev *rdev, int ramp)
179{
180	int ret;
181	unsigned int regval = 0x00;
182	enum lp8755_bucks id = rdev_get_id(rdev);
183	struct lp8755_chip *pchip = rdev_get_drvdata(rdev);
184
185	/* uV/us */
186	switch (ramp) {
187	case 0 ... 230:
188		regval = 0x07;
189		break;
190	case 231 ... 470:
191		regval = 0x06;
192		break;
193	case 471 ... 940:
194		regval = 0x05;
195		break;
196	case 941 ... 1900:
197		regval = 0x04;
198		break;
199	case 1901 ... 3800:
200		regval = 0x03;
201		break;
202	case 3801 ... 7500:
203		regval = 0x02;
204		break;
205	case 7501 ... 15000:
206		regval = 0x01;
207		break;
208	case 15001 ... 30000:
209		regval = 0x00;
210		break;
211	default:
212		dev_err(pchip->dev,
213			"Not supported ramp value %d %s\n", ramp, __func__);
214		return -EINVAL;
215	}
216
217	ret = lp8755_update_bits(pchip, 0x07 + id, 0x07, regval);
218	if (ret < 0)
219		goto err_i2c;
220	return ret;
221err_i2c:
222	dev_err(pchip->dev, "i2c access error %s\n", __func__);
223	return ret;
224}
225
226static const struct regulator_ops lp8755_buck_ops = {
227	.map_voltage = regulator_map_voltage_linear,
228	.list_voltage = regulator_list_voltage_linear,
229	.set_voltage_sel = regulator_set_voltage_sel_regmap,
230	.get_voltage_sel = regulator_get_voltage_sel_regmap,
231	.enable = regulator_enable_regmap,
232	.disable = regulator_disable_regmap,
233	.is_enabled = regulator_is_enabled_regmap,
234	.enable_time = lp8755_buck_enable_time,
235	.set_mode = lp8755_buck_set_mode,
236	.get_mode = lp8755_buck_get_mode,
237	.set_ramp_delay = lp8755_buck_set_ramp,
238};
239
240#define lp8755_rail(_id) "lp8755_buck"#_id
241#define lp8755_buck_init(_id)\
242{\
243	.constraints = {\
244		.name = lp8755_rail(_id),\
245		.valid_ops_mask = REGULATOR_CHANGE_VOLTAGE,\
246		.min_uV = 500000,\
247		.max_uV = 1675000,\
248	},\
249}
250
251static struct regulator_init_data lp8755_reg_default[LP8755_BUCK_MAX] = {
252	[LP8755_BUCK0] = lp8755_buck_init(0),
253	[LP8755_BUCK1] = lp8755_buck_init(1),
254	[LP8755_BUCK2] = lp8755_buck_init(2),
255	[LP8755_BUCK3] = lp8755_buck_init(3),
256	[LP8755_BUCK4] = lp8755_buck_init(4),
257	[LP8755_BUCK5] = lp8755_buck_init(5),
258};
259
260static const struct lp8755_mphase mphase_buck[MPHASE_CONF_MAX] = {
261	{ 3, { LP8755_BUCK0, LP8755_BUCK3, LP8755_BUCK5 } },
262	{ 6, { LP8755_BUCK0, LP8755_BUCK1, LP8755_BUCK2, LP8755_BUCK3,
263	       LP8755_BUCK4, LP8755_BUCK5 } },
264	{ 5, { LP8755_BUCK0, LP8755_BUCK2, LP8755_BUCK3, LP8755_BUCK4,
265	       LP8755_BUCK5} },
266	{ 4, { LP8755_BUCK0, LP8755_BUCK3, LP8755_BUCK4, LP8755_BUCK5} },
267	{ 3, { LP8755_BUCK0, LP8755_BUCK4, LP8755_BUCK5} },
268	{ 2, { LP8755_BUCK0, LP8755_BUCK5} },
269	{ 1, { LP8755_BUCK0} },
270	{ 2, { LP8755_BUCK0, LP8755_BUCK3} },
271	{ 4, { LP8755_BUCK0, LP8755_BUCK2, LP8755_BUCK3, LP8755_BUCK5} },
272};
273
274static int lp8755_init_data(struct lp8755_chip *pchip)
275{
276	unsigned int regval;
277	int ret, icnt, buck_num;
278	struct lp8755_platform_data *pdata = pchip->pdata;
279
280	/* read back  muti-phase configuration */
281	ret = lp8755_read(pchip, 0x3D, &regval);
282	if (ret < 0)
283		goto out_i2c_error;
284	pchip->mphase = regval & 0x0F;
285
286	/* set default data based on multi-phase config */
287	for (icnt = 0; icnt < mphase_buck[pchip->mphase].nreg; icnt++) {
288		buck_num = mphase_buck[pchip->mphase].buck_num[icnt];
289		pdata->buck_data[buck_num] = &lp8755_reg_default[buck_num];
290	}
291	return ret;
292
293out_i2c_error:
294	dev_err(pchip->dev, "i2c access error %s\n", __func__);
295	return ret;
296}
297
298#define lp8755_buck_desc(_id)\
299{\
300	.name = lp8755_rail(_id),\
301	.id   = LP8755_BUCK##_id,\
302	.ops  = &lp8755_buck_ops,\
303	.n_voltages = LP8755_BUCK_LINEAR_OUT_MAX+1,\
304	.uV_step = 10000,\
305	.min_uV = 500000,\
306	.type = REGULATOR_VOLTAGE,\
307	.owner = THIS_MODULE,\
308	.enable_reg = LP8755_REG_BUCK##_id,\
309	.enable_mask = LP8755_BUCK_EN_M,\
310	.vsel_reg = LP8755_REG_BUCK##_id,\
311	.vsel_mask = LP8755_BUCK_VOUT_M,\
 
 
 
 
312}
313
314static const struct regulator_desc lp8755_regulators[] = {
315	lp8755_buck_desc(0),
316	lp8755_buck_desc(1),
317	lp8755_buck_desc(2),
318	lp8755_buck_desc(3),
319	lp8755_buck_desc(4),
320	lp8755_buck_desc(5),
321};
322
323static int lp8755_regulator_init(struct lp8755_chip *pchip)
324{
325	int ret, icnt, buck_num;
326	struct lp8755_platform_data *pdata = pchip->pdata;
327	struct regulator_config rconfig = { };
328
329	rconfig.regmap = pchip->regmap;
330	rconfig.dev = pchip->dev;
331	rconfig.driver_data = pchip;
332
333	for (icnt = 0; icnt < mphase_buck[pchip->mphase].nreg; icnt++) {
334		buck_num = mphase_buck[pchip->mphase].buck_num[icnt];
335		rconfig.init_data = pdata->buck_data[buck_num];
336		rconfig.of_node = pchip->dev->of_node;
337		pchip->rdev[buck_num] =
338		    devm_regulator_register(pchip->dev,
339				    &lp8755_regulators[buck_num], &rconfig);
340		if (IS_ERR(pchip->rdev[buck_num])) {
341			ret = PTR_ERR(pchip->rdev[buck_num]);
342			pchip->rdev[buck_num] = NULL;
343			dev_err(pchip->dev, "regulator init failed: buck %d\n",
344				buck_num);
345			return ret;
346		}
347	}
348
349	return 0;
350}
351
352static irqreturn_t lp8755_irq_handler(int irq, void *data)
353{
354	int ret, icnt;
355	unsigned int flag0, flag1;
356	struct lp8755_chip *pchip = data;
357
358	/* read flag0 register */
359	ret = lp8755_read(pchip, 0x0D, &flag0);
360	if (ret < 0)
361		goto err_i2c;
362	/* clear flag register to pull up int. pin */
363	ret = lp8755_write(pchip, 0x0D, 0x00);
364	if (ret < 0)
365		goto err_i2c;
366
367	/* sent power fault detection event to specific regulator */
368	for (icnt = 0; icnt < LP8755_BUCK_MAX; icnt++)
369		if ((flag0 & (0x4 << icnt))
370		    && (pchip->irqmask & (0x04 << icnt))
371		    && (pchip->rdev[icnt] != NULL)) {
372			regulator_lock(pchip->rdev[icnt]);
373			regulator_notifier_call_chain(pchip->rdev[icnt],
374						      LP8755_EVENT_PWR_FAULT,
375						      NULL);
376			regulator_unlock(pchip->rdev[icnt]);
377		}
378
379	/* read flag1 register */
380	ret = lp8755_read(pchip, 0x0E, &flag1);
381	if (ret < 0)
382		goto err_i2c;
383	/* clear flag register to pull up int. pin */
384	ret = lp8755_write(pchip, 0x0E, 0x00);
385	if (ret < 0)
386		goto err_i2c;
387
388	/* send OCP event to all regulator devices */
389	if ((flag1 & 0x01) && (pchip->irqmask & 0x01))
390		for (icnt = 0; icnt < LP8755_BUCK_MAX; icnt++)
391			if (pchip->rdev[icnt] != NULL) {
392				regulator_lock(pchip->rdev[icnt]);
393				regulator_notifier_call_chain(pchip->rdev[icnt],
394							      LP8755_EVENT_OCP,
395							      NULL);
396				regulator_unlock(pchip->rdev[icnt]);
397			}
398
399	/* send OVP event to all regulator devices */
400	if ((flag1 & 0x02) && (pchip->irqmask & 0x02))
401		for (icnt = 0; icnt < LP8755_BUCK_MAX; icnt++)
402			if (pchip->rdev[icnt] != NULL) {
403				regulator_lock(pchip->rdev[icnt]);
404				regulator_notifier_call_chain(pchip->rdev[icnt],
405							      LP8755_EVENT_OVP,
406							      NULL);
407				regulator_unlock(pchip->rdev[icnt]);
408			}
409	return IRQ_HANDLED;
410
411err_i2c:
412	dev_err(pchip->dev, "i2c access error %s\n", __func__);
413	return IRQ_NONE;
414}
415
416static int lp8755_int_config(struct lp8755_chip *pchip)
417{
418	int ret;
419	unsigned int regval;
420
421	if (pchip->irq == 0) {
422		dev_warn(pchip->dev, "not use interrupt : %s\n", __func__);
423		return 0;
424	}
425
426	ret = lp8755_read(pchip, 0x0F, &regval);
427	if (ret < 0) {
428		dev_err(pchip->dev, "i2c access error %s\n", __func__);
429		return ret;
430	}
431
432	pchip->irqmask = regval;
433	return devm_request_threaded_irq(pchip->dev, pchip->irq, NULL,
434					 lp8755_irq_handler,
435					 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
436					 "lp8755-irq", pchip);
437}
438
439static const struct regmap_config lp8755_regmap = {
440	.reg_bits = 8,
441	.val_bits = 8,
442	.max_register = LP8755_REG_MAX,
443};
444
445static int lp8755_probe(struct i2c_client *client,
446			const struct i2c_device_id *id)
447{
448	int ret, icnt;
449	struct lp8755_chip *pchip;
450	struct lp8755_platform_data *pdata = dev_get_platdata(&client->dev);
451
452	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
453		dev_err(&client->dev, "i2c functionality check fail.\n");
454		return -EOPNOTSUPP;
455	}
456
457	pchip = devm_kzalloc(&client->dev,
458			     sizeof(struct lp8755_chip), GFP_KERNEL);
459	if (!pchip)
460		return -ENOMEM;
461
462	pchip->dev = &client->dev;
463	pchip->regmap = devm_regmap_init_i2c(client, &lp8755_regmap);
464	if (IS_ERR(pchip->regmap)) {
465		ret = PTR_ERR(pchip->regmap);
466		dev_err(&client->dev, "fail to allocate regmap %d\n", ret);
467		return ret;
468	}
469	i2c_set_clientdata(client, pchip);
470
471	if (pdata != NULL) {
472		pchip->pdata = pdata;
473		pchip->mphase = pdata->mphase;
474	} else {
475		pchip->pdata = devm_kzalloc(pchip->dev,
476					    sizeof(struct lp8755_platform_data),
477					    GFP_KERNEL);
478		if (!pchip->pdata)
479			return -ENOMEM;
480		ret = lp8755_init_data(pchip);
481		if (ret < 0) {
482			dev_err(&client->dev, "fail to initialize chip\n");
483			return ret;
484		}
485	}
486
487	ret = lp8755_regulator_init(pchip);
488	if (ret < 0) {
489		dev_err(&client->dev, "fail to initialize regulators\n");
490		goto err;
491	}
492
493	pchip->irq = client->irq;
494	ret = lp8755_int_config(pchip);
495	if (ret < 0) {
496		dev_err(&client->dev, "fail to irq config\n");
497		goto err;
498	}
499
500	return ret;
501
502err:
503	/* output disable */
504	for (icnt = 0; icnt < LP8755_BUCK_MAX; icnt++)
505		lp8755_write(pchip, icnt, 0x00);
506
507	return ret;
508}
509
510static int lp8755_remove(struct i2c_client *client)
511{
512	int icnt;
513	struct lp8755_chip *pchip = i2c_get_clientdata(client);
514
515	for (icnt = 0; icnt < LP8755_BUCK_MAX; icnt++)
516		lp8755_write(pchip, icnt, 0x00);
517
518	return 0;
519}
520
521static const struct i2c_device_id lp8755_id[] = {
522	{LP8755_NAME, 0},
523	{}
524};
525
526MODULE_DEVICE_TABLE(i2c, lp8755_id);
527
528static struct i2c_driver lp8755_i2c_driver = {
529	.driver = {
530		   .name = LP8755_NAME,
 
531		   },
532	.probe = lp8755_probe,
533	.remove = lp8755_remove,
534	.id_table = lp8755_id,
535};
536
537static int __init lp8755_init(void)
538{
539	return i2c_add_driver(&lp8755_i2c_driver);
540}
541
542subsys_initcall(lp8755_init);
543
544static void __exit lp8755_exit(void)
545{
546	i2c_del_driver(&lp8755_i2c_driver);
547}
548
549module_exit(lp8755_exit);
550
551MODULE_DESCRIPTION("Texas Instruments lp8755 driver");
552MODULE_AUTHOR("Daniel Jeong <daniel.jeong@ti.com>");
553MODULE_LICENSE("GPL v2");