4 * Support for Intersil ISL6271A voltage regulator
6 * Copyright (C) 2010 Marek Vasut <marek.vasut@gmail.com>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation version 2.
12 * This program is distributed "as is" WITHOUT ANY WARRANTY of any kind,
13 * whether express or implied; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/err.h>
22 #include <linux/platform_device.h>
23 #include <linux/regulator/driver.h>
24 #include <linux/i2c.h>
25 #include <linux/delay.h>
26 #include <linux/slab.h>
28 #define ISL6271A_VOLTAGE_MIN 850000
29 #define ISL6271A_VOLTAGE_MAX 1600000
30 #define ISL6271A_VOLTAGE_STEP 50000
34 struct i2c_client
*client
;
35 struct regulator_dev
*rdev
[3];
39 static int isl6271a_get_voltage(struct regulator_dev
*dev
)
41 struct isl_pmic
*pmic
= rdev_get_drvdata(dev
);
44 mutex_lock(&pmic
->mtx
);
46 idx
= i2c_smbus_read_byte(pmic
->client
);
48 dev_err(&pmic
->client
->dev
, "Error getting voltage\n");
53 /* Convert the data from chip to microvolts */
54 data
= ISL6271A_VOLTAGE_MIN
+ (ISL6271A_VOLTAGE_STEP
* (idx
& 0xf));
57 mutex_unlock(&pmic
->mtx
);
61 static int isl6271a_set_voltage(struct regulator_dev
*dev
,
65 struct isl_pmic
*pmic
= rdev_get_drvdata(dev
);
68 if (minuV
< ISL6271A_VOLTAGE_MIN
|| minuV
> ISL6271A_VOLTAGE_MAX
)
70 if (maxuV
< ISL6271A_VOLTAGE_MIN
|| maxuV
> ISL6271A_VOLTAGE_MAX
)
73 data
= DIV_ROUND_UP(minuV
- ISL6271A_VOLTAGE_MIN
,
74 ISL6271A_VOLTAGE_STEP
);
77 mutex_lock(&pmic
->mtx
);
79 err
= i2c_smbus_write_byte(pmic
->client
, data
);
81 dev_err(&pmic
->client
->dev
, "Error setting voltage\n");
83 mutex_unlock(&pmic
->mtx
);
87 static int isl6271a_list_voltage(struct regulator_dev
*dev
, unsigned selector
)
89 return ISL6271A_VOLTAGE_MIN
+ (ISL6271A_VOLTAGE_STEP
* selector
);
92 static struct regulator_ops isl_core_ops
= {
93 .get_voltage
= isl6271a_get_voltage
,
94 .set_voltage
= isl6271a_set_voltage
,
95 .list_voltage
= isl6271a_list_voltage
,
98 static int isl6271a_get_fixed_voltage(struct regulator_dev
*dev
)
100 int id
= rdev_get_id(dev
);
101 return (id
== 1) ? 1100000 : 1300000;
104 static int isl6271a_list_fixed_voltage(struct regulator_dev
*dev
, unsigned selector
)
106 int id
= rdev_get_id(dev
);
107 return (id
== 1) ? 1100000 : 1300000;
110 static struct regulator_ops isl_fixed_ops
= {
111 .get_voltage
= isl6271a_get_fixed_voltage
,
112 .list_voltage
= isl6271a_list_fixed_voltage
,
115 static struct regulator_desc isl_rd
[] = {
120 .ops
= &isl_core_ops
,
121 .type
= REGULATOR_VOLTAGE
,
122 .owner
= THIS_MODULE
,
127 .ops
= &isl_fixed_ops
,
128 .type
= REGULATOR_VOLTAGE
,
129 .owner
= THIS_MODULE
,
134 .ops
= &isl_fixed_ops
,
135 .type
= REGULATOR_VOLTAGE
,
136 .owner
= THIS_MODULE
,
140 static int __devinit
isl6271a_probe(struct i2c_client
*i2c
,
141 const struct i2c_device_id
*id
)
143 struct regulator_init_data
*init_data
= i2c
->dev
.platform_data
;
144 struct isl_pmic
*pmic
;
147 if (!i2c_check_functionality(i2c
->adapter
, I2C_FUNC_SMBUS_BYTE_DATA
))
151 dev_err(&i2c
->dev
, "no platform data supplied\n");
155 pmic
= kzalloc(sizeof(struct isl_pmic
), GFP_KERNEL
);
161 mutex_init(&pmic
->mtx
);
163 for (i
= 0; i
< 3; i
++) {
164 pmic
->rdev
[i
] = regulator_register(&isl_rd
[i
], &i2c
->dev
,
165 init_data
, pmic
, NULL
);
166 if (IS_ERR(pmic
->rdev
[i
])) {
167 dev_err(&i2c
->dev
, "failed to register %s\n", id
->name
);
168 err
= PTR_ERR(pmic
->rdev
[i
]);
173 i2c_set_clientdata(i2c
, pmic
);
179 regulator_unregister(pmic
->rdev
[i
]);
185 static int __devexit
isl6271a_remove(struct i2c_client
*i2c
)
187 struct isl_pmic
*pmic
= i2c_get_clientdata(i2c
);
190 for (i
= 0; i
< 3; i
++)
191 regulator_unregister(pmic
->rdev
[i
]);
198 static const struct i2c_device_id isl6271a_id
[] = {
199 {.name
= "isl6271a", 0 },
203 MODULE_DEVICE_TABLE(i2c
, isl6271a_id
);
205 static struct i2c_driver isl6271a_i2c_driver
= {
208 .owner
= THIS_MODULE
,
210 .probe
= isl6271a_probe
,
211 .remove
= __devexit_p(isl6271a_remove
),
212 .id_table
= isl6271a_id
,
215 static int __init
isl6271a_init(void)
217 return i2c_add_driver(&isl6271a_i2c_driver
);
220 static void __exit
isl6271a_cleanup(void)
222 i2c_del_driver(&isl6271a_i2c_driver
);
225 subsys_initcall(isl6271a_init
);
226 module_exit(isl6271a_cleanup
);
228 MODULE_AUTHOR("Marek Vasut <marek.vasut@gmail.com>");
229 MODULE_DESCRIPTION("Intersil ISL6271A voltage regulator driver");
230 MODULE_LICENSE("GPL v2");