blob: 3ba048655bf37fe5a9b1f1ff2ad7a2e6748fe0c7 [file] [log] [blame]
Aaro Koskinenc7b76dc2012-11-18 18:36:20 +02001/*
2 * Retu MFD driver
3 *
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 */
36#define RETU_REG_IMR 0x02 /* Interrupt mask */
37
38/* Interrupt sources */
39#define RETU_INT_PWR 0 /* Power button */
40
41struct retu_dev {
42 struct regmap *regmap;
43 struct device *dev;
44 struct mutex mutex;
45 struct regmap_irq_chip_data *irq_data;
46};
47
48static struct resource retu_pwrbutton_res[] = {
49 {
50 .name = "retu-pwrbutton",
51 .start = RETU_INT_PWR,
52 .end = RETU_INT_PWR,
53 .flags = IORESOURCE_IRQ,
54 },
55};
56
57static struct mfd_cell retu_devs[] = {
58 {
59 .name = "retu-wdt"
60 },
61 {
62 .name = "retu-pwrbutton",
63 .resources = retu_pwrbutton_res,
64 .num_resources = ARRAY_SIZE(retu_pwrbutton_res),
65 }
66};
67
68static struct regmap_irq retu_irqs[] = {
69 [RETU_INT_PWR] = {
70 .mask = 1 << RETU_INT_PWR,
71 }
72};
73
74static struct regmap_irq_chip retu_irq_chip = {
75 .name = "RETU",
76 .irqs = retu_irqs,
77 .num_irqs = ARRAY_SIZE(retu_irqs),
78 .num_regs = 1,
79 .status_base = RETU_REG_IDR,
80 .mask_base = RETU_REG_IMR,
81 .ack_base = RETU_REG_IDR,
82};
83
84/* Retu device registered for the power off. */
85static struct retu_dev *retu_pm_power_off;
86
87int retu_read(struct retu_dev *rdev, u8 reg)
88{
89 int ret;
90 int value;
91
92 mutex_lock(&rdev->mutex);
93 ret = regmap_read(rdev->regmap, reg, &value);
94 mutex_unlock(&rdev->mutex);
95
96 return ret ? ret : value;
97}
98EXPORT_SYMBOL_GPL(retu_read);
99
100int retu_write(struct retu_dev *rdev, u8 reg, u16 data)
101{
102 int ret;
103
104 mutex_lock(&rdev->mutex);
105 ret = regmap_write(rdev->regmap, reg, data);
106 mutex_unlock(&rdev->mutex);
107
108 return ret;
109}
110EXPORT_SYMBOL_GPL(retu_write);
111
112static void retu_power_off(void)
113{
114 struct retu_dev *rdev = retu_pm_power_off;
115 int reg;
116
117 mutex_lock(&retu_pm_power_off->mutex);
118
119 /* Ignore power button state */
120 regmap_read(rdev->regmap, RETU_REG_CC1, &reg);
121 regmap_write(rdev->regmap, RETU_REG_CC1, reg | 2);
122
123 /* Expire watchdog immediately */
124 regmap_write(rdev->regmap, RETU_REG_WATCHDOG, 0);
125
126 /* Wait for poweroff */
127 for (;;)
128 cpu_relax();
129
130 mutex_unlock(&retu_pm_power_off->mutex);
131}
132
133static int retu_regmap_read(void *context, const void *reg, size_t reg_size,
134 void *val, size_t val_size)
135{
136 int ret;
137 struct device *dev = context;
138 struct i2c_client *i2c = to_i2c_client(dev);
139
140 BUG_ON(reg_size != 1 || val_size != 2);
141
142 ret = i2c_smbus_read_word_data(i2c, *(u8 const *)reg);
143 if (ret < 0)
144 return ret;
145
146 *(u16 *)val = ret;
147 return 0;
148}
149
150static int retu_regmap_write(void *context, const void *data, size_t count)
151{
152 u8 reg;
153 u16 val;
154 struct device *dev = context;
155 struct i2c_client *i2c = to_i2c_client(dev);
156
157 BUG_ON(count != sizeof(reg) + sizeof(val));
158 memcpy(&reg, data, sizeof(reg));
159 memcpy(&val, data + sizeof(reg), sizeof(val));
160 return i2c_smbus_write_word_data(i2c, reg, val);
161}
162
163static struct regmap_bus retu_bus = {
164 .read = retu_regmap_read,
165 .write = retu_regmap_write,
166 .val_format_endian_default = REGMAP_ENDIAN_NATIVE,
167};
168
169static struct regmap_config retu_config = {
170 .reg_bits = 8,
171 .val_bits = 16,
172};
173
Greg Kroah-Hartman612b95c2012-12-21 15:03:15 -0800174static int retu_probe(struct i2c_client *i2c, const struct i2c_device_id *id)
Aaro Koskinenc7b76dc2012-11-18 18:36:20 +0200175{
176 struct retu_dev *rdev;
177 int ret;
178
179 rdev = devm_kzalloc(&i2c->dev, sizeof(*rdev), GFP_KERNEL);
180 if (rdev == NULL)
181 return -ENOMEM;
182
183 i2c_set_clientdata(i2c, rdev);
184 rdev->dev = &i2c->dev;
185 mutex_init(&rdev->mutex);
186 rdev->regmap = devm_regmap_init(&i2c->dev, &retu_bus, &i2c->dev,
187 &retu_config);
188 if (IS_ERR(rdev->regmap))
189 return PTR_ERR(rdev->regmap);
190
191 ret = retu_read(rdev, RETU_REG_ASICR);
192 if (ret < 0) {
193 dev_err(rdev->dev, "could not read Retu revision: %d\n", ret);
194 return ret;
195 }
196
197 dev_info(rdev->dev, "Retu%s v%d.%d found\n",
198 (ret & RETU_REG_ASICR_VILMA) ? " & Vilma" : "",
199 (ret >> 4) & 0x7, ret & 0xf);
200
201 /* Mask all RETU interrupts. */
202 ret = retu_write(rdev, RETU_REG_IMR, 0xffff);
203 if (ret < 0)
204 return ret;
205
206 ret = regmap_add_irq_chip(rdev->regmap, i2c->irq, IRQF_ONESHOT, -1,
207 &retu_irq_chip, &rdev->irq_data);
208 if (ret < 0)
209 return ret;
210
211 ret = mfd_add_devices(rdev->dev, -1, retu_devs, ARRAY_SIZE(retu_devs),
212 NULL, regmap_irq_chip_get_base(rdev->irq_data),
213 NULL);
214 if (ret < 0) {
215 regmap_del_irq_chip(i2c->irq, rdev->irq_data);
216 return ret;
217 }
218
219 if (!pm_power_off) {
220 retu_pm_power_off = rdev;
221 pm_power_off = retu_power_off;
222 }
223
224 return 0;
225}
226
Greg Kroah-Hartman612b95c2012-12-21 15:03:15 -0800227static int retu_remove(struct i2c_client *i2c)
Aaro Koskinenc7b76dc2012-11-18 18:36:20 +0200228{
229 struct retu_dev *rdev = i2c_get_clientdata(i2c);
230
231 if (retu_pm_power_off == rdev) {
232 pm_power_off = NULL;
233 retu_pm_power_off = NULL;
234 }
235 mfd_remove_devices(rdev->dev);
236 regmap_del_irq_chip(i2c->irq, rdev->irq_data);
237
238 return 0;
239}
240
241static const struct i2c_device_id retu_id[] = {
242 { "retu-mfd", 0 },
243 { }
244};
245MODULE_DEVICE_TABLE(i2c, retu_id);
246
247static struct i2c_driver retu_driver = {
248 .driver = {
249 .name = "retu-mfd",
250 .owner = THIS_MODULE,
251 },
252 .probe = retu_probe,
253 .remove = retu_remove,
254 .id_table = retu_id,
255};
256module_i2c_driver(retu_driver);
257
258MODULE_DESCRIPTION("Retu MFD driver");
259MODULE_AUTHOR("Juha Yrjölä");
260MODULE_AUTHOR("David Weinehall");
261MODULE_AUTHOR("Mikko Ylinen");
262MODULE_AUTHOR("Aaro Koskinen <aaro.koskinen@iki.fi>");
263MODULE_LICENSE("GPL");