Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 1 | /* |
| 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 Jones | 15e27b1 | 2012-09-07 12:14:56 +0100 | [diff] [blame] | 12 | #include <linux/irqdomain.h> |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 13 | #include <linux/slab.h> |
| 14 | #include <linux/i2c.h> |
Lee Jones | a435ae1 | 2012-09-07 12:14:57 +0100 | [diff] [blame] | 15 | #include <linux/of.h> |
Linus Walleij | a381b13 | 2014-01-23 13:43:28 +0100 | [diff] [blame] | 16 | #include <linux/of_device.h> |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 17 | #include <linux/mfd/core.h> |
Sundar Iyer | c6eda6c | 2010-12-13 09:33:12 +0530 | [diff] [blame] | 18 | #include <linux/mfd/tc3589x.h> |
Linus Walleij | a381b13 | 2014-01-23 13:43:28 +0100 | [diff] [blame] | 19 | #include <linux/err.h> |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 20 | |
Linus Walleij | e64c1eb | 2013-10-18 11:51:45 +0200 | [diff] [blame] | 21 | /** |
| 22 | * enum tc3589x_version - indicates the TC3589x version |
| 23 | */ |
| 24 | enum 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 Iyer | 593e9d7 | 2010-12-13 09:33:18 +0530 | [diff] [blame] | 34 | #define TC3589x_CLKMODE_MODCTL_SLEEP 0x0 |
| 35 | #define TC3589x_CLKMODE_MODCTL_OPERATION (1 << 0) |
| 36 | |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 37 | /** |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 38 | * tc3589x_reg_read() - read a single TC3589x register |
| 39 | * @tc3589x: Device to read from |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 40 | * @reg: Register to read |
| 41 | */ |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 42 | int tc3589x_reg_read(struct tc3589x *tc3589x, u8 reg) |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 43 | { |
| 44 | int ret; |
| 45 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 46 | ret = i2c_smbus_read_byte_data(tc3589x->i2c, reg); |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 47 | if (ret < 0) |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 48 | dev_err(tc3589x->dev, "failed to read reg %#x: %d\n", |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 49 | reg, ret); |
| 50 | |
| 51 | return ret; |
| 52 | } |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 53 | EXPORT_SYMBOL_GPL(tc3589x_reg_read); |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 54 | |
| 55 | /** |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 56 | * tc3589x_reg_read() - write a single TC3589x register |
| 57 | * @tc3589x: Device to write to |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 58 | * @reg: Register to read |
| 59 | * @data: Value to write |
| 60 | */ |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 61 | int tc3589x_reg_write(struct tc3589x *tc3589x, u8 reg, u8 data) |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 62 | { |
| 63 | int ret; |
| 64 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 65 | ret = i2c_smbus_write_byte_data(tc3589x->i2c, reg, data); |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 66 | if (ret < 0) |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 67 | dev_err(tc3589x->dev, "failed to write reg %#x: %d\n", |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 68 | reg, ret); |
| 69 | |
| 70 | return ret; |
| 71 | } |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 72 | EXPORT_SYMBOL_GPL(tc3589x_reg_write); |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 73 | |
| 74 | /** |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 75 | * tc3589x_block_read() - read multiple TC3589x registers |
| 76 | * @tc3589x: Device to read from |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 77 | * @reg: First register |
| 78 | * @length: Number of registers |
| 79 | * @values: Buffer to write to |
| 80 | */ |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 81 | int tc3589x_block_read(struct tc3589x *tc3589x, u8 reg, u8 length, u8 *values) |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 82 | { |
| 83 | int ret; |
| 84 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 85 | ret = i2c_smbus_read_i2c_block_data(tc3589x->i2c, reg, length, values); |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 86 | if (ret < 0) |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 87 | dev_err(tc3589x->dev, "failed to read regs %#x: %d\n", |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 88 | reg, ret); |
| 89 | |
| 90 | return ret; |
| 91 | } |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 92 | EXPORT_SYMBOL_GPL(tc3589x_block_read); |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 93 | |
| 94 | /** |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 95 | * tc3589x_block_write() - write multiple TC3589x registers |
| 96 | * @tc3589x: Device to write to |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 97 | * @reg: First register |
| 98 | * @length: Number of registers |
| 99 | * @values: Values to write |
| 100 | */ |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 101 | int tc3589x_block_write(struct tc3589x *tc3589x, u8 reg, u8 length, |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 102 | const u8 *values) |
| 103 | { |
| 104 | int ret; |
| 105 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 106 | ret = i2c_smbus_write_i2c_block_data(tc3589x->i2c, reg, length, |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 107 | values); |
| 108 | if (ret < 0) |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 109 | dev_err(tc3589x->dev, "failed to write regs %#x: %d\n", |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 110 | reg, ret); |
| 111 | |
| 112 | return ret; |
| 113 | } |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 114 | EXPORT_SYMBOL_GPL(tc3589x_block_write); |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 115 | |
| 116 | /** |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 117 | * tc3589x_set_bits() - set the value of a bitfield in a TC3589x register |
| 118 | * @tc3589x: Device to write to |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 119 | * @reg: Register to write |
| 120 | * @mask: Mask of bits to set |
| 121 | * @values: Value to set |
| 122 | */ |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 123 | int tc3589x_set_bits(struct tc3589x *tc3589x, u8 reg, u8 mask, u8 val) |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 124 | { |
| 125 | int ret; |
| 126 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 127 | mutex_lock(&tc3589x->lock); |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 128 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 129 | ret = tc3589x_reg_read(tc3589x, reg); |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 130 | if (ret < 0) |
| 131 | goto out; |
| 132 | |
| 133 | ret &= ~mask; |
| 134 | ret |= val; |
| 135 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 136 | ret = tc3589x_reg_write(tc3589x, reg, ret); |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 137 | |
| 138 | out: |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 139 | mutex_unlock(&tc3589x->lock); |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 140 | return ret; |
| 141 | } |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 142 | EXPORT_SYMBOL_GPL(tc3589x_set_bits); |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 143 | |
| 144 | static struct resource gpio_resources[] = { |
| 145 | { |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 146 | .start = TC3589x_INT_GPIIRQ, |
| 147 | .end = TC3589x_INT_GPIIRQ, |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 148 | .flags = IORESOURCE_IRQ, |
| 149 | }, |
| 150 | }; |
| 151 | |
Sundar Iyer | 09c730a | 2010-12-21 15:53:31 +0530 | [diff] [blame] | 152 | static struct resource keypad_resources[] = { |
| 153 | { |
| 154 | .start = TC3589x_INT_KBDIRQ, |
| 155 | .end = TC3589x_INT_KBDIRQ, |
| 156 | .flags = IORESOURCE_IRQ, |
| 157 | }, |
| 158 | }; |
| 159 | |
Geert Uytterhoeven | afb580a | 2013-11-18 14:33:03 +0100 | [diff] [blame] | 160 | static const struct mfd_cell tc3589x_dev_gpio[] = { |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 161 | { |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 162 | .name = "tc3589x-gpio", |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 163 | .num_resources = ARRAY_SIZE(gpio_resources), |
| 164 | .resources = &gpio_resources[0], |
Linus Walleij | a381b13 | 2014-01-23 13:43:28 +0100 | [diff] [blame] | 165 | .of_compatible = "toshiba,tc3589x-gpio", |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 166 | }, |
| 167 | }; |
| 168 | |
Geert Uytterhoeven | afb580a | 2013-11-18 14:33:03 +0100 | [diff] [blame] | 169 | static const struct mfd_cell tc3589x_dev_keypad[] = { |
Sundar Iyer | 09c730a | 2010-12-21 15:53:31 +0530 | [diff] [blame] | 170 | { |
| 171 | .name = "tc3589x-keypad", |
| 172 | .num_resources = ARRAY_SIZE(keypad_resources), |
| 173 | .resources = &keypad_resources[0], |
Linus Walleij | a381b13 | 2014-01-23 13:43:28 +0100 | [diff] [blame] | 174 | .of_compatible = "toshiba,tc3589x-keypad", |
Sundar Iyer | 09c730a | 2010-12-21 15:53:31 +0530 | [diff] [blame] | 175 | }, |
| 176 | }; |
| 177 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 178 | static irqreturn_t tc3589x_irq(int irq, void *data) |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 179 | { |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 180 | struct tc3589x *tc3589x = data; |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 181 | int status; |
| 182 | |
Sundar Iyer | bd77efd | 2010-12-13 09:33:16 +0530 | [diff] [blame] | 183 | again: |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 184 | status = tc3589x_reg_read(tc3589x, TC3589x_IRQST); |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 185 | if (status < 0) |
| 186 | return IRQ_NONE; |
| 187 | |
| 188 | while (status) { |
| 189 | int bit = __ffs(status); |
Lee Jones | 15e27b1 | 2012-09-07 12:14:56 +0100 | [diff] [blame] | 190 | int virq = irq_create_mapping(tc3589x->domain, bit); |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 191 | |
Lee Jones | 15e27b1 | 2012-09-07 12:14:56 +0100 | [diff] [blame] | 192 | handle_nested_irq(virq); |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 193 | 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 Iyer | bd77efd | 2010-12-13 09:33:16 +0530 | [diff] [blame] | 199 | * effect. In such a case, recheck for any interrupt which is still |
| 200 | * pending. |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 201 | */ |
Sundar Iyer | bd77efd | 2010-12-13 09:33:16 +0530 | [diff] [blame] | 202 | status = tc3589x_reg_read(tc3589x, TC3589x_IRQST); |
| 203 | if (status) |
| 204 | goto again; |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 205 | |
| 206 | return IRQ_HANDLED; |
| 207 | } |
| 208 | |
Lee Jones | 15e27b1 | 2012-09-07 12:14:56 +0100 | [diff] [blame] | 209 | static int tc3589x_irq_map(struct irq_domain *d, unsigned int virq, |
| 210 | irq_hw_number_t hwirq) |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 211 | { |
Lee Jones | 15e27b1 | 2012-09-07 12:14:56 +0100 | [diff] [blame] | 212 | struct tc3589x *tc3589x = d->host_data; |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 213 | |
Lee Jones | 15e27b1 | 2012-09-07 12:14:56 +0100 | [diff] [blame] | 214 | 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 Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 218 | #ifdef CONFIG_ARM |
Lee Jones | 15e27b1 | 2012-09-07 12:14:56 +0100 | [diff] [blame] | 219 | set_irq_flags(virq, IRQF_VALID); |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 220 | #else |
Lee Jones | 15e27b1 | 2012-09-07 12:14:56 +0100 | [diff] [blame] | 221 | irq_set_noprobe(virq); |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 222 | #endif |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 223 | |
| 224 | return 0; |
| 225 | } |
| 226 | |
Lee Jones | 15e27b1 | 2012-09-07 12:14:56 +0100 | [diff] [blame] | 227 | static 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 | |
| 236 | static struct irq_domain_ops tc3589x_irq_ops = { |
Linus Walleij | 1f0529b | 2013-01-02 14:40:14 +0100 | [diff] [blame] | 237 | .map = tc3589x_irq_map, |
Lee Jones | 15e27b1 | 2012-09-07 12:14:56 +0100 | [diff] [blame] | 238 | .unmap = tc3589x_irq_unmap, |
Linus Walleij | 627918e | 2014-06-10 14:31:32 +0200 | [diff] [blame] | 239 | .xlate = irq_domain_xlate_onecell, |
Lee Jones | 15e27b1 | 2012-09-07 12:14:56 +0100 | [diff] [blame] | 240 | }; |
| 241 | |
Lee Jones | a435ae1 | 2012-09-07 12:14:57 +0100 | [diff] [blame] | 242 | static int tc3589x_irq_init(struct tc3589x *tc3589x, struct device_node *np) |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 243 | { |
Linus Walleij | 1f0529b | 2013-01-02 14:40:14 +0100 | [diff] [blame] | 244 | tc3589x->domain = irq_domain_add_simple( |
Linus Walleij | 90f2d0f | 2014-10-28 11:06:56 +0100 | [diff] [blame] | 245 | np, TC3589x_NR_INTERNAL_IRQS, 0, |
Linus Walleij | 1f0529b | 2013-01-02 14:40:14 +0100 | [diff] [blame] | 246 | &tc3589x_irq_ops, tc3589x); |
Lee Jones | 15e27b1 | 2012-09-07 12:14:56 +0100 | [diff] [blame] | 247 | |
| 248 | if (!tc3589x->domain) { |
| 249 | dev_err(tc3589x->dev, "Failed to create irqdomain\n"); |
| 250 | return -ENOSYS; |
| 251 | } |
| 252 | |
| 253 | return 0; |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 254 | } |
| 255 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 256 | static int tc3589x_chip_init(struct tc3589x *tc3589x) |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 257 | { |
| 258 | int manf, ver, ret; |
| 259 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 260 | manf = tc3589x_reg_read(tc3589x, TC3589x_MANFCODE); |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 261 | if (manf < 0) |
| 262 | return manf; |
| 263 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 264 | ver = tc3589x_reg_read(tc3589x, TC3589x_VERSION); |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 265 | if (ver < 0) |
| 266 | return ver; |
| 267 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 268 | if (manf != TC3589x_MANFCODE_MAGIC) { |
| 269 | dev_err(tc3589x->dev, "unknown manufacturer: %#x\n", manf); |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 270 | return -EINVAL; |
| 271 | } |
| 272 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 273 | dev_info(tc3589x->dev, "manufacturer: %#x, version: %#x\n", manf, ver); |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 274 | |
Sundar Iyer | 523bc38 | 2010-12-13 09:33:17 +0530 | [diff] [blame] | 275 | /* |
| 276 | * Put everything except the IRQ module into reset; |
| 277 | * also spare the GPIO module for any pin initialization |
| 278 | * done during pre-kernel boot |
| 279 | */ |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 280 | ret = tc3589x_reg_write(tc3589x, TC3589x_RSTCTRL, |
| 281 | TC3589x_RSTCTRL_TIMRST |
| 282 | | TC3589x_RSTCTRL_ROTRST |
Sundar Iyer | 523bc38 | 2010-12-13 09:33:17 +0530 | [diff] [blame] | 283 | | TC3589x_RSTCTRL_KBDRST); |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 284 | if (ret < 0) |
| 285 | return ret; |
| 286 | |
| 287 | /* Clear the reset interrupt. */ |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 288 | return tc3589x_reg_write(tc3589x, TC3589x_RSTINTCLR, 0x1); |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 289 | } |
| 290 | |
Bill Pemberton | f791be4 | 2012-11-19 13:23:04 -0500 | [diff] [blame] | 291 | static int tc3589x_device_init(struct tc3589x *tc3589x) |
Sundar Iyer | 611b759 | 2010-12-13 09:33:15 +0530 | [diff] [blame] | 292 | { |
| 293 | int ret = 0; |
| 294 | unsigned int blocks = tc3589x->pdata->block; |
| 295 | |
| 296 | if (blocks & TC3589x_BLOCK_GPIO) { |
| 297 | ret = mfd_add_devices(tc3589x->dev, -1, tc3589x_dev_gpio, |
Mark Brown | 55692af | 2012-09-11 15:16:36 +0800 | [diff] [blame] | 298 | ARRAY_SIZE(tc3589x_dev_gpio), NULL, |
Linus Walleij | 90f2d0f | 2014-10-28 11:06:56 +0100 | [diff] [blame] | 299 | 0, tc3589x->domain); |
Sundar Iyer | 611b759 | 2010-12-13 09:33:15 +0530 | [diff] [blame] | 300 | if (ret) { |
| 301 | dev_err(tc3589x->dev, "failed to add gpio child\n"); |
| 302 | return ret; |
| 303 | } |
| 304 | dev_info(tc3589x->dev, "added gpio block\n"); |
| 305 | } |
| 306 | |
Sundar Iyer | 09c730a | 2010-12-21 15:53:31 +0530 | [diff] [blame] | 307 | if (blocks & TC3589x_BLOCK_KEYPAD) { |
| 308 | ret = mfd_add_devices(tc3589x->dev, -1, tc3589x_dev_keypad, |
Mark Brown | 55692af | 2012-09-11 15:16:36 +0800 | [diff] [blame] | 309 | ARRAY_SIZE(tc3589x_dev_keypad), NULL, |
Linus Walleij | 90f2d0f | 2014-10-28 11:06:56 +0100 | [diff] [blame] | 310 | 0, tc3589x->domain); |
Sundar Iyer | 09c730a | 2010-12-21 15:53:31 +0530 | [diff] [blame] | 311 | if (ret) { |
| 312 | dev_err(tc3589x->dev, "failed to keypad child\n"); |
| 313 | return ret; |
| 314 | } |
| 315 | dev_info(tc3589x->dev, "added keypad block\n"); |
| 316 | } |
Sundar Iyer | 611b759 | 2010-12-13 09:33:15 +0530 | [diff] [blame] | 317 | |
Sundar Iyer | 09c730a | 2010-12-21 15:53:31 +0530 | [diff] [blame] | 318 | return ret; |
Sundar Iyer | 611b759 | 2010-12-13 09:33:15 +0530 | [diff] [blame] | 319 | } |
| 320 | |
Linus Walleij | a381b13 | 2014-01-23 13:43:28 +0100 | [diff] [blame] | 321 | #ifdef CONFIG_OF |
| 322 | static const struct of_device_id tc3589x_match[] = { |
| 323 | /* Legacy compatible string */ |
| 324 | { .compatible = "tc3589x", .data = (void *) TC3589X_UNKNOWN }, |
| 325 | { .compatible = "toshiba,tc35890", .data = (void *) TC3589X_TC35890 }, |
| 326 | { .compatible = "toshiba,tc35892", .data = (void *) TC3589X_TC35892 }, |
| 327 | { .compatible = "toshiba,tc35893", .data = (void *) TC3589X_TC35893 }, |
| 328 | { .compatible = "toshiba,tc35894", .data = (void *) TC3589X_TC35894 }, |
| 329 | { .compatible = "toshiba,tc35895", .data = (void *) TC3589X_TC35895 }, |
| 330 | { .compatible = "toshiba,tc35896", .data = (void *) TC3589X_TC35896 }, |
| 331 | { } |
| 332 | }; |
| 333 | |
| 334 | MODULE_DEVICE_TABLE(of, tc3589x_match); |
| 335 | |
| 336 | static struct tc3589x_platform_data * |
| 337 | tc3589x_of_probe(struct device *dev, enum tc3589x_version *version) |
Lee Jones | a435ae1 | 2012-09-07 12:14:57 +0100 | [diff] [blame] | 338 | { |
Linus Walleij | a381b13 | 2014-01-23 13:43:28 +0100 | [diff] [blame] | 339 | struct device_node *np = dev->of_node; |
| 340 | struct tc3589x_platform_data *pdata; |
Lee Jones | a435ae1 | 2012-09-07 12:14:57 +0100 | [diff] [blame] | 341 | struct device_node *child; |
Linus Walleij | a381b13 | 2014-01-23 13:43:28 +0100 | [diff] [blame] | 342 | const struct of_device_id *of_id; |
| 343 | |
| 344 | pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL); |
| 345 | if (!pdata) |
| 346 | return ERR_PTR(-ENOMEM); |
| 347 | |
| 348 | of_id = of_match_device(tc3589x_match, dev); |
| 349 | if (!of_id) |
| 350 | return ERR_PTR(-ENODEV); |
| 351 | *version = (enum tc3589x_version) of_id->data; |
Lee Jones | a435ae1 | 2012-09-07 12:14:57 +0100 | [diff] [blame] | 352 | |
| 353 | for_each_child_of_node(np, child) { |
Linus Walleij | a381b13 | 2014-01-23 13:43:28 +0100 | [diff] [blame] | 354 | if (of_device_is_compatible(child, "toshiba,tc3589x-gpio")) |
Lee Jones | a435ae1 | 2012-09-07 12:14:57 +0100 | [diff] [blame] | 355 | pdata->block |= TC3589x_BLOCK_GPIO; |
Linus Walleij | a381b13 | 2014-01-23 13:43:28 +0100 | [diff] [blame] | 356 | if (of_device_is_compatible(child, "toshiba,tc3589x-keypad")) |
Lee Jones | a435ae1 | 2012-09-07 12:14:57 +0100 | [diff] [blame] | 357 | pdata->block |= TC3589x_BLOCK_KEYPAD; |
Lee Jones | a435ae1 | 2012-09-07 12:14:57 +0100 | [diff] [blame] | 358 | } |
| 359 | |
Linus Walleij | a381b13 | 2014-01-23 13:43:28 +0100 | [diff] [blame] | 360 | return pdata; |
Lee Jones | a435ae1 | 2012-09-07 12:14:57 +0100 | [diff] [blame] | 361 | } |
Linus Walleij | a381b13 | 2014-01-23 13:43:28 +0100 | [diff] [blame] | 362 | #else |
| 363 | static inline struct tc3589x_platform_data * |
| 364 | tc3589x_of_probe(struct device *dev, enum tc3589x_version *version) |
| 365 | { |
| 366 | dev_err(dev, "no device tree support\n"); |
| 367 | return ERR_PTR(-ENODEV); |
| 368 | } |
| 369 | #endif |
Lee Jones | a435ae1 | 2012-09-07 12:14:57 +0100 | [diff] [blame] | 370 | |
Bill Pemberton | f791be4 | 2012-11-19 13:23:04 -0500 | [diff] [blame] | 371 | static int tc3589x_probe(struct i2c_client *i2c, |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 372 | const struct i2c_device_id *id) |
| 373 | { |
Lee Jones | a435ae1 | 2012-09-07 12:14:57 +0100 | [diff] [blame] | 374 | struct device_node *np = i2c->dev.of_node; |
Linus Walleij | a381b13 | 2014-01-23 13:43:28 +0100 | [diff] [blame] | 375 | struct tc3589x_platform_data *pdata = dev_get_platdata(&i2c->dev); |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 376 | struct tc3589x *tc3589x; |
Linus Walleij | a381b13 | 2014-01-23 13:43:28 +0100 | [diff] [blame] | 377 | enum tc3589x_version version; |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 378 | int ret; |
| 379 | |
Lee Jones | a435ae1 | 2012-09-07 12:14:57 +0100 | [diff] [blame] | 380 | if (!pdata) { |
Linus Walleij | a381b13 | 2014-01-23 13:43:28 +0100 | [diff] [blame] | 381 | pdata = tc3589x_of_probe(&i2c->dev, &version); |
| 382 | if (IS_ERR(pdata)) { |
Lee Jones | a435ae1 | 2012-09-07 12:14:57 +0100 | [diff] [blame] | 383 | dev_err(&i2c->dev, "No platform data or DT found\n"); |
Linus Walleij | a381b13 | 2014-01-23 13:43:28 +0100 | [diff] [blame] | 384 | return PTR_ERR(pdata); |
Lee Jones | a435ae1 | 2012-09-07 12:14:57 +0100 | [diff] [blame] | 385 | } |
Linus Walleij | a381b13 | 2014-01-23 13:43:28 +0100 | [diff] [blame] | 386 | } else { |
| 387 | /* When not probing from device tree we have this ID */ |
| 388 | version = id->driver_data; |
Lee Jones | a435ae1 | 2012-09-07 12:14:57 +0100 | [diff] [blame] | 389 | } |
| 390 | |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 391 | if (!i2c_check_functionality(i2c->adapter, I2C_FUNC_SMBUS_BYTE_DATA |
| 392 | | I2C_FUNC_SMBUS_I2C_BLOCK)) |
| 393 | return -EIO; |
| 394 | |
Jingoo Han | 1383e00 | 2013-02-20 18:31:52 +0900 | [diff] [blame] | 395 | tc3589x = devm_kzalloc(&i2c->dev, sizeof(struct tc3589x), |
| 396 | GFP_KERNEL); |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 397 | if (!tc3589x) |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 398 | return -ENOMEM; |
| 399 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 400 | mutex_init(&tc3589x->lock); |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 401 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 402 | tc3589x->dev = &i2c->dev; |
| 403 | tc3589x->i2c = i2c; |
| 404 | tc3589x->pdata = pdata; |
Linus Walleij | e64c1eb | 2013-10-18 11:51:45 +0200 | [diff] [blame] | 405 | |
Linus Walleij | a381b13 | 2014-01-23 13:43:28 +0100 | [diff] [blame] | 406 | switch (version) { |
Linus Walleij | e64c1eb | 2013-10-18 11:51:45 +0200 | [diff] [blame] | 407 | case TC3589X_TC35893: |
| 408 | case TC3589X_TC35895: |
| 409 | case TC3589X_TC35896: |
| 410 | tc3589x->num_gpio = 20; |
| 411 | break; |
| 412 | case TC3589X_TC35890: |
| 413 | case TC3589X_TC35892: |
| 414 | case TC3589X_TC35894: |
| 415 | case TC3589X_UNKNOWN: |
| 416 | default: |
| 417 | tc3589x->num_gpio = 24; |
| 418 | break; |
| 419 | } |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 420 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 421 | i2c_set_clientdata(i2c, tc3589x); |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 422 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 423 | ret = tc3589x_chip_init(tc3589x); |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 424 | if (ret) |
Jingoo Han | 1383e00 | 2013-02-20 18:31:52 +0900 | [diff] [blame] | 425 | return ret; |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 426 | |
Lee Jones | a435ae1 | 2012-09-07 12:14:57 +0100 | [diff] [blame] | 427 | ret = tc3589x_irq_init(tc3589x, np); |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 428 | if (ret) |
Jingoo Han | 1383e00 | 2013-02-20 18:31:52 +0900 | [diff] [blame] | 429 | return ret; |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 430 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 431 | ret = request_threaded_irq(tc3589x->i2c->irq, NULL, tc3589x_irq, |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 432 | IRQF_TRIGGER_FALLING | IRQF_ONESHOT, |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 433 | "tc3589x", tc3589x); |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 434 | if (ret) { |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 435 | dev_err(tc3589x->dev, "failed to request IRQ: %d\n", ret); |
Jingoo Han | 1383e00 | 2013-02-20 18:31:52 +0900 | [diff] [blame] | 436 | return ret; |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 437 | } |
| 438 | |
Sundar Iyer | 611b759 | 2010-12-13 09:33:15 +0530 | [diff] [blame] | 439 | ret = tc3589x_device_init(tc3589x); |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 440 | if (ret) { |
Sundar Iyer | 611b759 | 2010-12-13 09:33:15 +0530 | [diff] [blame] | 441 | dev_err(tc3589x->dev, "failed to add child devices\n"); |
Jingoo Han | 1383e00 | 2013-02-20 18:31:52 +0900 | [diff] [blame] | 442 | return ret; |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | return 0; |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 446 | } |
| 447 | |
Bill Pemberton | 4740f73 | 2012-11-19 13:26:01 -0500 | [diff] [blame] | 448 | static int tc3589x_remove(struct i2c_client *client) |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 449 | { |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 450 | struct tc3589x *tc3589x = i2c_get_clientdata(client); |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 451 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 452 | mfd_remove_devices(tc3589x->dev); |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 453 | |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 454 | return 0; |
| 455 | } |
| 456 | |
Axel Lin | 930bf02 | 2012-07-02 17:19:52 +0800 | [diff] [blame] | 457 | #ifdef CONFIG_PM_SLEEP |
Sundar Iyer | 593e9d7 | 2010-12-13 09:33:18 +0530 | [diff] [blame] | 458 | static int tc3589x_suspend(struct device *dev) |
| 459 | { |
| 460 | struct tc3589x *tc3589x = dev_get_drvdata(dev); |
| 461 | struct i2c_client *client = tc3589x->i2c; |
| 462 | int ret = 0; |
| 463 | |
| 464 | /* put the system to sleep mode */ |
| 465 | if (!device_may_wakeup(&client->dev)) |
| 466 | ret = tc3589x_reg_write(tc3589x, TC3589x_CLKMODE, |
| 467 | TC3589x_CLKMODE_MODCTL_SLEEP); |
| 468 | |
| 469 | return ret; |
| 470 | } |
| 471 | |
| 472 | static int tc3589x_resume(struct device *dev) |
| 473 | { |
| 474 | struct tc3589x *tc3589x = dev_get_drvdata(dev); |
| 475 | struct i2c_client *client = tc3589x->i2c; |
| 476 | int ret = 0; |
| 477 | |
| 478 | /* enable the system into operation */ |
| 479 | if (!device_may_wakeup(&client->dev)) |
| 480 | ret = tc3589x_reg_write(tc3589x, TC3589x_CLKMODE, |
| 481 | TC3589x_CLKMODE_MODCTL_OPERATION); |
| 482 | |
| 483 | return ret; |
| 484 | } |
Linus Walleij | 54d8e2c | 2011-08-09 20:37:17 +0200 | [diff] [blame] | 485 | #endif |
Sundar Iyer | 593e9d7 | 2010-12-13 09:33:18 +0530 | [diff] [blame] | 486 | |
Axel Lin | 930bf02 | 2012-07-02 17:19:52 +0800 | [diff] [blame] | 487 | static SIMPLE_DEV_PM_OPS(tc3589x_dev_pm_ops, tc3589x_suspend, tc3589x_resume); |
| 488 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 489 | static const struct i2c_device_id tc3589x_id[] = { |
Linus Walleij | e64c1eb | 2013-10-18 11:51:45 +0200 | [diff] [blame] | 490 | { "tc35890", TC3589X_TC35890 }, |
| 491 | { "tc35892", TC3589X_TC35892 }, |
| 492 | { "tc35893", TC3589X_TC35893 }, |
| 493 | { "tc35894", TC3589X_TC35894 }, |
| 494 | { "tc35895", TC3589X_TC35895 }, |
| 495 | { "tc35896", TC3589X_TC35896 }, |
| 496 | { "tc3589x", TC3589X_UNKNOWN }, |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 497 | { } |
| 498 | }; |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 499 | MODULE_DEVICE_TABLE(i2c, tc3589x_id); |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 500 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 501 | static struct i2c_driver tc3589x_driver = { |
Linus Walleij | a381b13 | 2014-01-23 13:43:28 +0100 | [diff] [blame] | 502 | .driver = { |
| 503 | .name = "tc3589x", |
| 504 | .owner = THIS_MODULE, |
| 505 | .pm = &tc3589x_dev_pm_ops, |
| 506 | .of_match_table = of_match_ptr(tc3589x_match), |
| 507 | }, |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 508 | .probe = tc3589x_probe, |
Bill Pemberton | 8444921 | 2012-11-19 13:20:24 -0500 | [diff] [blame] | 509 | .remove = tc3589x_remove, |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 510 | .id_table = tc3589x_id, |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 511 | }; |
| 512 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 513 | static int __init tc3589x_init(void) |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 514 | { |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 515 | return i2c_add_driver(&tc3589x_driver); |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 516 | } |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 517 | subsys_initcall(tc3589x_init); |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 518 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 519 | static void __exit tc3589x_exit(void) |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 520 | { |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 521 | i2c_del_driver(&tc3589x_driver); |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 522 | } |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 523 | module_exit(tc3589x_exit); |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 524 | |
| 525 | MODULE_LICENSE("GPL v2"); |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 526 | MODULE_DESCRIPTION("TC3589x MFD core driver"); |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 527 | MODULE_AUTHOR("Hanumath Prasad, Rabin Vincent"); |