blob: f000d2ed23e0bd725641f276ceda40f88d58e0f1 [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 Iyerbd77efd2010-12-13 09:33:16 +0530145again:
Sundar Iyer20406eb2010-12-13 09:33:14 +0530146 status = tc3589x_reg_read(tc3589x, TC3589x_IRQST);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200147 if (status < 0)
148 return IRQ_NONE;
149
150 while (status) {
151 int bit = __ffs(status);
152
Sundar Iyer20406eb2010-12-13 09:33:14 +0530153 handle_nested_irq(tc3589x->irq_base + bit);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200154 status &= ~(1 << bit);
155 }
156
157 /*
158 * A dummy read or write (to any register) appears to be necessary to
159 * have the last interrupt clear (for example, GPIO IC write) take
Sundar Iyerbd77efd2010-12-13 09:33:16 +0530160 * effect. In such a case, recheck for any interrupt which is still
161 * pending.
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200162 */
Sundar Iyerbd77efd2010-12-13 09:33:16 +0530163 status = tc3589x_reg_read(tc3589x, TC3589x_IRQST);
164 if (status)
165 goto again;
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200166
167 return IRQ_HANDLED;
168}
169
Sundar Iyer20406eb2010-12-13 09:33:14 +0530170static void tc3589x_irq_dummy(unsigned int irq)
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200171{
172 /* No mask/unmask at this level */
173}
174
Sundar Iyer20406eb2010-12-13 09:33:14 +0530175static struct irq_chip tc3589x_irq_chip = {
176 .name = "tc3589x",
177 .mask = tc3589x_irq_dummy,
178 .unmask = tc3589x_irq_dummy,
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200179};
180
Sundar Iyer20406eb2010-12-13 09:33:14 +0530181static int tc3589x_irq_init(struct tc3589x *tc3589x)
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200182{
Sundar Iyer20406eb2010-12-13 09:33:14 +0530183 int base = tc3589x->irq_base;
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200184 int irq;
185
Sundar Iyer20406eb2010-12-13 09:33:14 +0530186 for (irq = base; irq < base + TC3589x_NR_INTERNAL_IRQS; irq++) {
187 set_irq_chip_data(irq, tc3589x);
188 set_irq_chip_and_handler(irq, &tc3589x_irq_chip,
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200189 handle_edge_irq);
190 set_irq_nested_thread(irq, 1);
191#ifdef CONFIG_ARM
192 set_irq_flags(irq, IRQF_VALID);
193#else
194 set_irq_noprobe(irq);
195#endif
196 }
197
198 return 0;
199}
200
Sundar Iyer20406eb2010-12-13 09:33:14 +0530201static void tc3589x_irq_remove(struct tc3589x *tc3589x)
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200202{
Sundar Iyer20406eb2010-12-13 09:33:14 +0530203 int base = tc3589x->irq_base;
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200204 int irq;
205
Sundar Iyer20406eb2010-12-13 09:33:14 +0530206 for (irq = base; irq < base + TC3589x_NR_INTERNAL_IRQS; irq++) {
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200207#ifdef CONFIG_ARM
208 set_irq_flags(irq, 0);
209#endif
210 set_irq_chip_and_handler(irq, NULL, NULL);
211 set_irq_chip_data(irq, NULL);
212 }
213}
214
Sundar Iyer20406eb2010-12-13 09:33:14 +0530215static int tc3589x_chip_init(struct tc3589x *tc3589x)
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200216{
217 int manf, ver, ret;
218
Sundar Iyer20406eb2010-12-13 09:33:14 +0530219 manf = tc3589x_reg_read(tc3589x, TC3589x_MANFCODE);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200220 if (manf < 0)
221 return manf;
222
Sundar Iyer20406eb2010-12-13 09:33:14 +0530223 ver = tc3589x_reg_read(tc3589x, TC3589x_VERSION);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200224 if (ver < 0)
225 return ver;
226
Sundar Iyer20406eb2010-12-13 09:33:14 +0530227 if (manf != TC3589x_MANFCODE_MAGIC) {
228 dev_err(tc3589x->dev, "unknown manufacturer: %#x\n", manf);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200229 return -EINVAL;
230 }
231
Sundar Iyer20406eb2010-12-13 09:33:14 +0530232 dev_info(tc3589x->dev, "manufacturer: %#x, version: %#x\n", manf, ver);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200233
Sundar Iyer523bc382010-12-13 09:33:17 +0530234 /*
235 * Put everything except the IRQ module into reset;
236 * also spare the GPIO module for any pin initialization
237 * done during pre-kernel boot
238 */
Sundar Iyer20406eb2010-12-13 09:33:14 +0530239 ret = tc3589x_reg_write(tc3589x, TC3589x_RSTCTRL,
240 TC3589x_RSTCTRL_TIMRST
241 | TC3589x_RSTCTRL_ROTRST
Sundar Iyer523bc382010-12-13 09:33:17 +0530242 | TC3589x_RSTCTRL_KBDRST);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200243 if (ret < 0)
244 return ret;
245
246 /* Clear the reset interrupt. */
Sundar Iyer20406eb2010-12-13 09:33:14 +0530247 return tc3589x_reg_write(tc3589x, TC3589x_RSTINTCLR, 0x1);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200248}
249
Sundar Iyer611b7592010-12-13 09:33:15 +0530250static int __devinit tc3589x_device_init(struct tc3589x *tc3589x)
251{
252 int ret = 0;
253 unsigned int blocks = tc3589x->pdata->block;
254
255 if (blocks & TC3589x_BLOCK_GPIO) {
256 ret = mfd_add_devices(tc3589x->dev, -1, tc3589x_dev_gpio,
257 ARRAY_SIZE(tc3589x_dev_gpio), NULL,
258 tc3589x->irq_base);
259 if (ret) {
260 dev_err(tc3589x->dev, "failed to add gpio child\n");
261 return ret;
262 }
263 dev_info(tc3589x->dev, "added gpio block\n");
264 }
265
266 return ret;
267
268}
269
Sundar Iyer20406eb2010-12-13 09:33:14 +0530270static int __devinit tc3589x_probe(struct i2c_client *i2c,
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200271 const struct i2c_device_id *id)
272{
Sundar Iyer20406eb2010-12-13 09:33:14 +0530273 struct tc3589x_platform_data *pdata = i2c->dev.platform_data;
274 struct tc3589x *tc3589x;
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200275 int ret;
276
277 if (!i2c_check_functionality(i2c->adapter, I2C_FUNC_SMBUS_BYTE_DATA
278 | I2C_FUNC_SMBUS_I2C_BLOCK))
279 return -EIO;
280
Sundar Iyer20406eb2010-12-13 09:33:14 +0530281 tc3589x = kzalloc(sizeof(struct tc3589x), GFP_KERNEL);
282 if (!tc3589x)
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200283 return -ENOMEM;
284
Sundar Iyer20406eb2010-12-13 09:33:14 +0530285 mutex_init(&tc3589x->lock);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200286
Sundar Iyer20406eb2010-12-13 09:33:14 +0530287 tc3589x->dev = &i2c->dev;
288 tc3589x->i2c = i2c;
289 tc3589x->pdata = pdata;
290 tc3589x->irq_base = pdata->irq_base;
291 tc3589x->num_gpio = id->driver_data;
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200292
Sundar Iyer20406eb2010-12-13 09:33:14 +0530293 i2c_set_clientdata(i2c, tc3589x);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200294
Sundar Iyer20406eb2010-12-13 09:33:14 +0530295 ret = tc3589x_chip_init(tc3589x);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200296 if (ret)
297 goto out_free;
298
Sundar Iyer20406eb2010-12-13 09:33:14 +0530299 ret = tc3589x_irq_init(tc3589x);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200300 if (ret)
301 goto out_free;
302
Sundar Iyer20406eb2010-12-13 09:33:14 +0530303 ret = request_threaded_irq(tc3589x->i2c->irq, NULL, tc3589x_irq,
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200304 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
Sundar Iyer20406eb2010-12-13 09:33:14 +0530305 "tc3589x", tc3589x);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200306 if (ret) {
Sundar Iyer20406eb2010-12-13 09:33:14 +0530307 dev_err(tc3589x->dev, "failed to request IRQ: %d\n", ret);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200308 goto out_removeirq;
309 }
310
Sundar Iyer611b7592010-12-13 09:33:15 +0530311 ret = tc3589x_device_init(tc3589x);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200312 if (ret) {
Sundar Iyer611b7592010-12-13 09:33:15 +0530313 dev_err(tc3589x->dev, "failed to add child devices\n");
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200314 goto out_freeirq;
315 }
316
317 return 0;
318
319out_freeirq:
Sundar Iyer20406eb2010-12-13 09:33:14 +0530320 free_irq(tc3589x->i2c->irq, tc3589x);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200321out_removeirq:
Sundar Iyer20406eb2010-12-13 09:33:14 +0530322 tc3589x_irq_remove(tc3589x);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200323out_free:
Sundar Iyer20406eb2010-12-13 09:33:14 +0530324 kfree(tc3589x);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200325 return ret;
326}
327
Sundar Iyer20406eb2010-12-13 09:33:14 +0530328static int __devexit tc3589x_remove(struct i2c_client *client)
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200329{
Sundar Iyer20406eb2010-12-13 09:33:14 +0530330 struct tc3589x *tc3589x = i2c_get_clientdata(client);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200331
Sundar Iyer20406eb2010-12-13 09:33:14 +0530332 mfd_remove_devices(tc3589x->dev);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200333
Sundar Iyer20406eb2010-12-13 09:33:14 +0530334 free_irq(tc3589x->i2c->irq, tc3589x);
335 tc3589x_irq_remove(tc3589x);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200336
Sundar Iyer20406eb2010-12-13 09:33:14 +0530337 kfree(tc3589x);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200338
339 return 0;
340}
341
Sundar Iyer20406eb2010-12-13 09:33:14 +0530342static const struct i2c_device_id tc3589x_id[] = {
343 { "tc3589x", 24 },
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200344 { }
345};
Sundar Iyer20406eb2010-12-13 09:33:14 +0530346MODULE_DEVICE_TABLE(i2c, tc3589x_id);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200347
Sundar Iyer20406eb2010-12-13 09:33:14 +0530348static struct i2c_driver tc3589x_driver = {
349 .driver.name = "tc3589x",
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200350 .driver.owner = THIS_MODULE,
Sundar Iyer20406eb2010-12-13 09:33:14 +0530351 .probe = tc3589x_probe,
352 .remove = __devexit_p(tc3589x_remove),
353 .id_table = tc3589x_id,
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200354};
355
Sundar Iyer20406eb2010-12-13 09:33:14 +0530356static int __init tc3589x_init(void)
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200357{
Sundar Iyer20406eb2010-12-13 09:33:14 +0530358 return i2c_add_driver(&tc3589x_driver);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200359}
Sundar Iyer20406eb2010-12-13 09:33:14 +0530360subsys_initcall(tc3589x_init);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200361
Sundar Iyer20406eb2010-12-13 09:33:14 +0530362static void __exit tc3589x_exit(void)
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200363{
Sundar Iyer20406eb2010-12-13 09:33:14 +0530364 i2c_del_driver(&tc3589x_driver);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200365}
Sundar Iyer20406eb2010-12-13 09:33:14 +0530366module_exit(tc3589x_exit);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200367
368MODULE_LICENSE("GPL v2");
Sundar Iyer20406eb2010-12-13 09:33:14 +0530369MODULE_DESCRIPTION("TC3589x MFD core driver");
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200370MODULE_AUTHOR("Hanumath Prasad, Rabin Vincent");