blob: 0ed9669d95fa18d8c6199ca1c06a52f2140932ae [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 Iyer611b7592010-12-13 09:33:15 +0530132static struct mfd_cell tc3589x_dev_gpio[] = {
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 Iyer611b7592010-12-13 09:33:15 +0530243static int __devinit tc3589x_device_init(struct tc3589x *tc3589x)
244{
245 int ret = 0;
246 unsigned int blocks = tc3589x->pdata->block;
247
248 if (blocks & TC3589x_BLOCK_GPIO) {
249 ret = mfd_add_devices(tc3589x->dev, -1, tc3589x_dev_gpio,
250 ARRAY_SIZE(tc3589x_dev_gpio), NULL,
251 tc3589x->irq_base);
252 if (ret) {
253 dev_err(tc3589x->dev, "failed to add gpio child\n");
254 return ret;
255 }
256 dev_info(tc3589x->dev, "added gpio block\n");
257 }
258
259 return ret;
260
261}
262
Sundar Iyer20406eb2010-12-13 09:33:14 +0530263static int __devinit tc3589x_probe(struct i2c_client *i2c,
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200264 const struct i2c_device_id *id)
265{
Sundar Iyer20406eb2010-12-13 09:33:14 +0530266 struct tc3589x_platform_data *pdata = i2c->dev.platform_data;
267 struct tc3589x *tc3589x;
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200268 int ret;
269
270 if (!i2c_check_functionality(i2c->adapter, I2C_FUNC_SMBUS_BYTE_DATA
271 | I2C_FUNC_SMBUS_I2C_BLOCK))
272 return -EIO;
273
Sundar Iyer20406eb2010-12-13 09:33:14 +0530274 tc3589x = kzalloc(sizeof(struct tc3589x), GFP_KERNEL);
275 if (!tc3589x)
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200276 return -ENOMEM;
277
Sundar Iyer20406eb2010-12-13 09:33:14 +0530278 mutex_init(&tc3589x->lock);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200279
Sundar Iyer20406eb2010-12-13 09:33:14 +0530280 tc3589x->dev = &i2c->dev;
281 tc3589x->i2c = i2c;
282 tc3589x->pdata = pdata;
283 tc3589x->irq_base = pdata->irq_base;
284 tc3589x->num_gpio = id->driver_data;
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200285
Sundar Iyer20406eb2010-12-13 09:33:14 +0530286 i2c_set_clientdata(i2c, tc3589x);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200287
Sundar Iyer20406eb2010-12-13 09:33:14 +0530288 ret = tc3589x_chip_init(tc3589x);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200289 if (ret)
290 goto out_free;
291
Sundar Iyer20406eb2010-12-13 09:33:14 +0530292 ret = tc3589x_irq_init(tc3589x);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200293 if (ret)
294 goto out_free;
295
Sundar Iyer20406eb2010-12-13 09:33:14 +0530296 ret = request_threaded_irq(tc3589x->i2c->irq, NULL, tc3589x_irq,
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200297 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
Sundar Iyer20406eb2010-12-13 09:33:14 +0530298 "tc3589x", tc3589x);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200299 if (ret) {
Sundar Iyer20406eb2010-12-13 09:33:14 +0530300 dev_err(tc3589x->dev, "failed to request IRQ: %d\n", ret);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200301 goto out_removeirq;
302 }
303
Sundar Iyer611b7592010-12-13 09:33:15 +0530304 ret = tc3589x_device_init(tc3589x);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200305 if (ret) {
Sundar Iyer611b7592010-12-13 09:33:15 +0530306 dev_err(tc3589x->dev, "failed to add child devices\n");
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200307 goto out_freeirq;
308 }
309
310 return 0;
311
312out_freeirq:
Sundar Iyer20406eb2010-12-13 09:33:14 +0530313 free_irq(tc3589x->i2c->irq, tc3589x);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200314out_removeirq:
Sundar Iyer20406eb2010-12-13 09:33:14 +0530315 tc3589x_irq_remove(tc3589x);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200316out_free:
Sundar Iyer20406eb2010-12-13 09:33:14 +0530317 kfree(tc3589x);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200318 return ret;
319}
320
Sundar Iyer20406eb2010-12-13 09:33:14 +0530321static int __devexit tc3589x_remove(struct i2c_client *client)
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200322{
Sundar Iyer20406eb2010-12-13 09:33:14 +0530323 struct tc3589x *tc3589x = i2c_get_clientdata(client);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200324
Sundar Iyer20406eb2010-12-13 09:33:14 +0530325 mfd_remove_devices(tc3589x->dev);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200326
Sundar Iyer20406eb2010-12-13 09:33:14 +0530327 free_irq(tc3589x->i2c->irq, tc3589x);
328 tc3589x_irq_remove(tc3589x);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200329
Sundar Iyer20406eb2010-12-13 09:33:14 +0530330 kfree(tc3589x);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200331
332 return 0;
333}
334
Sundar Iyer20406eb2010-12-13 09:33:14 +0530335static const struct i2c_device_id tc3589x_id[] = {
336 { "tc3589x", 24 },
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200337 { }
338};
Sundar Iyer20406eb2010-12-13 09:33:14 +0530339MODULE_DEVICE_TABLE(i2c, tc3589x_id);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200340
Sundar Iyer20406eb2010-12-13 09:33:14 +0530341static struct i2c_driver tc3589x_driver = {
342 .driver.name = "tc3589x",
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200343 .driver.owner = THIS_MODULE,
Sundar Iyer20406eb2010-12-13 09:33:14 +0530344 .probe = tc3589x_probe,
345 .remove = __devexit_p(tc3589x_remove),
346 .id_table = tc3589x_id,
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200347};
348
Sundar Iyer20406eb2010-12-13 09:33:14 +0530349static int __init tc3589x_init(void)
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200350{
Sundar Iyer20406eb2010-12-13 09:33:14 +0530351 return i2c_add_driver(&tc3589x_driver);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200352}
Sundar Iyer20406eb2010-12-13 09:33:14 +0530353subsys_initcall(tc3589x_init);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200354
Sundar Iyer20406eb2010-12-13 09:33:14 +0530355static void __exit tc3589x_exit(void)
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200356{
Sundar Iyer20406eb2010-12-13 09:33:14 +0530357 i2c_del_driver(&tc3589x_driver);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200358}
Sundar Iyer20406eb2010-12-13 09:33:14 +0530359module_exit(tc3589x_exit);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200360
361MODULE_LICENSE("GPL v2");
Sundar Iyer20406eb2010-12-13 09:33:14 +0530362MODULE_DESCRIPTION("TC3589x MFD core driver");
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200363MODULE_AUTHOR("Hanumath Prasad, Rabin Vincent");