Linux Audio

Check our new training course

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