blob: 0072e668c208abc22608b245395323f7122f3283 [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>
Linus Walleija381b132014-01-23 13:43:28 +010016#include <linux/of_device.h>
Rabin Vincentb4ecd322010-05-10 23:39:47 +020017#include <linux/mfd/core.h>
Sundar Iyerc6eda6c2010-12-13 09:33:12 +053018#include <linux/mfd/tc3589x.h>
Linus Walleija381b132014-01-23 13:43:28 +010019#include <linux/err.h>
Rabin Vincentb4ecd322010-05-10 23:39:47 +020020
Linus Walleije64c1eb2013-10-18 11:51:45 +020021/**
22 * enum tc3589x_version - indicates the TC3589x version
23 */
24enum tc3589x_version {
25 TC3589X_TC35890,
26 TC3589X_TC35892,
27 TC3589X_TC35893,
28 TC3589X_TC35894,
29 TC3589X_TC35895,
30 TC3589X_TC35896,
31 TC3589X_UNKNOWN,
32};
33
Sundar Iyer593e9d72010-12-13 09:33:18 +053034#define TC3589x_CLKMODE_MODCTL_SLEEP 0x0
35#define TC3589x_CLKMODE_MODCTL_OPERATION (1 << 0)
36
Rabin Vincentb4ecd322010-05-10 23:39:47 +020037/**
Sundar Iyer20406eb2010-12-13 09:33:14 +053038 * tc3589x_reg_read() - read a single TC3589x register
39 * @tc3589x: Device to read from
Rabin Vincentb4ecd322010-05-10 23:39:47 +020040 * @reg: Register to read
41 */
Sundar Iyer20406eb2010-12-13 09:33:14 +053042int tc3589x_reg_read(struct tc3589x *tc3589x, u8 reg)
Rabin Vincentb4ecd322010-05-10 23:39:47 +020043{
44 int ret;
45
Sundar Iyer20406eb2010-12-13 09:33:14 +053046 ret = i2c_smbus_read_byte_data(tc3589x->i2c, reg);
Rabin Vincentb4ecd322010-05-10 23:39:47 +020047 if (ret < 0)
Sundar Iyer20406eb2010-12-13 09:33:14 +053048 dev_err(tc3589x->dev, "failed to read reg %#x: %d\n",
Rabin Vincentb4ecd322010-05-10 23:39:47 +020049 reg, ret);
50
51 return ret;
52}
Sundar Iyer20406eb2010-12-13 09:33:14 +053053EXPORT_SYMBOL_GPL(tc3589x_reg_read);
Rabin Vincentb4ecd322010-05-10 23:39:47 +020054
55/**
Sundar Iyer20406eb2010-12-13 09:33:14 +053056 * tc3589x_reg_read() - write a single TC3589x register
57 * @tc3589x: Device to write to
Rabin Vincentb4ecd322010-05-10 23:39:47 +020058 * @reg: Register to read
59 * @data: Value to write
60 */
Sundar Iyer20406eb2010-12-13 09:33:14 +053061int tc3589x_reg_write(struct tc3589x *tc3589x, u8 reg, u8 data)
Rabin Vincentb4ecd322010-05-10 23:39:47 +020062{
63 int ret;
64
Sundar Iyer20406eb2010-12-13 09:33:14 +053065 ret = i2c_smbus_write_byte_data(tc3589x->i2c, reg, data);
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 write reg %#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_reg_write);
Rabin Vincentb4ecd322010-05-10 23:39:47 +020073
74/**
Sundar Iyer20406eb2010-12-13 09:33:14 +053075 * tc3589x_block_read() - read multiple TC3589x registers
76 * @tc3589x: Device to read from
Rabin Vincentb4ecd322010-05-10 23:39:47 +020077 * @reg: First register
78 * @length: Number of registers
79 * @values: Buffer to write to
80 */
Sundar Iyer20406eb2010-12-13 09:33:14 +053081int tc3589x_block_read(struct tc3589x *tc3589x, u8 reg, u8 length, u8 *values)
Rabin Vincentb4ecd322010-05-10 23:39:47 +020082{
83 int ret;
84
Sundar Iyer20406eb2010-12-13 09:33:14 +053085 ret = i2c_smbus_read_i2c_block_data(tc3589x->i2c, reg, length, values);
Rabin Vincentb4ecd322010-05-10 23:39:47 +020086 if (ret < 0)
Sundar Iyer20406eb2010-12-13 09:33:14 +053087 dev_err(tc3589x->dev, "failed to read regs %#x: %d\n",
Rabin Vincentb4ecd322010-05-10 23:39:47 +020088 reg, ret);
89
90 return ret;
91}
Sundar Iyer20406eb2010-12-13 09:33:14 +053092EXPORT_SYMBOL_GPL(tc3589x_block_read);
Rabin Vincentb4ecd322010-05-10 23:39:47 +020093
94/**
Sundar Iyer20406eb2010-12-13 09:33:14 +053095 * tc3589x_block_write() - write multiple TC3589x registers
96 * @tc3589x: Device to write to
Rabin Vincentb4ecd322010-05-10 23:39:47 +020097 * @reg: First register
98 * @length: Number of registers
99 * @values: Values to write
100 */
Sundar Iyer20406eb2010-12-13 09:33:14 +0530101int tc3589x_block_write(struct tc3589x *tc3589x, u8 reg, u8 length,
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200102 const u8 *values)
103{
104 int ret;
105
Sundar Iyer20406eb2010-12-13 09:33:14 +0530106 ret = i2c_smbus_write_i2c_block_data(tc3589x->i2c, reg, length,
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200107 values);
108 if (ret < 0)
Sundar Iyer20406eb2010-12-13 09:33:14 +0530109 dev_err(tc3589x->dev, "failed to write regs %#x: %d\n",
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200110 reg, ret);
111
112 return ret;
113}
Sundar Iyer20406eb2010-12-13 09:33:14 +0530114EXPORT_SYMBOL_GPL(tc3589x_block_write);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200115
116/**
Sundar Iyer20406eb2010-12-13 09:33:14 +0530117 * tc3589x_set_bits() - set the value of a bitfield in a TC3589x register
118 * @tc3589x: Device to write to
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200119 * @reg: Register to write
120 * @mask: Mask of bits to set
121 * @values: Value to set
122 */
Sundar Iyer20406eb2010-12-13 09:33:14 +0530123int tc3589x_set_bits(struct tc3589x *tc3589x, u8 reg, u8 mask, u8 val)
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200124{
125 int ret;
126
Sundar Iyer20406eb2010-12-13 09:33:14 +0530127 mutex_lock(&tc3589x->lock);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200128
Sundar Iyer20406eb2010-12-13 09:33:14 +0530129 ret = tc3589x_reg_read(tc3589x, reg);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200130 if (ret < 0)
131 goto out;
132
133 ret &= ~mask;
134 ret |= val;
135
Sundar Iyer20406eb2010-12-13 09:33:14 +0530136 ret = tc3589x_reg_write(tc3589x, reg, ret);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200137
138out:
Sundar Iyer20406eb2010-12-13 09:33:14 +0530139 mutex_unlock(&tc3589x->lock);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200140 return ret;
141}
Sundar Iyer20406eb2010-12-13 09:33:14 +0530142EXPORT_SYMBOL_GPL(tc3589x_set_bits);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200143
144static struct resource gpio_resources[] = {
145 {
Sundar Iyer20406eb2010-12-13 09:33:14 +0530146 .start = TC3589x_INT_GPIIRQ,
147 .end = TC3589x_INT_GPIIRQ,
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200148 .flags = IORESOURCE_IRQ,
149 },
150};
151
Sundar Iyer09c730a2010-12-21 15:53:31 +0530152static struct resource keypad_resources[] = {
153 {
154 .start = TC3589x_INT_KBDIRQ,
155 .end = TC3589x_INT_KBDIRQ,
156 .flags = IORESOURCE_IRQ,
157 },
158};
159
Geert Uytterhoevenafb580a2013-11-18 14:33:03 +0100160static const struct mfd_cell tc3589x_dev_gpio[] = {
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200161 {
Sundar Iyer20406eb2010-12-13 09:33:14 +0530162 .name = "tc3589x-gpio",
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200163 .num_resources = ARRAY_SIZE(gpio_resources),
164 .resources = &gpio_resources[0],
Linus Walleija381b132014-01-23 13:43:28 +0100165 .of_compatible = "toshiba,tc3589x-gpio",
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200166 },
167};
168
Geert Uytterhoevenafb580a2013-11-18 14:33:03 +0100169static const struct mfd_cell tc3589x_dev_keypad[] = {
Sundar Iyer09c730a2010-12-21 15:53:31 +0530170 {
171 .name = "tc3589x-keypad",
172 .num_resources = ARRAY_SIZE(keypad_resources),
173 .resources = &keypad_resources[0],
Linus Walleija381b132014-01-23 13:43:28 +0100174 .of_compatible = "toshiba,tc3589x-keypad",
Sundar Iyer09c730a2010-12-21 15:53:31 +0530175 },
176};
177
Sundar Iyer20406eb2010-12-13 09:33:14 +0530178static irqreturn_t tc3589x_irq(int irq, void *data)
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200179{
Sundar Iyer20406eb2010-12-13 09:33:14 +0530180 struct tc3589x *tc3589x = data;
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200181 int status;
182
Sundar Iyerbd77efd2010-12-13 09:33:16 +0530183again:
Sundar Iyer20406eb2010-12-13 09:33:14 +0530184 status = tc3589x_reg_read(tc3589x, TC3589x_IRQST);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200185 if (status < 0)
186 return IRQ_NONE;
187
188 while (status) {
189 int bit = __ffs(status);
Lee Jones15e27b12012-09-07 12:14:56 +0100190 int virq = irq_create_mapping(tc3589x->domain, bit);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200191
Lee Jones15e27b12012-09-07 12:14:56 +0100192 handle_nested_irq(virq);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200193 status &= ~(1 << bit);
194 }
195
196 /*
197 * A dummy read or write (to any register) appears to be necessary to
198 * have the last interrupt clear (for example, GPIO IC write) take
Sundar Iyerbd77efd2010-12-13 09:33:16 +0530199 * effect. In such a case, recheck for any interrupt which is still
200 * pending.
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200201 */
Sundar Iyerbd77efd2010-12-13 09:33:16 +0530202 status = tc3589x_reg_read(tc3589x, TC3589x_IRQST);
203 if (status)
204 goto again;
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200205
206 return IRQ_HANDLED;
207}
208
Lee Jones15e27b12012-09-07 12:14:56 +0100209static int tc3589x_irq_map(struct irq_domain *d, unsigned int virq,
210 irq_hw_number_t hwirq)
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200211{
Lee Jones15e27b12012-09-07 12:14:56 +0100212 struct tc3589x *tc3589x = d->host_data;
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200213
Lee Jones15e27b12012-09-07 12:14:56 +0100214 irq_set_chip_data(virq, tc3589x);
215 irq_set_chip_and_handler(virq, &dummy_irq_chip,
216 handle_edge_irq);
217 irq_set_nested_thread(virq, 1);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200218#ifdef CONFIG_ARM
Lee Jones15e27b12012-09-07 12:14:56 +0100219 set_irq_flags(virq, IRQF_VALID);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200220#else
Lee Jones15e27b12012-09-07 12:14:56 +0100221 irq_set_noprobe(virq);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200222#endif
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200223
224 return 0;
225}
226
Lee Jones15e27b12012-09-07 12:14:56 +0100227static void tc3589x_irq_unmap(struct irq_domain *d, unsigned int virq)
228{
229#ifdef CONFIG_ARM
230 set_irq_flags(virq, 0);
231#endif
232 irq_set_chip_and_handler(virq, NULL, NULL);
233 irq_set_chip_data(virq, NULL);
234}
235
236static struct irq_domain_ops tc3589x_irq_ops = {
Linus Walleij1f0529b2013-01-02 14:40:14 +0100237 .map = tc3589x_irq_map,
Lee Jones15e27b12012-09-07 12:14:56 +0100238 .unmap = tc3589x_irq_unmap,
Linus Walleij627918e2014-06-10 14:31:32 +0200239 .xlate = irq_domain_xlate_onecell,
Lee Jones15e27b12012-09-07 12:14:56 +0100240};
241
Lee Jonesa435ae12012-09-07 12:14:57 +0100242static int tc3589x_irq_init(struct tc3589x *tc3589x, struct device_node *np)
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200243{
Sundar Iyer20406eb2010-12-13 09:33:14 +0530244 int base = tc3589x->irq_base;
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200245
Linus Walleij1f0529b2013-01-02 14:40:14 +0100246 tc3589x->domain = irq_domain_add_simple(
247 np, TC3589x_NR_INTERNAL_IRQS, base,
248 &tc3589x_irq_ops, tc3589x);
Lee Jones15e27b12012-09-07 12:14:56 +0100249
250 if (!tc3589x->domain) {
251 dev_err(tc3589x->dev, "Failed to create irqdomain\n");
252 return -ENOSYS;
253 }
254
255 return 0;
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200256}
257
Sundar Iyer20406eb2010-12-13 09:33:14 +0530258static int tc3589x_chip_init(struct tc3589x *tc3589x)
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200259{
260 int manf, ver, ret;
261
Sundar Iyer20406eb2010-12-13 09:33:14 +0530262 manf = tc3589x_reg_read(tc3589x, TC3589x_MANFCODE);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200263 if (manf < 0)
264 return manf;
265
Sundar Iyer20406eb2010-12-13 09:33:14 +0530266 ver = tc3589x_reg_read(tc3589x, TC3589x_VERSION);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200267 if (ver < 0)
268 return ver;
269
Sundar Iyer20406eb2010-12-13 09:33:14 +0530270 if (manf != TC3589x_MANFCODE_MAGIC) {
271 dev_err(tc3589x->dev, "unknown manufacturer: %#x\n", manf);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200272 return -EINVAL;
273 }
274
Sundar Iyer20406eb2010-12-13 09:33:14 +0530275 dev_info(tc3589x->dev, "manufacturer: %#x, version: %#x\n", manf, ver);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200276
Sundar Iyer523bc382010-12-13 09:33:17 +0530277 /*
278 * Put everything except the IRQ module into reset;
279 * also spare the GPIO module for any pin initialization
280 * done during pre-kernel boot
281 */
Sundar Iyer20406eb2010-12-13 09:33:14 +0530282 ret = tc3589x_reg_write(tc3589x, TC3589x_RSTCTRL,
283 TC3589x_RSTCTRL_TIMRST
284 | TC3589x_RSTCTRL_ROTRST
Sundar Iyer523bc382010-12-13 09:33:17 +0530285 | TC3589x_RSTCTRL_KBDRST);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200286 if (ret < 0)
287 return ret;
288
289 /* Clear the reset interrupt. */
Sundar Iyer20406eb2010-12-13 09:33:14 +0530290 return tc3589x_reg_write(tc3589x, TC3589x_RSTINTCLR, 0x1);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200291}
292
Bill Pembertonf791be42012-11-19 13:23:04 -0500293static int tc3589x_device_init(struct tc3589x *tc3589x)
Sundar Iyer611b7592010-12-13 09:33:15 +0530294{
295 int ret = 0;
296 unsigned int blocks = tc3589x->pdata->block;
297
298 if (blocks & TC3589x_BLOCK_GPIO) {
299 ret = mfd_add_devices(tc3589x->dev, -1, tc3589x_dev_gpio,
Mark Brown55692af2012-09-11 15:16:36 +0800300 ARRAY_SIZE(tc3589x_dev_gpio), NULL,
Lee Jones15e27b12012-09-07 12:14:56 +0100301 tc3589x->irq_base, tc3589x->domain);
Sundar Iyer611b7592010-12-13 09:33:15 +0530302 if (ret) {
303 dev_err(tc3589x->dev, "failed to add gpio child\n");
304 return ret;
305 }
306 dev_info(tc3589x->dev, "added gpio block\n");
307 }
308
Sundar Iyer09c730a2010-12-21 15:53:31 +0530309 if (blocks & TC3589x_BLOCK_KEYPAD) {
310 ret = mfd_add_devices(tc3589x->dev, -1, tc3589x_dev_keypad,
Mark Brown55692af2012-09-11 15:16:36 +0800311 ARRAY_SIZE(tc3589x_dev_keypad), NULL,
Lee Jones15e27b12012-09-07 12:14:56 +0100312 tc3589x->irq_base, tc3589x->domain);
Sundar Iyer09c730a2010-12-21 15:53:31 +0530313 if (ret) {
314 dev_err(tc3589x->dev, "failed to keypad child\n");
315 return ret;
316 }
317 dev_info(tc3589x->dev, "added keypad block\n");
318 }
Sundar Iyer611b7592010-12-13 09:33:15 +0530319
Sundar Iyer09c730a2010-12-21 15:53:31 +0530320 return ret;
Sundar Iyer611b7592010-12-13 09:33:15 +0530321}
322
Linus Walleija381b132014-01-23 13:43:28 +0100323#ifdef CONFIG_OF
324static const struct of_device_id tc3589x_match[] = {
325 /* Legacy compatible string */
326 { .compatible = "tc3589x", .data = (void *) TC3589X_UNKNOWN },
327 { .compatible = "toshiba,tc35890", .data = (void *) TC3589X_TC35890 },
328 { .compatible = "toshiba,tc35892", .data = (void *) TC3589X_TC35892 },
329 { .compatible = "toshiba,tc35893", .data = (void *) TC3589X_TC35893 },
330 { .compatible = "toshiba,tc35894", .data = (void *) TC3589X_TC35894 },
331 { .compatible = "toshiba,tc35895", .data = (void *) TC3589X_TC35895 },
332 { .compatible = "toshiba,tc35896", .data = (void *) TC3589X_TC35896 },
333 { }
334};
335
336MODULE_DEVICE_TABLE(of, tc3589x_match);
337
338static struct tc3589x_platform_data *
339tc3589x_of_probe(struct device *dev, enum tc3589x_version *version)
Lee Jonesa435ae12012-09-07 12:14:57 +0100340{
Linus Walleija381b132014-01-23 13:43:28 +0100341 struct device_node *np = dev->of_node;
342 struct tc3589x_platform_data *pdata;
Lee Jonesa435ae12012-09-07 12:14:57 +0100343 struct device_node *child;
Linus Walleija381b132014-01-23 13:43:28 +0100344 const struct of_device_id *of_id;
345
346 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
347 if (!pdata)
348 return ERR_PTR(-ENOMEM);
349
350 of_id = of_match_device(tc3589x_match, dev);
351 if (!of_id)
352 return ERR_PTR(-ENODEV);
353 *version = (enum tc3589x_version) of_id->data;
Lee Jonesa435ae12012-09-07 12:14:57 +0100354
355 for_each_child_of_node(np, child) {
Linus Walleija381b132014-01-23 13:43:28 +0100356 if (of_device_is_compatible(child, "toshiba,tc3589x-gpio"))
Lee Jonesa435ae12012-09-07 12:14:57 +0100357 pdata->block |= TC3589x_BLOCK_GPIO;
Linus Walleija381b132014-01-23 13:43:28 +0100358 if (of_device_is_compatible(child, "toshiba,tc3589x-keypad"))
Lee Jonesa435ae12012-09-07 12:14:57 +0100359 pdata->block |= TC3589x_BLOCK_KEYPAD;
Lee Jonesa435ae12012-09-07 12:14:57 +0100360 }
361
Linus Walleija381b132014-01-23 13:43:28 +0100362 return pdata;
Lee Jonesa435ae12012-09-07 12:14:57 +0100363}
Linus Walleija381b132014-01-23 13:43:28 +0100364#else
365static inline struct tc3589x_platform_data *
366tc3589x_of_probe(struct device *dev, enum tc3589x_version *version)
367{
368 dev_err(dev, "no device tree support\n");
369 return ERR_PTR(-ENODEV);
370}
371#endif
Lee Jonesa435ae12012-09-07 12:14:57 +0100372
Bill Pembertonf791be42012-11-19 13:23:04 -0500373static int tc3589x_probe(struct i2c_client *i2c,
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200374 const struct i2c_device_id *id)
375{
Lee Jonesa435ae12012-09-07 12:14:57 +0100376 struct device_node *np = i2c->dev.of_node;
Linus Walleija381b132014-01-23 13:43:28 +0100377 struct tc3589x_platform_data *pdata = dev_get_platdata(&i2c->dev);
Sundar Iyer20406eb2010-12-13 09:33:14 +0530378 struct tc3589x *tc3589x;
Linus Walleija381b132014-01-23 13:43:28 +0100379 enum tc3589x_version version;
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200380 int ret;
381
Lee Jonesa435ae12012-09-07 12:14:57 +0100382 if (!pdata) {
Linus Walleija381b132014-01-23 13:43:28 +0100383 pdata = tc3589x_of_probe(&i2c->dev, &version);
384 if (IS_ERR(pdata)) {
Lee Jonesa435ae12012-09-07 12:14:57 +0100385 dev_err(&i2c->dev, "No platform data or DT found\n");
Linus Walleija381b132014-01-23 13:43:28 +0100386 return PTR_ERR(pdata);
Lee Jonesa435ae12012-09-07 12:14:57 +0100387 }
Linus Walleija381b132014-01-23 13:43:28 +0100388 } else {
389 /* When not probing from device tree we have this ID */
390 version = id->driver_data;
Lee Jonesa435ae12012-09-07 12:14:57 +0100391 }
392
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200393 if (!i2c_check_functionality(i2c->adapter, I2C_FUNC_SMBUS_BYTE_DATA
394 | I2C_FUNC_SMBUS_I2C_BLOCK))
395 return -EIO;
396
Jingoo Han1383e002013-02-20 18:31:52 +0900397 tc3589x = devm_kzalloc(&i2c->dev, sizeof(struct tc3589x),
398 GFP_KERNEL);
Sundar Iyer20406eb2010-12-13 09:33:14 +0530399 if (!tc3589x)
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200400 return -ENOMEM;
401
Sundar Iyer20406eb2010-12-13 09:33:14 +0530402 mutex_init(&tc3589x->lock);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200403
Sundar Iyer20406eb2010-12-13 09:33:14 +0530404 tc3589x->dev = &i2c->dev;
405 tc3589x->i2c = i2c;
406 tc3589x->pdata = pdata;
407 tc3589x->irq_base = pdata->irq_base;
Linus Walleije64c1eb2013-10-18 11:51:45 +0200408
Linus Walleija381b132014-01-23 13:43:28 +0100409 switch (version) {
Linus Walleije64c1eb2013-10-18 11:51:45 +0200410 case TC3589X_TC35893:
411 case TC3589X_TC35895:
412 case TC3589X_TC35896:
413 tc3589x->num_gpio = 20;
414 break;
415 case TC3589X_TC35890:
416 case TC3589X_TC35892:
417 case TC3589X_TC35894:
418 case TC3589X_UNKNOWN:
419 default:
420 tc3589x->num_gpio = 24;
421 break;
422 }
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200423
Sundar Iyer20406eb2010-12-13 09:33:14 +0530424 i2c_set_clientdata(i2c, tc3589x);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200425
Sundar Iyer20406eb2010-12-13 09:33:14 +0530426 ret = tc3589x_chip_init(tc3589x);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200427 if (ret)
Jingoo Han1383e002013-02-20 18:31:52 +0900428 return ret;
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200429
Lee Jonesa435ae12012-09-07 12:14:57 +0100430 ret = tc3589x_irq_init(tc3589x, np);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200431 if (ret)
Jingoo Han1383e002013-02-20 18:31:52 +0900432 return ret;
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200433
Sundar Iyer20406eb2010-12-13 09:33:14 +0530434 ret = request_threaded_irq(tc3589x->i2c->irq, NULL, tc3589x_irq,
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200435 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
Sundar Iyer20406eb2010-12-13 09:33:14 +0530436 "tc3589x", tc3589x);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200437 if (ret) {
Sundar Iyer20406eb2010-12-13 09:33:14 +0530438 dev_err(tc3589x->dev, "failed to request IRQ: %d\n", ret);
Jingoo Han1383e002013-02-20 18:31:52 +0900439 return ret;
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200440 }
441
Sundar Iyer611b7592010-12-13 09:33:15 +0530442 ret = tc3589x_device_init(tc3589x);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200443 if (ret) {
Sundar Iyer611b7592010-12-13 09:33:15 +0530444 dev_err(tc3589x->dev, "failed to add child devices\n");
Jingoo Han1383e002013-02-20 18:31:52 +0900445 return ret;
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200446 }
447
448 return 0;
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200449}
450
Bill Pemberton4740f732012-11-19 13:26:01 -0500451static int tc3589x_remove(struct i2c_client *client)
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200452{
Sundar Iyer20406eb2010-12-13 09:33:14 +0530453 struct tc3589x *tc3589x = i2c_get_clientdata(client);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200454
Sundar Iyer20406eb2010-12-13 09:33:14 +0530455 mfd_remove_devices(tc3589x->dev);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200456
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200457 return 0;
458}
459
Axel Lin930bf022012-07-02 17:19:52 +0800460#ifdef CONFIG_PM_SLEEP
Sundar Iyer593e9d72010-12-13 09:33:18 +0530461static int tc3589x_suspend(struct device *dev)
462{
463 struct tc3589x *tc3589x = dev_get_drvdata(dev);
464 struct i2c_client *client = tc3589x->i2c;
465 int ret = 0;
466
467 /* put the system to sleep mode */
468 if (!device_may_wakeup(&client->dev))
469 ret = tc3589x_reg_write(tc3589x, TC3589x_CLKMODE,
470 TC3589x_CLKMODE_MODCTL_SLEEP);
471
472 return ret;
473}
474
475static int tc3589x_resume(struct device *dev)
476{
477 struct tc3589x *tc3589x = dev_get_drvdata(dev);
478 struct i2c_client *client = tc3589x->i2c;
479 int ret = 0;
480
481 /* enable the system into operation */
482 if (!device_may_wakeup(&client->dev))
483 ret = tc3589x_reg_write(tc3589x, TC3589x_CLKMODE,
484 TC3589x_CLKMODE_MODCTL_OPERATION);
485
486 return ret;
487}
Linus Walleij54d8e2c2011-08-09 20:37:17 +0200488#endif
Sundar Iyer593e9d72010-12-13 09:33:18 +0530489
Axel Lin930bf022012-07-02 17:19:52 +0800490static SIMPLE_DEV_PM_OPS(tc3589x_dev_pm_ops, tc3589x_suspend, tc3589x_resume);
491
Sundar Iyer20406eb2010-12-13 09:33:14 +0530492static const struct i2c_device_id tc3589x_id[] = {
Linus Walleije64c1eb2013-10-18 11:51:45 +0200493 { "tc35890", TC3589X_TC35890 },
494 { "tc35892", TC3589X_TC35892 },
495 { "tc35893", TC3589X_TC35893 },
496 { "tc35894", TC3589X_TC35894 },
497 { "tc35895", TC3589X_TC35895 },
498 { "tc35896", TC3589X_TC35896 },
499 { "tc3589x", TC3589X_UNKNOWN },
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200500 { }
501};
Sundar Iyer20406eb2010-12-13 09:33:14 +0530502MODULE_DEVICE_TABLE(i2c, tc3589x_id);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200503
Sundar Iyer20406eb2010-12-13 09:33:14 +0530504static struct i2c_driver tc3589x_driver = {
Linus Walleija381b132014-01-23 13:43:28 +0100505 .driver = {
506 .name = "tc3589x",
507 .owner = THIS_MODULE,
508 .pm = &tc3589x_dev_pm_ops,
509 .of_match_table = of_match_ptr(tc3589x_match),
510 },
Sundar Iyer20406eb2010-12-13 09:33:14 +0530511 .probe = tc3589x_probe,
Bill Pemberton84449212012-11-19 13:20:24 -0500512 .remove = tc3589x_remove,
Sundar Iyer20406eb2010-12-13 09:33:14 +0530513 .id_table = tc3589x_id,
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200514};
515
Sundar Iyer20406eb2010-12-13 09:33:14 +0530516static int __init tc3589x_init(void)
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200517{
Sundar Iyer20406eb2010-12-13 09:33:14 +0530518 return i2c_add_driver(&tc3589x_driver);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200519}
Sundar Iyer20406eb2010-12-13 09:33:14 +0530520subsys_initcall(tc3589x_init);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200521
Sundar Iyer20406eb2010-12-13 09:33:14 +0530522static void __exit tc3589x_exit(void)
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200523{
Sundar Iyer20406eb2010-12-13 09:33:14 +0530524 i2c_del_driver(&tc3589x_driver);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200525}
Sundar Iyer20406eb2010-12-13 09:33:14 +0530526module_exit(tc3589x_exit);
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200527
528MODULE_LICENSE("GPL v2");
Sundar Iyer20406eb2010-12-13 09:33:14 +0530529MODULE_DESCRIPTION("TC3589x MFD core driver");
Rabin Vincentb4ecd322010-05-10 23:39:47 +0200530MODULE_AUTHOR("Hanumath Prasad, Rabin Vincent");