blob: a1830986eeb71006b9c693e0aab1bb5e25d98e81 [file] [log] [blame]
Aaro Koskinenc7b76dc2012-11-18 18:36:20 +02001/*
Aaro Koskinen95e50f62013-04-09 22:51:25 +03002 * Retu/Tahvo MFD driver
Aaro Koskinenc7b76dc2012-11-18 18:36:20 +02003 *
4 * Copyright (C) 2004, 2005 Nokia Corporation
5 *
6 * Based on code written by Juha Yrjölä, David Weinehall and Mikko Ylinen.
7 * Rewritten by Aaro Koskinen.
8 *
9 * This file is subject to the terms and conditions of the GNU General
10 * Public License. See the file "COPYING" in the main directory of this
11 * archive for more details.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 */
18
19#include <linux/err.h>
20#include <linux/i2c.h>
21#include <linux/irq.h>
22#include <linux/init.h>
23#include <linux/slab.h>
24#include <linux/mutex.h>
25#include <linux/module.h>
26#include <linux/regmap.h>
27#include <linux/mfd/core.h>
28#include <linux/mfd/retu.h>
29#include <linux/interrupt.h>
30#include <linux/moduleparam.h>
31
32/* Registers */
33#define RETU_REG_ASICR 0x00 /* ASIC ID and revision */
34#define RETU_REG_ASICR_VILMA (1 << 7) /* Bit indicating Vilma */
35#define RETU_REG_IDR 0x01 /* Interrupt ID */
Aaro Koskinen95e50f62013-04-09 22:51:25 +030036#define RETU_REG_IMR 0x02 /* Interrupt mask (Retu) */
37#define TAHVO_REG_IMR 0x03 /* Interrupt mask (Tahvo) */
Aaro Koskinenc7b76dc2012-11-18 18:36:20 +020038
39/* Interrupt sources */
40#define RETU_INT_PWR 0 /* Power button */
41
42struct retu_dev {
43 struct regmap *regmap;
44 struct device *dev;
45 struct mutex mutex;
46 struct regmap_irq_chip_data *irq_data;
47};
48
49static struct resource retu_pwrbutton_res[] = {
50 {
51 .name = "retu-pwrbutton",
52 .start = RETU_INT_PWR,
53 .end = RETU_INT_PWR,
54 .flags = IORESOURCE_IRQ,
55 },
56};
57
58static struct mfd_cell retu_devs[] = {
59 {
60 .name = "retu-wdt"
61 },
62 {
63 .name = "retu-pwrbutton",
64 .resources = retu_pwrbutton_res,
65 .num_resources = ARRAY_SIZE(retu_pwrbutton_res),
66 }
67};
68
69static struct regmap_irq retu_irqs[] = {
70 [RETU_INT_PWR] = {
71 .mask = 1 << RETU_INT_PWR,
72 }
73};
74
75static struct regmap_irq_chip retu_irq_chip = {
76 .name = "RETU",
77 .irqs = retu_irqs,
78 .num_irqs = ARRAY_SIZE(retu_irqs),
79 .num_regs = 1,
80 .status_base = RETU_REG_IDR,
81 .mask_base = RETU_REG_IMR,
82 .ack_base = RETU_REG_IDR,
83};
84
85/* Retu device registered for the power off. */
86static struct retu_dev *retu_pm_power_off;
87
Aaro Koskinen95e50f62013-04-09 22:51:25 +030088static struct resource tahvo_usb_res[] = {
89 {
90 .name = "tahvo-usb",
91 .start = TAHVO_INT_VBUS,
92 .end = TAHVO_INT_VBUS,
93 .flags = IORESOURCE_IRQ,
94 },
95};
96
97static struct mfd_cell tahvo_devs[] = {
98 {
99 .name = "tahvo-usb",
100 .resources = tahvo_usb_res,
101 .num_resources = ARRAY_SIZE(tahvo_usb_res),
102 },
103};
104
105static struct regmap_irq tahvo_irqs[] = {
106 [TAHVO_INT_VBUS] = {
107 .mask = 1 << TAHVO_INT_VBUS,
108 }
109};
110
111static struct regmap_irq_chip tahvo_irq_chip = {
112 .name = "TAHVO",
113 .irqs = tahvo_irqs,
114 .num_irqs = ARRAY_SIZE(tahvo_irqs),
115 .num_regs = 1,
116 .status_base = RETU_REG_IDR,
117 .mask_base = TAHVO_REG_IMR,
118 .ack_base = RETU_REG_IDR,
119};
120
121static const struct retu_data {
122 char *chip_name;
123 char *companion_name;
124 struct regmap_irq_chip *irq_chip;
125 struct mfd_cell *children;
126 int nchildren;
127} retu_data[] = {
128 [0] = {
129 .chip_name = "Retu",
130 .companion_name = "Vilma",
131 .irq_chip = &retu_irq_chip,
132 .children = retu_devs,
133 .nchildren = ARRAY_SIZE(retu_devs),
134 },
135 [1] = {
136 .chip_name = "Tahvo",
137 .companion_name = "Betty",
138 .irq_chip = &tahvo_irq_chip,
139 .children = tahvo_devs,
140 .nchildren = ARRAY_SIZE(tahvo_devs),
141 }
142};
143
Aaro Koskinenc7b76dc2012-11-18 18:36:20 +0200144int retu_read(struct retu_dev *rdev, u8 reg)
145{
146 int ret;
147 int value;
148
149 mutex_lock(&rdev->mutex);
150 ret = regmap_read(rdev->regmap, reg, &value);
151 mutex_unlock(&rdev->mutex);
152
153 return ret ? ret : value;
154}
155EXPORT_SYMBOL_GPL(retu_read);
156
157int retu_write(struct retu_dev *rdev, u8 reg, u16 data)
158{
159 int ret;
160
161 mutex_lock(&rdev->mutex);
162 ret = regmap_write(rdev->regmap, reg, data);
163 mutex_unlock(&rdev->mutex);
164
165 return ret;
166}
167EXPORT_SYMBOL_GPL(retu_write);
168
169static void retu_power_off(void)
170{
171 struct retu_dev *rdev = retu_pm_power_off;
172 int reg;
173
174 mutex_lock(&retu_pm_power_off->mutex);
175
176 /* Ignore power button state */
177 regmap_read(rdev->regmap, RETU_REG_CC1, &reg);
178 regmap_write(rdev->regmap, RETU_REG_CC1, reg | 2);
179
180 /* Expire watchdog immediately */
181 regmap_write(rdev->regmap, RETU_REG_WATCHDOG, 0);
182
183 /* Wait for poweroff */
184 for (;;)
185 cpu_relax();
186
187 mutex_unlock(&retu_pm_power_off->mutex);
188}
189
190static int retu_regmap_read(void *context, const void *reg, size_t reg_size,
191 void *val, size_t val_size)
192{
193 int ret;
194 struct device *dev = context;
195 struct i2c_client *i2c = to_i2c_client(dev);
196
197 BUG_ON(reg_size != 1 || val_size != 2);
198
199 ret = i2c_smbus_read_word_data(i2c, *(u8 const *)reg);
200 if (ret < 0)
201 return ret;
202
203 *(u16 *)val = ret;
204 return 0;
205}
206
207static int retu_regmap_write(void *context, const void *data, size_t count)
208{
209 u8 reg;
210 u16 val;
211 struct device *dev = context;
212 struct i2c_client *i2c = to_i2c_client(dev);
213
214 BUG_ON(count != sizeof(reg) + sizeof(val));
215 memcpy(&reg, data, sizeof(reg));
216 memcpy(&val, data + sizeof(reg), sizeof(val));
217 return i2c_smbus_write_word_data(i2c, reg, val);
218}
219
220static struct regmap_bus retu_bus = {
221 .read = retu_regmap_read,
222 .write = retu_regmap_write,
223 .val_format_endian_default = REGMAP_ENDIAN_NATIVE,
224};
225
226static struct regmap_config retu_config = {
227 .reg_bits = 8,
228 .val_bits = 16,
229};
230
Greg Kroah-Hartman612b95c2012-12-21 15:03:15 -0800231static int retu_probe(struct i2c_client *i2c, const struct i2c_device_id *id)
Aaro Koskinenc7b76dc2012-11-18 18:36:20 +0200232{
Aaro Koskinen95e50f62013-04-09 22:51:25 +0300233 struct retu_data const *rdat;
Aaro Koskinenc7b76dc2012-11-18 18:36:20 +0200234 struct retu_dev *rdev;
235 int ret;
236
Aaro Koskinen95e50f62013-04-09 22:51:25 +0300237 if (i2c->addr > ARRAY_SIZE(retu_data))
238 return -ENODEV;
239 rdat = &retu_data[i2c->addr - 1];
240
Aaro Koskinenc7b76dc2012-11-18 18:36:20 +0200241 rdev = devm_kzalloc(&i2c->dev, sizeof(*rdev), GFP_KERNEL);
242 if (rdev == NULL)
243 return -ENOMEM;
244
245 i2c_set_clientdata(i2c, rdev);
246 rdev->dev = &i2c->dev;
247 mutex_init(&rdev->mutex);
248 rdev->regmap = devm_regmap_init(&i2c->dev, &retu_bus, &i2c->dev,
249 &retu_config);
250 if (IS_ERR(rdev->regmap))
251 return PTR_ERR(rdev->regmap);
252
253 ret = retu_read(rdev, RETU_REG_ASICR);
254 if (ret < 0) {
Aaro Koskinen95e50f62013-04-09 22:51:25 +0300255 dev_err(rdev->dev, "could not read %s revision: %d\n",
256 rdat->chip_name, ret);
Aaro Koskinenc7b76dc2012-11-18 18:36:20 +0200257 return ret;
258 }
259
Aaro Koskinen95e50f62013-04-09 22:51:25 +0300260 dev_info(rdev->dev, "%s%s%s v%d.%d found\n", rdat->chip_name,
261 (ret & RETU_REG_ASICR_VILMA) ? " & " : "",
262 (ret & RETU_REG_ASICR_VILMA) ? rdat->companion_name : "",
Aaro Koskinenc7b76dc2012-11-18 18:36:20 +0200263 (ret >> 4) & 0x7, ret & 0xf);
264
Aaro Koskinen95e50f62013-04-09 22:51:25 +0300265 /* Mask all interrupts. */
266 ret = retu_write(rdev, rdat->irq_chip->mask_base, 0xffff);
Aaro Koskinenc7b76dc2012-11-18 18:36:20 +0200267 if (ret < 0)
268 return ret;
269
270 ret = regmap_add_irq_chip(rdev->regmap, i2c->irq, IRQF_ONESHOT, -1,
Aaro Koskinen95e50f62013-04-09 22:51:25 +0300271 rdat->irq_chip, &rdev->irq_data);
Aaro Koskinenc7b76dc2012-11-18 18:36:20 +0200272 if (ret < 0)
273 return ret;
274
Aaro Koskinen95e50f62013-04-09 22:51:25 +0300275 ret = mfd_add_devices(rdev->dev, -1, rdat->children, rdat->nchildren,
Aaro Koskinenc7b76dc2012-11-18 18:36:20 +0200276 NULL, regmap_irq_chip_get_base(rdev->irq_data),
277 NULL);
278 if (ret < 0) {
279 regmap_del_irq_chip(i2c->irq, rdev->irq_data);
280 return ret;
281 }
282
Aaro Koskinen95e50f62013-04-09 22:51:25 +0300283 if (i2c->addr == 1 && !pm_power_off) {
Aaro Koskinenc7b76dc2012-11-18 18:36:20 +0200284 retu_pm_power_off = rdev;
285 pm_power_off = retu_power_off;
286 }
287
288 return 0;
289}
290
Greg Kroah-Hartman612b95c2012-12-21 15:03:15 -0800291static int retu_remove(struct i2c_client *i2c)
Aaro Koskinenc7b76dc2012-11-18 18:36:20 +0200292{
293 struct retu_dev *rdev = i2c_get_clientdata(i2c);
294
295 if (retu_pm_power_off == rdev) {
296 pm_power_off = NULL;
297 retu_pm_power_off = NULL;
298 }
299 mfd_remove_devices(rdev->dev);
300 regmap_del_irq_chip(i2c->irq, rdev->irq_data);
301
302 return 0;
303}
304
305static const struct i2c_device_id retu_id[] = {
306 { "retu-mfd", 0 },
Aaro Koskinen95e50f62013-04-09 22:51:25 +0300307 { "tahvo-mfd", 0 },
Aaro Koskinenc7b76dc2012-11-18 18:36:20 +0200308 { }
309};
310MODULE_DEVICE_TABLE(i2c, retu_id);
311
312static struct i2c_driver retu_driver = {
313 .driver = {
314 .name = "retu-mfd",
315 .owner = THIS_MODULE,
316 },
317 .probe = retu_probe,
318 .remove = retu_remove,
319 .id_table = retu_id,
320};
321module_i2c_driver(retu_driver);
322
323MODULE_DESCRIPTION("Retu MFD driver");
324MODULE_AUTHOR("Juha Yrjölä");
325MODULE_AUTHOR("David Weinehall");
326MODULE_AUTHOR("Mikko Ylinen");
327MODULE_AUTHOR("Aaro Koskinen <aaro.koskinen@iki.fi>");
328MODULE_LICENSE("GPL");