blob: ecc092c7f7453e4cfb7146fc222546869239f8b0 [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>
Lee Jones15e27b12012-09-07 12:14:56 +010012#include <linux/irqdomain.h>
Rabin Vincentb4ecd322010-05-10 23:39:47 +020013#include <linux/slab.h>
14#include <linux/i2c.h>
Lee Jonesa435ae12012-09-07 12:14:57 +010015#include <linux/of.h>
Rabin Vincentb4ecd322010-05-10 23:39:47 +020016#include <linux/mfd/core.h>
Sundar Iyerc6eda6c2010-12-13 09:33:12 +053017#include <linux/mfd/tc3589x.h>
Rabin Vincentb4ecd322010-05-10 23:39:47 +020018
Sundar Iyer593e9d72010-12-13 09:33:18 +053019#define TC3589x_CLKMODE_MODCTL_SLEEP 0x0
20#define TC3589x_CLKMODE_MODCTL_OPERATION (1 << 0)
21
Rabin Vincentb4ecd322010-05-10 23:39:47 +020022/**
Sundar Iyer20406eb2010-12-13 09:33:14 +053023 * tc3589x_reg_read() - read a single TC3589x register
24 * @tc3589x: Device to read from
Rabin Vincentb4ecd322010-05-10 23:39:47 +020025 * @reg: Register to read
26 */
Sundar Iyer20406eb2010-12-13 09:33:14 +053027int tc3589x_reg_read(struct tc3589x *tc3589x, u8 reg)
Rabin Vincentb4ecd322010-05-10 23:39:47 +020028{
29 int ret;
30
Sundar Iyer20406eb2010-12-13 09:33:14 +053031 ret = i2c_smbus_read_byte_data(tc3589x->i2c, reg);
Rabin Vincentb4ecd322010-05-10 23:39:47 +020032 if (ret < 0)
Sundar Iyer20406eb2010-12-13 09:33:14 +053033 dev_err(tc3589x->dev, "failed to read reg %#x: %d\n",
Rabin Vincentb4ecd322010-05-10 23:39:47 +020034 reg, ret);
35
36 return ret;
37}
Sundar Iyer20406eb2010-12-13 09:33:14 +053038EXPORT_SYMBOL_GPL(tc3589x_reg_read);
Rabin Vincentb4ecd322010-05-10 23:39:47 +020039
40/**
Sundar Iyer20406eb2010-12-13 09:33:14 +053041 * tc3589x_reg_read() - write a single TC3589x register
42 * @tc3589x: Device to write to
Rabin Vincentb4ecd322010-05-10 23:39:47 +020043 * @reg: Register to read
44 * @data: Value to write
45 */
Sundar Iyer20406eb2010-12-13 09:33:14 +053046int tc3589x_reg_write(struct tc3589x *tc3589x, u8 reg, u8 data)
Rabin Vincentb4ecd322010-05-10 23:39:47 +020047{
48 int ret;
49
Sundar Iyer20406eb2010-12-13 09:33:14 +053050 ret = i2c_smbus_write_byte_data(tc3589x->i2c, reg, data);
Rabin Vincentb4ecd322010-05-10 23:39:47 +020051 if (ret < 0)
Sundar Iyer20406eb2010-12-13 09:33:14 +053052 dev_err(tc3589x->dev, "failed to write reg %#x: %d\n",
Rabin Vincentb4ecd322010-05-10 23:39:47 +020053 reg, ret);
54
55 return ret;
56}
Sundar Iyer20406eb2010-12-13 09:33:14 +053057EXPORT_SYMBOL_GPL(tc3589x_reg_write);
Rabin Vincentb4ecd322010-05-10 23:39:47 +020058
59/**
Sundar Iyer20406eb2010-12-13 09:33:14 +053060 * tc3589x_block_read() - read multiple TC3589x registers
61 * @tc3589x: Device to read from
Rabin Vincentb4ecd322010-05-10 23:39:47 +020062 * @reg: First register
63 * @length: Number of registers
64 * @values: Buffer to write to
65 */
Sundar Iyer20406eb2010-12-13 09:33:14 +053066int tc3589x_block_read(struct tc3589x *tc3589x, u8 reg, u8 length, u8 *values)
Rabin Vincentb4ecd322010-05-10 23:39:47 +020067{
68 int ret;
69
Sundar Iyer20406eb2010-12-13 09:33:14 +053070 ret = i2c_smbus_read_i2c_block_data(tc3589x->i2c, reg, length, values);
Rabin Vincentb4ecd322010-05-10 23:39:47 +020071 if (ret < 0)
Sundar Iyer20406eb2010-12-13 09:33:14 +053072 dev_err(tc3589x->dev, "failed to read regs %#x: %d\n",
Rabin Vincentb4ecd322010-05-10 23:39:47 +020073 reg, ret);
74
75 return ret;
76}
Sundar Iyer20406eb2010-12-13 09:33:14 +053077EXPORT_SYMBOL_GPL(tc3589x_block_read);
Rabin Vincentb4ecd322010-05-10 23:39:47 +020078
79/**
Sundar Iyer20406eb2010-12-13 09:33:14 +053080 * tc3589x_block_write() - write multiple TC3589x registers
81 * @tc3589x: Device to write to
Rabin Vincentb4ecd322010-05-10 23:39:47 +020082 * @reg: First register
83 * @length: Number of registers
84 * @values: Values to write
85 */
Sundar Iyer20406eb2010-12-13 09:33:14 +053086int tc3589x_block_write(struct tc3589x *tc3589x, u8 reg, u8 length,
Rabin Vincentb4ecd322010-05-10 23:39:47 +020087 const u8 *values)
88{
89 int ret;
90
Sundar Iyer20406eb2010-12-13 09:33:14 +053091 ret = i2c_smbus_write_i2c_block_data(tc3589x->i2c, reg, length,
Rabin Vincentb4ecd322010-05-10 23:39:47 +020092 values);
93 if (ret < 0)
Sundar Iyer20406eb2010-12-13 09:33:14 +053094 dev_err(tc3589x->dev, "failed to write regs %#x: %d\n",
Rabin Vincentb4ecd322010-05-10 23:39:47 +020095 reg, ret);
96
97 return ret;
98}
Sundar Iyer20406eb2010-12-13 09:33:14 +053099EXPORT_SYMBOL_GPL(tc3589x_block_write);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200100
101/**
Sundar Iyer20406eb2010-12-13 09:33:14 +0530102 * tc3589x_set_bits() - set the value of a bitfield in a TC3589x register
103 * @tc3589x: Device to write to
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200104 * @reg: Register to write
105 * @mask: Mask of bits to set
106 * @values: Value to set
107 */
Sundar Iyer20406eb2010-12-13 09:33:14 +0530108int tc3589x_set_bits(struct tc3589x *tc3589x, u8 reg, u8 mask, u8 val)
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200109{
110 int ret;
111
Sundar Iyer20406eb2010-12-13 09:33:14 +0530112 mutex_lock(&tc3589x->lock);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200113
Sundar Iyer20406eb2010-12-13 09:33:14 +0530114 ret = tc3589x_reg_read(tc3589x, reg);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200115 if (ret < 0)
116 goto out;
117
118 ret &= ~mask;
119 ret |= val;
120
Sundar Iyer20406eb2010-12-13 09:33:14 +0530121 ret = tc3589x_reg_write(tc3589x, reg, ret);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200122
123out:
Sundar Iyer20406eb2010-12-13 09:33:14 +0530124 mutex_unlock(&tc3589x->lock);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200125 return ret;
126}
Sundar Iyer20406eb2010-12-13 09:33:14 +0530127EXPORT_SYMBOL_GPL(tc3589x_set_bits);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200128
129static struct resource gpio_resources[] = {
130 {
Sundar Iyer20406eb2010-12-13 09:33:14 +0530131 .start = TC3589x_INT_GPIIRQ,
132 .end = TC3589x_INT_GPIIRQ,
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200133 .flags = IORESOURCE_IRQ,
134 },
135};
136
Sundar Iyer09c730a2010-12-21 15:53:31 +0530137static struct resource keypad_resources[] = {
138 {
139 .start = TC3589x_INT_KBDIRQ,
140 .end = TC3589x_INT_KBDIRQ,
141 .flags = IORESOURCE_IRQ,
142 },
143};
144
Sundar Iyer611b7592010-12-13 09:33:15 +0530145static struct mfd_cell tc3589x_dev_gpio[] = {
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200146 {
Sundar Iyer20406eb2010-12-13 09:33:14 +0530147 .name = "tc3589x-gpio",
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200148 .num_resources = ARRAY_SIZE(gpio_resources),
149 .resources = &gpio_resources[0],
Lee Jonesa435ae12012-09-07 12:14:57 +0100150 .of_compatible = "tc3589x-gpio",
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200151 },
152};
153
Sundar Iyer09c730a2010-12-21 15:53:31 +0530154static struct mfd_cell tc3589x_dev_keypad[] = {
155 {
156 .name = "tc3589x-keypad",
157 .num_resources = ARRAY_SIZE(keypad_resources),
158 .resources = &keypad_resources[0],
Lee Jonesa435ae12012-09-07 12:14:57 +0100159 .of_compatible = "tc3589x-keypad",
Sundar Iyer09c730a2010-12-21 15:53:31 +0530160 },
161};
162
Sundar Iyer20406eb2010-12-13 09:33:14 +0530163static irqreturn_t tc3589x_irq(int irq, void *data)
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200164{
Sundar Iyer20406eb2010-12-13 09:33:14 +0530165 struct tc3589x *tc3589x = data;
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200166 int status;
167
Sundar Iyerbd77efd2010-12-13 09:33:16 +0530168again:
Sundar Iyer20406eb2010-12-13 09:33:14 +0530169 status = tc3589x_reg_read(tc3589x, TC3589x_IRQST);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200170 if (status < 0)
171 return IRQ_NONE;
172
173 while (status) {
174 int bit = __ffs(status);
Lee Jones15e27b12012-09-07 12:14:56 +0100175 int virq = irq_create_mapping(tc3589x->domain, bit);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200176
Lee Jones15e27b12012-09-07 12:14:56 +0100177 handle_nested_irq(virq);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200178 status &= ~(1 << bit);
179 }
180
181 /*
182 * A dummy read or write (to any register) appears to be necessary to
183 * have the last interrupt clear (for example, GPIO IC write) take
Sundar Iyerbd77efd2010-12-13 09:33:16 +0530184 * effect. In such a case, recheck for any interrupt which is still
185 * pending.
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200186 */
Sundar Iyerbd77efd2010-12-13 09:33:16 +0530187 status = tc3589x_reg_read(tc3589x, TC3589x_IRQST);
188 if (status)
189 goto again;
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200190
191 return IRQ_HANDLED;
192}
193
Lee Jones15e27b12012-09-07 12:14:56 +0100194static int tc3589x_irq_map(struct irq_domain *d, unsigned int virq,
195 irq_hw_number_t hwirq)
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200196{
Lee Jones15e27b12012-09-07 12:14:56 +0100197 struct tc3589x *tc3589x = d->host_data;
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200198
Lee Jones15e27b12012-09-07 12:14:56 +0100199 irq_set_chip_data(virq, tc3589x);
200 irq_set_chip_and_handler(virq, &dummy_irq_chip,
201 handle_edge_irq);
202 irq_set_nested_thread(virq, 1);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200203#ifdef CONFIG_ARM
Lee Jones15e27b12012-09-07 12:14:56 +0100204 set_irq_flags(virq, IRQF_VALID);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200205#else
Lee Jones15e27b12012-09-07 12:14:56 +0100206 irq_set_noprobe(virq);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200207#endif
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200208
209 return 0;
210}
211
Lee Jones15e27b12012-09-07 12:14:56 +0100212static void tc3589x_irq_unmap(struct irq_domain *d, unsigned int virq)
213{
214#ifdef CONFIG_ARM
215 set_irq_flags(virq, 0);
216#endif
217 irq_set_chip_and_handler(virq, NULL, NULL);
218 irq_set_chip_data(virq, NULL);
219}
220
221static struct irq_domain_ops tc3589x_irq_ops = {
Linus Walleij1f0529b2013-01-02 14:40:14 +0100222 .map = tc3589x_irq_map,
Lee Jones15e27b12012-09-07 12:14:56 +0100223 .unmap = tc3589x_irq_unmap,
Linus Walleij1f0529b2013-01-02 14:40:14 +0100224 .xlate = irq_domain_xlate_twocell,
Lee Jones15e27b12012-09-07 12:14:56 +0100225};
226
Lee Jonesa435ae12012-09-07 12:14:57 +0100227static int tc3589x_irq_init(struct tc3589x *tc3589x, struct device_node *np)
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200228{
Sundar Iyer20406eb2010-12-13 09:33:14 +0530229 int base = tc3589x->irq_base;
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200230
Linus Walleij1f0529b2013-01-02 14:40:14 +0100231 tc3589x->domain = irq_domain_add_simple(
232 np, TC3589x_NR_INTERNAL_IRQS, base,
233 &tc3589x_irq_ops, tc3589x);
Lee Jones15e27b12012-09-07 12:14:56 +0100234
235 if (!tc3589x->domain) {
236 dev_err(tc3589x->dev, "Failed to create irqdomain\n");
237 return -ENOSYS;
238 }
239
240 return 0;
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200241}
242
Sundar Iyer20406eb2010-12-13 09:33:14 +0530243static int tc3589x_chip_init(struct tc3589x *tc3589x)
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200244{
245 int manf, ver, ret;
246
Sundar Iyer20406eb2010-12-13 09:33:14 +0530247 manf = tc3589x_reg_read(tc3589x, TC3589x_MANFCODE);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200248 if (manf < 0)
249 return manf;
250
Sundar Iyer20406eb2010-12-13 09:33:14 +0530251 ver = tc3589x_reg_read(tc3589x, TC3589x_VERSION);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200252 if (ver < 0)
253 return ver;
254
Sundar Iyer20406eb2010-12-13 09:33:14 +0530255 if (manf != TC3589x_MANFCODE_MAGIC) {
256 dev_err(tc3589x->dev, "unknown manufacturer: %#x\n", manf);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200257 return -EINVAL;
258 }
259
Sundar Iyer20406eb2010-12-13 09:33:14 +0530260 dev_info(tc3589x->dev, "manufacturer: %#x, version: %#x\n", manf, ver);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200261
Sundar Iyer523bc382010-12-13 09:33:17 +0530262 /*
263 * Put everything except the IRQ module into reset;
264 * also spare the GPIO module for any pin initialization
265 * done during pre-kernel boot
266 */
Sundar Iyer20406eb2010-12-13 09:33:14 +0530267 ret = tc3589x_reg_write(tc3589x, TC3589x_RSTCTRL,
268 TC3589x_RSTCTRL_TIMRST
269 | TC3589x_RSTCTRL_ROTRST
Sundar Iyer523bc382010-12-13 09:33:17 +0530270 | TC3589x_RSTCTRL_KBDRST);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200271 if (ret < 0)
272 return ret;
273
274 /* Clear the reset interrupt. */
Sundar Iyer20406eb2010-12-13 09:33:14 +0530275 return tc3589x_reg_write(tc3589x, TC3589x_RSTINTCLR, 0x1);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200276}
277
Bill Pembertonf791be42012-11-19 13:23:04 -0500278static int tc3589x_device_init(struct tc3589x *tc3589x)
Sundar Iyer611b7592010-12-13 09:33:15 +0530279{
280 int ret = 0;
281 unsigned int blocks = tc3589x->pdata->block;
282
283 if (blocks & TC3589x_BLOCK_GPIO) {
284 ret = mfd_add_devices(tc3589x->dev, -1, tc3589x_dev_gpio,
Mark Brown55692af2012-09-11 15:16:36 +0800285 ARRAY_SIZE(tc3589x_dev_gpio), NULL,
Lee Jones15e27b12012-09-07 12:14:56 +0100286 tc3589x->irq_base, tc3589x->domain);
Sundar Iyer611b7592010-12-13 09:33:15 +0530287 if (ret) {
288 dev_err(tc3589x->dev, "failed to add gpio child\n");
289 return ret;
290 }
291 dev_info(tc3589x->dev, "added gpio block\n");
292 }
293
Sundar Iyer09c730a2010-12-21 15:53:31 +0530294 if (blocks & TC3589x_BLOCK_KEYPAD) {
295 ret = mfd_add_devices(tc3589x->dev, -1, tc3589x_dev_keypad,
Mark Brown55692af2012-09-11 15:16:36 +0800296 ARRAY_SIZE(tc3589x_dev_keypad), NULL,
Lee Jones15e27b12012-09-07 12:14:56 +0100297 tc3589x->irq_base, tc3589x->domain);
Sundar Iyer09c730a2010-12-21 15:53:31 +0530298 if (ret) {
299 dev_err(tc3589x->dev, "failed to keypad child\n");
300 return ret;
301 }
302 dev_info(tc3589x->dev, "added keypad block\n");
303 }
Sundar Iyer611b7592010-12-13 09:33:15 +0530304
Sundar Iyer09c730a2010-12-21 15:53:31 +0530305 return ret;
Sundar Iyer611b7592010-12-13 09:33:15 +0530306}
307
Lee Jonesa435ae12012-09-07 12:14:57 +0100308static int tc3589x_of_probe(struct device_node *np,
309 struct tc3589x_platform_data *pdata)
310{
311 struct device_node *child;
312
313 for_each_child_of_node(np, child) {
314 if (!strcmp(child->name, "tc3589x_gpio")) {
315 pdata->block |= TC3589x_BLOCK_GPIO;
316 }
317 if (!strcmp(child->name, "tc3589x_keypad")) {
318 pdata->block |= TC3589x_BLOCK_KEYPAD;
319 }
320 }
321
322 return 0;
323}
324
Bill Pembertonf791be42012-11-19 13:23:04 -0500325static int tc3589x_probe(struct i2c_client *i2c,
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200326 const struct i2c_device_id *id)
327{
Sundar Iyer20406eb2010-12-13 09:33:14 +0530328 struct tc3589x_platform_data *pdata = i2c->dev.platform_data;
Lee Jonesa435ae12012-09-07 12:14:57 +0100329 struct device_node *np = i2c->dev.of_node;
Sundar Iyer20406eb2010-12-13 09:33:14 +0530330 struct tc3589x *tc3589x;
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200331 int ret;
332
Lee Jonesa435ae12012-09-07 12:14:57 +0100333 if (!pdata) {
334 if (np) {
335 pdata = devm_kzalloc(&i2c->dev, sizeof(*pdata), GFP_KERNEL);
336 if (!pdata)
337 return -ENOMEM;
338
339 ret = tc3589x_of_probe(np, pdata);
340 if (ret)
341 return ret;
342 }
343 else {
344 dev_err(&i2c->dev, "No platform data or DT found\n");
345 return -EINVAL;
346 }
347 }
348
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200349 if (!i2c_check_functionality(i2c->adapter, I2C_FUNC_SMBUS_BYTE_DATA
350 | I2C_FUNC_SMBUS_I2C_BLOCK))
351 return -EIO;
352
Sundar Iyer20406eb2010-12-13 09:33:14 +0530353 tc3589x = kzalloc(sizeof(struct tc3589x), GFP_KERNEL);
354 if (!tc3589x)
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200355 return -ENOMEM;
356
Sundar Iyer20406eb2010-12-13 09:33:14 +0530357 mutex_init(&tc3589x->lock);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200358
Sundar Iyer20406eb2010-12-13 09:33:14 +0530359 tc3589x->dev = &i2c->dev;
360 tc3589x->i2c = i2c;
361 tc3589x->pdata = pdata;
362 tc3589x->irq_base = pdata->irq_base;
363 tc3589x->num_gpio = id->driver_data;
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200364
Sundar Iyer20406eb2010-12-13 09:33:14 +0530365 i2c_set_clientdata(i2c, tc3589x);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200366
Sundar Iyer20406eb2010-12-13 09:33:14 +0530367 ret = tc3589x_chip_init(tc3589x);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200368 if (ret)
369 goto out_free;
370
Lee Jonesa435ae12012-09-07 12:14:57 +0100371 ret = tc3589x_irq_init(tc3589x, np);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200372 if (ret)
373 goto out_free;
374
Sundar Iyer20406eb2010-12-13 09:33:14 +0530375 ret = request_threaded_irq(tc3589x->i2c->irq, NULL, tc3589x_irq,
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200376 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
Sundar Iyer20406eb2010-12-13 09:33:14 +0530377 "tc3589x", tc3589x);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200378 if (ret) {
Sundar Iyer20406eb2010-12-13 09:33:14 +0530379 dev_err(tc3589x->dev, "failed to request IRQ: %d\n", ret);
Lee Jones15e27b12012-09-07 12:14:56 +0100380 goto out_free;
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200381 }
382
Sundar Iyer611b7592010-12-13 09:33:15 +0530383 ret = tc3589x_device_init(tc3589x);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200384 if (ret) {
Sundar Iyer611b7592010-12-13 09:33:15 +0530385 dev_err(tc3589x->dev, "failed to add child devices\n");
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200386 goto out_freeirq;
387 }
388
389 return 0;
390
391out_freeirq:
Sundar Iyer20406eb2010-12-13 09:33:14 +0530392 free_irq(tc3589x->i2c->irq, tc3589x);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200393out_free:
Sundar Iyer20406eb2010-12-13 09:33:14 +0530394 kfree(tc3589x);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200395 return ret;
396}
397
Bill Pemberton4740f732012-11-19 13:26:01 -0500398static int tc3589x_remove(struct i2c_client *client)
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200399{
Sundar Iyer20406eb2010-12-13 09:33:14 +0530400 struct tc3589x *tc3589x = i2c_get_clientdata(client);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200401
Sundar Iyer20406eb2010-12-13 09:33:14 +0530402 mfd_remove_devices(tc3589x->dev);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200403
Sundar Iyer20406eb2010-12-13 09:33:14 +0530404 free_irq(tc3589x->i2c->irq, tc3589x);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200405
Sundar Iyer20406eb2010-12-13 09:33:14 +0530406 kfree(tc3589x);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200407
408 return 0;
409}
410
Axel Lin930bf022012-07-02 17:19:52 +0800411#ifdef CONFIG_PM_SLEEP
Sundar Iyer593e9d72010-12-13 09:33:18 +0530412static int tc3589x_suspend(struct device *dev)
413{
414 struct tc3589x *tc3589x = dev_get_drvdata(dev);
415 struct i2c_client *client = tc3589x->i2c;
416 int ret = 0;
417
418 /* put the system to sleep mode */
419 if (!device_may_wakeup(&client->dev))
420 ret = tc3589x_reg_write(tc3589x, TC3589x_CLKMODE,
421 TC3589x_CLKMODE_MODCTL_SLEEP);
422
423 return ret;
424}
425
426static int tc3589x_resume(struct device *dev)
427{
428 struct tc3589x *tc3589x = dev_get_drvdata(dev);
429 struct i2c_client *client = tc3589x->i2c;
430 int ret = 0;
431
432 /* enable the system into operation */
433 if (!device_may_wakeup(&client->dev))
434 ret = tc3589x_reg_write(tc3589x, TC3589x_CLKMODE,
435 TC3589x_CLKMODE_MODCTL_OPERATION);
436
437 return ret;
438}
Linus Walleij54d8e2c2011-08-09 20:37:17 +0200439#endif
Sundar Iyer593e9d72010-12-13 09:33:18 +0530440
Axel Lin930bf022012-07-02 17:19:52 +0800441static SIMPLE_DEV_PM_OPS(tc3589x_dev_pm_ops, tc3589x_suspend, tc3589x_resume);
442
Sundar Iyer20406eb2010-12-13 09:33:14 +0530443static const struct i2c_device_id tc3589x_id[] = {
444 { "tc3589x", 24 },
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200445 { }
446};
Sundar Iyer20406eb2010-12-13 09:33:14 +0530447MODULE_DEVICE_TABLE(i2c, tc3589x_id);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200448
Sundar Iyer20406eb2010-12-13 09:33:14 +0530449static struct i2c_driver tc3589x_driver = {
450 .driver.name = "tc3589x",
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200451 .driver.owner = THIS_MODULE,
Sundar Iyer593e9d72010-12-13 09:33:18 +0530452 .driver.pm = &tc3589x_dev_pm_ops,
Sundar Iyer20406eb2010-12-13 09:33:14 +0530453 .probe = tc3589x_probe,
Bill Pemberton84449212012-11-19 13:20:24 -0500454 .remove = tc3589x_remove,
Sundar Iyer20406eb2010-12-13 09:33:14 +0530455 .id_table = tc3589x_id,
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200456};
457
Sundar Iyer20406eb2010-12-13 09:33:14 +0530458static int __init tc3589x_init(void)
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200459{
Sundar Iyer20406eb2010-12-13 09:33:14 +0530460 return i2c_add_driver(&tc3589x_driver);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200461}
Sundar Iyer20406eb2010-12-13 09:33:14 +0530462subsys_initcall(tc3589x_init);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200463
Sundar Iyer20406eb2010-12-13 09:33:14 +0530464static void __exit tc3589x_exit(void)
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200465{
Sundar Iyer20406eb2010-12-13 09:33:14 +0530466 i2c_del_driver(&tc3589x_driver);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200467}
Sundar Iyer20406eb2010-12-13 09:33:14 +0530468module_exit(tc3589x_exit);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200469
470MODULE_LICENSE("GPL v2");
Sundar Iyer20406eb2010-12-13 09:33:14 +0530471MODULE_DESCRIPTION("TC3589x MFD core driver");
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200472MODULE_AUTHOR("Hanumath Prasad, Rabin Vincent");