blob: 7deff53181d168d3d68fd8ad20b92691473ebcbf [file] [log] [blame]
Rabin Vincentb4ecd322010-05-10 23:39:47 +02001/*
2 * Copyright (C) ST-Ericsson SA 2010
3 *
4 * License Terms: GNU General Public License, version 2
5 * Author: Hanumath Prasad <hanumath.prasad@stericsson.com> for ST-Ericsson
6 * Author: Rabin Vincent <rabin.vincent@stericsson.com> for ST-Ericsson
7 */
8
9#include <linux/module.h>
10#include <linux/interrupt.h>
11#include <linux/irq.h>
12#include <linux/slab.h>
13#include <linux/i2c.h>
14#include <linux/mfd/core.h>
Sundar Iyerc6eda6c2010-12-13 09:33:12 +053015#include <linux/mfd/tc3589x.h>
Rabin Vincentb4ecd322010-05-10 23:39:47 +020016
17/**
Sundar Iyer20406eb2010-12-13 09:33:14 +053018 * tc3589x_reg_read() - read a single TC3589x register
19 * @tc3589x: Device to read from
Rabin Vincentb4ecd322010-05-10 23:39:47 +020020 * @reg: Register to read
21 */
Sundar Iyer20406eb2010-12-13 09:33:14 +053022int tc3589x_reg_read(struct tc3589x *tc3589x, u8 reg)
Rabin Vincentb4ecd322010-05-10 23:39:47 +020023{
24 int ret;
25
Sundar Iyer20406eb2010-12-13 09:33:14 +053026 ret = i2c_smbus_read_byte_data(tc3589x->i2c, reg);
Rabin Vincentb4ecd322010-05-10 23:39:47 +020027 if (ret < 0)
Sundar Iyer20406eb2010-12-13 09:33:14 +053028 dev_err(tc3589x->dev, "failed to read reg %#x: %d\n",
Rabin Vincentb4ecd322010-05-10 23:39:47 +020029 reg, ret);
30
31 return ret;
32}
Sundar Iyer20406eb2010-12-13 09:33:14 +053033EXPORT_SYMBOL_GPL(tc3589x_reg_read);
Rabin Vincentb4ecd322010-05-10 23:39:47 +020034
35/**
Sundar Iyer20406eb2010-12-13 09:33:14 +053036 * tc3589x_reg_read() - write a single TC3589x register
37 * @tc3589x: Device to write to
Rabin Vincentb4ecd322010-05-10 23:39:47 +020038 * @reg: Register to read
39 * @data: Value to write
40 */
Sundar Iyer20406eb2010-12-13 09:33:14 +053041int tc3589x_reg_write(struct tc3589x *tc3589x, u8 reg, u8 data)
Rabin Vincentb4ecd322010-05-10 23:39:47 +020042{
43 int ret;
44
Sundar Iyer20406eb2010-12-13 09:33:14 +053045 ret = i2c_smbus_write_byte_data(tc3589x->i2c, reg, data);
Rabin Vincentb4ecd322010-05-10 23:39:47 +020046 if (ret < 0)
Sundar Iyer20406eb2010-12-13 09:33:14 +053047 dev_err(tc3589x->dev, "failed to write reg %#x: %d\n",
Rabin Vincentb4ecd322010-05-10 23:39:47 +020048 reg, ret);
49
50 return ret;
51}
Sundar Iyer20406eb2010-12-13 09:33:14 +053052EXPORT_SYMBOL_GPL(tc3589x_reg_write);
Rabin Vincentb4ecd322010-05-10 23:39:47 +020053
54/**
Sundar Iyer20406eb2010-12-13 09:33:14 +053055 * tc3589x_block_read() - read multiple TC3589x registers
56 * @tc3589x: Device to read from
Rabin Vincentb4ecd322010-05-10 23:39:47 +020057 * @reg: First register
58 * @length: Number of registers
59 * @values: Buffer to write to
60 */
Sundar Iyer20406eb2010-12-13 09:33:14 +053061int tc3589x_block_read(struct tc3589x *tc3589x, u8 reg, u8 length, u8 *values)
Rabin Vincentb4ecd322010-05-10 23:39:47 +020062{
63 int ret;
64
Sundar Iyer20406eb2010-12-13 09:33:14 +053065 ret = i2c_smbus_read_i2c_block_data(tc3589x->i2c, reg, length, values);
Rabin Vincentb4ecd322010-05-10 23:39:47 +020066 if (ret < 0)
Sundar Iyer20406eb2010-12-13 09:33:14 +053067 dev_err(tc3589x->dev, "failed to read regs %#x: %d\n",
Rabin Vincentb4ecd322010-05-10 23:39:47 +020068 reg, ret);
69
70 return ret;
71}
Sundar Iyer20406eb2010-12-13 09:33:14 +053072EXPORT_SYMBOL_GPL(tc3589x_block_read);
Rabin Vincentb4ecd322010-05-10 23:39:47 +020073
74/**
Sundar Iyer20406eb2010-12-13 09:33:14 +053075 * tc3589x_block_write() - write multiple TC3589x registers
76 * @tc3589x: Device to write to
Rabin Vincentb4ecd322010-05-10 23:39:47 +020077 * @reg: First register
78 * @length: Number of registers
79 * @values: Values to write
80 */
Sundar Iyer20406eb2010-12-13 09:33:14 +053081int tc3589x_block_write(struct tc3589x *tc3589x, u8 reg, u8 length,
Rabin Vincentb4ecd322010-05-10 23:39:47 +020082 const u8 *values)
83{
84 int ret;
85
Sundar Iyer20406eb2010-12-13 09:33:14 +053086 ret = i2c_smbus_write_i2c_block_data(tc3589x->i2c, reg, length,
Rabin Vincentb4ecd322010-05-10 23:39:47 +020087 values);
88 if (ret < 0)
Sundar Iyer20406eb2010-12-13 09:33:14 +053089 dev_err(tc3589x->dev, "failed to write regs %#x: %d\n",
Rabin Vincentb4ecd322010-05-10 23:39:47 +020090 reg, ret);
91
92 return ret;
93}
Sundar Iyer20406eb2010-12-13 09:33:14 +053094EXPORT_SYMBOL_GPL(tc3589x_block_write);
Rabin Vincentb4ecd322010-05-10 23:39:47 +020095
96/**
Sundar Iyer20406eb2010-12-13 09:33:14 +053097 * tc3589x_set_bits() - set the value of a bitfield in a TC3589x register
98 * @tc3589x: Device to write to
Rabin Vincentb4ecd322010-05-10 23:39:47 +020099 * @reg: Register to write
100 * @mask: Mask of bits to set
101 * @values: Value to set
102 */
Sundar Iyer20406eb2010-12-13 09:33:14 +0530103int tc3589x_set_bits(struct tc3589x *tc3589x, u8 reg, u8 mask, u8 val)
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200104{
105 int ret;
106
Sundar Iyer20406eb2010-12-13 09:33:14 +0530107 mutex_lock(&tc3589x->lock);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200108
Sundar Iyer20406eb2010-12-13 09:33:14 +0530109 ret = tc3589x_reg_read(tc3589x, reg);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200110 if (ret < 0)
111 goto out;
112
113 ret &= ~mask;
114 ret |= val;
115
Sundar Iyer20406eb2010-12-13 09:33:14 +0530116 ret = tc3589x_reg_write(tc3589x, reg, ret);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200117
118out:
Sundar Iyer20406eb2010-12-13 09:33:14 +0530119 mutex_unlock(&tc3589x->lock);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200120 return ret;
121}
Sundar Iyer20406eb2010-12-13 09:33:14 +0530122EXPORT_SYMBOL_GPL(tc3589x_set_bits);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200123
124static struct resource gpio_resources[] = {
125 {
Sundar Iyer20406eb2010-12-13 09:33:14 +0530126 .start = TC3589x_INT_GPIIRQ,
127 .end = TC3589x_INT_GPIIRQ,
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200128 .flags = IORESOURCE_IRQ,
129 },
130};
131
Sundar Iyer20406eb2010-12-13 09:33:14 +0530132static struct mfd_cell tc3589x_devs[] = {
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200133 {
Sundar Iyer20406eb2010-12-13 09:33:14 +0530134 .name = "tc3589x-gpio",
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200135 .num_resources = ARRAY_SIZE(gpio_resources),
136 .resources = &gpio_resources[0],
137 },
138};
139
Sundar Iyer20406eb2010-12-13 09:33:14 +0530140static irqreturn_t tc3589x_irq(int irq, void *data)
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200141{
Sundar Iyer20406eb2010-12-13 09:33:14 +0530142 struct tc3589x *tc3589x = data;
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200143 int status;
144
Sundar Iyer20406eb2010-12-13 09:33:14 +0530145 status = tc3589x_reg_read(tc3589x, TC3589x_IRQST);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200146 if (status < 0)
147 return IRQ_NONE;
148
149 while (status) {
150 int bit = __ffs(status);
151
Sundar Iyer20406eb2010-12-13 09:33:14 +0530152 handle_nested_irq(tc3589x->irq_base + bit);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200153 status &= ~(1 << bit);
154 }
155
156 /*
157 * A dummy read or write (to any register) appears to be necessary to
158 * have the last interrupt clear (for example, GPIO IC write) take
159 * effect.
160 */
Sundar Iyer20406eb2010-12-13 09:33:14 +0530161 tc3589x_reg_read(tc3589x, TC3589x_IRQST);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200162
163 return IRQ_HANDLED;
164}
165
Sundar Iyer20406eb2010-12-13 09:33:14 +0530166static void tc3589x_irq_dummy(unsigned int irq)
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200167{
168 /* No mask/unmask at this level */
169}
170
Sundar Iyer20406eb2010-12-13 09:33:14 +0530171static struct irq_chip tc3589x_irq_chip = {
172 .name = "tc3589x",
173 .mask = tc3589x_irq_dummy,
174 .unmask = tc3589x_irq_dummy,
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200175};
176
Sundar Iyer20406eb2010-12-13 09:33:14 +0530177static int tc3589x_irq_init(struct tc3589x *tc3589x)
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200178{
Sundar Iyer20406eb2010-12-13 09:33:14 +0530179 int base = tc3589x->irq_base;
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200180 int irq;
181
Sundar Iyer20406eb2010-12-13 09:33:14 +0530182 for (irq = base; irq < base + TC3589x_NR_INTERNAL_IRQS; irq++) {
183 set_irq_chip_data(irq, tc3589x);
184 set_irq_chip_and_handler(irq, &tc3589x_irq_chip,
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200185 handle_edge_irq);
186 set_irq_nested_thread(irq, 1);
187#ifdef CONFIG_ARM
188 set_irq_flags(irq, IRQF_VALID);
189#else
190 set_irq_noprobe(irq);
191#endif
192 }
193
194 return 0;
195}
196
Sundar Iyer20406eb2010-12-13 09:33:14 +0530197static void tc3589x_irq_remove(struct tc3589x *tc3589x)
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200198{
Sundar Iyer20406eb2010-12-13 09:33:14 +0530199 int base = tc3589x->irq_base;
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200200 int irq;
201
Sundar Iyer20406eb2010-12-13 09:33:14 +0530202 for (irq = base; irq < base + TC3589x_NR_INTERNAL_IRQS; irq++) {
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200203#ifdef CONFIG_ARM
204 set_irq_flags(irq, 0);
205#endif
206 set_irq_chip_and_handler(irq, NULL, NULL);
207 set_irq_chip_data(irq, NULL);
208 }
209}
210
Sundar Iyer20406eb2010-12-13 09:33:14 +0530211static int tc3589x_chip_init(struct tc3589x *tc3589x)
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200212{
213 int manf, ver, ret;
214
Sundar Iyer20406eb2010-12-13 09:33:14 +0530215 manf = tc3589x_reg_read(tc3589x, TC3589x_MANFCODE);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200216 if (manf < 0)
217 return manf;
218
Sundar Iyer20406eb2010-12-13 09:33:14 +0530219 ver = tc3589x_reg_read(tc3589x, TC3589x_VERSION);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200220 if (ver < 0)
221 return ver;
222
Sundar Iyer20406eb2010-12-13 09:33:14 +0530223 if (manf != TC3589x_MANFCODE_MAGIC) {
224 dev_err(tc3589x->dev, "unknown manufacturer: %#x\n", manf);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200225 return -EINVAL;
226 }
227
Sundar Iyer20406eb2010-12-13 09:33:14 +0530228 dev_info(tc3589x->dev, "manufacturer: %#x, version: %#x\n", manf, ver);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200229
230 /* Put everything except the IRQ module into reset */
Sundar Iyer20406eb2010-12-13 09:33:14 +0530231 ret = tc3589x_reg_write(tc3589x, TC3589x_RSTCTRL,
232 TC3589x_RSTCTRL_TIMRST
233 | TC3589x_RSTCTRL_ROTRST
234 | TC3589x_RSTCTRL_KBDRST
235 | TC3589x_RSTCTRL_GPIRST);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200236 if (ret < 0)
237 return ret;
238
239 /* Clear the reset interrupt. */
Sundar Iyer20406eb2010-12-13 09:33:14 +0530240 return tc3589x_reg_write(tc3589x, TC3589x_RSTINTCLR, 0x1);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200241}
242
Sundar Iyer20406eb2010-12-13 09:33:14 +0530243static int __devinit tc3589x_probe(struct i2c_client *i2c,
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200244 const struct i2c_device_id *id)
245{
Sundar Iyer20406eb2010-12-13 09:33:14 +0530246 struct tc3589x_platform_data *pdata = i2c->dev.platform_data;
247 struct tc3589x *tc3589x;
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200248 int ret;
249
250 if (!i2c_check_functionality(i2c->adapter, I2C_FUNC_SMBUS_BYTE_DATA
251 | I2C_FUNC_SMBUS_I2C_BLOCK))
252 return -EIO;
253
Sundar Iyer20406eb2010-12-13 09:33:14 +0530254 tc3589x = kzalloc(sizeof(struct tc3589x), GFP_KERNEL);
255 if (!tc3589x)
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200256 return -ENOMEM;
257
Sundar Iyer20406eb2010-12-13 09:33:14 +0530258 mutex_init(&tc3589x->lock);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200259
Sundar Iyer20406eb2010-12-13 09:33:14 +0530260 tc3589x->dev = &i2c->dev;
261 tc3589x->i2c = i2c;
262 tc3589x->pdata = pdata;
263 tc3589x->irq_base = pdata->irq_base;
264 tc3589x->num_gpio = id->driver_data;
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200265
Sundar Iyer20406eb2010-12-13 09:33:14 +0530266 i2c_set_clientdata(i2c, tc3589x);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200267
Sundar Iyer20406eb2010-12-13 09:33:14 +0530268 ret = tc3589x_chip_init(tc3589x);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200269 if (ret)
270 goto out_free;
271
Sundar Iyer20406eb2010-12-13 09:33:14 +0530272 ret = tc3589x_irq_init(tc3589x);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200273 if (ret)
274 goto out_free;
275
Sundar Iyer20406eb2010-12-13 09:33:14 +0530276 ret = request_threaded_irq(tc3589x->i2c->irq, NULL, tc3589x_irq,
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200277 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
Sundar Iyer20406eb2010-12-13 09:33:14 +0530278 "tc3589x", tc3589x);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200279 if (ret) {
Sundar Iyer20406eb2010-12-13 09:33:14 +0530280 dev_err(tc3589x->dev, "failed to request IRQ: %d\n", ret);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200281 goto out_removeirq;
282 }
283
Sundar Iyer20406eb2010-12-13 09:33:14 +0530284 ret = mfd_add_devices(tc3589x->dev, -1, tc3589x_devs,
285 ARRAY_SIZE(tc3589x_devs), NULL,
286 tc3589x->irq_base);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200287 if (ret) {
Sundar Iyer20406eb2010-12-13 09:33:14 +0530288 dev_err(tc3589x->dev, "failed to add children\n");
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200289 goto out_freeirq;
290 }
291
292 return 0;
293
294out_freeirq:
Sundar Iyer20406eb2010-12-13 09:33:14 +0530295 free_irq(tc3589x->i2c->irq, tc3589x);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200296out_removeirq:
Sundar Iyer20406eb2010-12-13 09:33:14 +0530297 tc3589x_irq_remove(tc3589x);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200298out_free:
Sundar Iyer20406eb2010-12-13 09:33:14 +0530299 kfree(tc3589x);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200300 return ret;
301}
302
Sundar Iyer20406eb2010-12-13 09:33:14 +0530303static int __devexit tc3589x_remove(struct i2c_client *client)
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200304{
Sundar Iyer20406eb2010-12-13 09:33:14 +0530305 struct tc3589x *tc3589x = i2c_get_clientdata(client);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200306
Sundar Iyer20406eb2010-12-13 09:33:14 +0530307 mfd_remove_devices(tc3589x->dev);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200308
Sundar Iyer20406eb2010-12-13 09:33:14 +0530309 free_irq(tc3589x->i2c->irq, tc3589x);
310 tc3589x_irq_remove(tc3589x);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200311
Sundar Iyer20406eb2010-12-13 09:33:14 +0530312 kfree(tc3589x);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200313
314 return 0;
315}
316
Sundar Iyer20406eb2010-12-13 09:33:14 +0530317static const struct i2c_device_id tc3589x_id[] = {
318 { "tc3589x", 24 },
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200319 { }
320};
Sundar Iyer20406eb2010-12-13 09:33:14 +0530321MODULE_DEVICE_TABLE(i2c, tc3589x_id);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200322
Sundar Iyer20406eb2010-12-13 09:33:14 +0530323static struct i2c_driver tc3589x_driver = {
324 .driver.name = "tc3589x",
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200325 .driver.owner = THIS_MODULE,
Sundar Iyer20406eb2010-12-13 09:33:14 +0530326 .probe = tc3589x_probe,
327 .remove = __devexit_p(tc3589x_remove),
328 .id_table = tc3589x_id,
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200329};
330
Sundar Iyer20406eb2010-12-13 09:33:14 +0530331static int __init tc3589x_init(void)
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200332{
Sundar Iyer20406eb2010-12-13 09:33:14 +0530333 return i2c_add_driver(&tc3589x_driver);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200334}
Sundar Iyer20406eb2010-12-13 09:33:14 +0530335subsys_initcall(tc3589x_init);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200336
Sundar Iyer20406eb2010-12-13 09:33:14 +0530337static void __exit tc3589x_exit(void)
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200338{
Sundar Iyer20406eb2010-12-13 09:33:14 +0530339 i2c_del_driver(&tc3589x_driver);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200340}
Sundar Iyer20406eb2010-12-13 09:33:14 +0530341module_exit(tc3589x_exit);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200342
343MODULE_LICENSE("GPL v2");
Sundar Iyer20406eb2010-12-13 09:33:14 +0530344MODULE_DESCRIPTION("TC3589x MFD core driver");
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200345MODULE_AUTHOR("Hanumath Prasad, Rabin Vincent");