blob: 4f22efe04d3644740fc4609f504d8d26cff4bb72 [file] [log] [blame]
Anirudh Ghayalfcfbea62011-07-27 11:04:58 +05301/* Copyright (c) 2010-2011, Code Aurora Forum. All rights reserved.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
13#include <linux/module.h>
14#include <linux/init.h>
15#include <linux/kernel.h>
16#include <linux/errno.h>
17#include <linux/interrupt.h>
18#include <linux/platform_device.h>
19#include <linux/slab.h>
20#include <linux/mfd/pm8xxx/core.h>
21#include <linux/mfd/pm8xxx/vibrator.h>
22
23#include "../staging/android/timed_output.h"
24
25#define VIB_DRV 0x4A
26
27#define VIB_DRV_SEL_MASK 0xf8
28#define VIB_DRV_SEL_SHIFT 0x03
29#define VIB_DRV_EN_MANUAL_MASK 0xfc
Anirudh Ghayalc2019332011-11-12 06:29:10 +053030#define VIB_DRV_LOGIC_SHIFT 0x2
Anirudh Ghayalfcfbea62011-07-27 11:04:58 +053031
32#define VIB_MAX_LEVEL_mV 3100
33#define VIB_MIN_LEVEL_mV 1200
34
35struct pm8xxx_vib {
36 struct hrtimer vib_timer;
37 struct timed_output_dev timed_dev;
38 spinlock_t lock;
39 struct work_struct work;
40 struct device *dev;
41 const struct pm8xxx_vibrator_platform_data *pdata;
42 int state;
43 int level;
44 u8 reg_vib_drv;
45};
46
Anirudh Ghayalc2019332011-11-12 06:29:10 +053047static struct pm8xxx_vib *vib_dev;
48
49int pm8xxx_vibrator_config(struct pm8xxx_vib_config *vib_config)
50{
51 u8 reg = 0;
52 int rc;
53
54 if (vib_dev == NULL) {
55 pr_err("%s: vib_dev is NULL\n", __func__);
56 return -EINVAL;
57 }
58
59 if (vib_config->drive_mV) {
60 if ((vib_config->drive_mV < VIB_MIN_LEVEL_mV) ||
61 (vib_config->drive_mV > VIB_MAX_LEVEL_mV)) {
62 pr_err("Invalid vibrator drive strength\n");
63 return -EINVAL;
64 }
65 }
66
67 reg = (vib_config->drive_mV / 100) << VIB_DRV_SEL_SHIFT;
68
69 reg |= (!!vib_config->active_low) << VIB_DRV_LOGIC_SHIFT;
70
71 reg |= vib_config->enable_mode;
72
73 rc = pm8xxx_writeb(vib_dev->dev->parent, VIB_DRV, reg);
74 if (rc)
75 pr_err("%s: pm8xxx write failed: rc=%d\n", __func__, rc);
76
77 return rc;
78}
79EXPORT_SYMBOL(pm8xxx_vibrator_config);
80
Anirudh Ghayalfcfbea62011-07-27 11:04:58 +053081/* REVISIT: just for debugging, will be removed in final working version */
82static void __dump_vib_regs(struct pm8xxx_vib *vib, char *msg)
83{
84 u8 temp;
85
86 dev_dbg(vib->dev, "%s\n", msg);
87
88 pm8xxx_readb(vib->dev->parent, VIB_DRV, &temp);
89 dev_dbg(vib->dev, "VIB_DRV - %X\n", temp);
90}
91
92static int pm8xxx_vib_read_u8(struct pm8xxx_vib *vib,
93 u8 *data, u16 reg)
94{
95 int rc;
96
97 rc = pm8xxx_readb(vib->dev->parent, reg, data);
98 if (rc < 0)
99 dev_warn(vib->dev, "Error reading pm8xxx: %X - ret %X\n",
100 reg, rc);
101
102 return rc;
103}
104
105static int pm8xxx_vib_write_u8(struct pm8xxx_vib *vib,
106 u8 data, u16 reg)
107{
108 int rc;
109
110 rc = pm8xxx_writeb(vib->dev->parent, reg, data);
111 if (rc < 0)
112 dev_warn(vib->dev, "Error writing pm8xxx: %X - ret %X\n",
113 reg, rc);
114 return rc;
115}
116
117static int pm8xxx_vib_set(struct pm8xxx_vib *vib, int on)
118{
119 int rc;
120 u8 val;
121
122 if (on) {
123 val = vib->reg_vib_drv;
124 val |= ((vib->level << VIB_DRV_SEL_SHIFT) & VIB_DRV_SEL_MASK);
125 rc = pm8xxx_vib_write_u8(vib, val, VIB_DRV);
126 if (rc < 0)
127 return rc;
128 vib->reg_vib_drv = val;
129 } else {
130 val = vib->reg_vib_drv;
131 val &= ~VIB_DRV_SEL_MASK;
132 rc = pm8xxx_vib_write_u8(vib, val, VIB_DRV);
133 if (rc < 0)
134 return rc;
135 vib->reg_vib_drv = val;
136 }
137 __dump_vib_regs(vib, "vib_set_end");
138
139 return rc;
140}
141
142static void pm8xxx_vib_enable(struct timed_output_dev *dev, int value)
143{
144 struct pm8xxx_vib *vib = container_of(dev, struct pm8xxx_vib,
145 timed_dev);
146 unsigned long flags;
147
148 spin_lock_irqsave(&vib->lock, flags);
149
150retry:
151 if (hrtimer_try_to_cancel(&vib->vib_timer) < 0) {
152 spin_unlock_irqrestore(&vib->lock, flags);
153 cpu_relax();
154 goto retry;
155 }
156
157 if (value == 0)
158 vib->state = 0;
159 else {
160 value = (value > vib->pdata->max_timeout_ms ?
161 vib->pdata->max_timeout_ms : value);
162 vib->state = 1;
163 hrtimer_start(&vib->vib_timer,
164 ktime_set(value / 1000, (value % 1000) * 1000000),
165 HRTIMER_MODE_REL);
166 }
167 spin_unlock_irqrestore(&vib->lock, flags);
168 schedule_work(&vib->work);
169}
170
171static void pm8xxx_vib_update(struct work_struct *work)
172{
173 struct pm8xxx_vib *vib = container_of(work, struct pm8xxx_vib,
174 work);
175
176 pm8xxx_vib_set(vib, vib->state);
177}
178
179static int pm8xxx_vib_get_time(struct timed_output_dev *dev)
180{
181 struct pm8xxx_vib *vib = container_of(dev, struct pm8xxx_vib,
182 timed_dev);
183
184 if (hrtimer_active(&vib->vib_timer)) {
185 ktime_t r = hrtimer_get_remaining(&vib->vib_timer);
186 return (int)ktime_to_us(r);
187 } else
188 return 0;
189}
190
191static enum hrtimer_restart pm8xxx_vib_timer_func(struct hrtimer *timer)
192{
193 struct pm8xxx_vib *vib = container_of(timer, struct pm8xxx_vib,
194 vib_timer);
195
196 vib->state = 0;
197 schedule_work(&vib->work);
198
199 return HRTIMER_NORESTART;
200}
201
202#ifdef CONFIG_PM
203static int pm8xxx_vib_suspend(struct device *dev)
204{
205 struct pm8xxx_vib *vib = dev_get_drvdata(dev);
206
207 hrtimer_cancel(&vib->vib_timer);
208 cancel_work_sync(&vib->work);
209 /* turn-off vibrator */
210 pm8xxx_vib_set(vib, 0);
211
212 return 0;
213}
214
215static const struct dev_pm_ops pm8xxx_vib_pm_ops = {
216 .suspend = pm8xxx_vib_suspend,
217};
218#endif
219
220static int __devinit pm8xxx_vib_probe(struct platform_device *pdev)
221
222{
223 const struct pm8xxx_vibrator_platform_data *pdata =
224 pdev->dev.platform_data;
225 struct pm8xxx_vib *vib;
226 u8 val;
227 int rc;
228
229 if (!pdata)
230 return -EINVAL;
231
232 if (pdata->level_mV < VIB_MIN_LEVEL_mV ||
233 pdata->level_mV > VIB_MAX_LEVEL_mV)
234 return -EINVAL;
235
236 vib = kzalloc(sizeof(*vib), GFP_KERNEL);
237 if (!vib)
238 return -ENOMEM;
239
240 vib->pdata = pdata;
241 vib->level = pdata->level_mV / 100;
242 vib->dev = &pdev->dev;
243
244 spin_lock_init(&vib->lock);
245 INIT_WORK(&vib->work, pm8xxx_vib_update);
246
247 hrtimer_init(&vib->vib_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
248 vib->vib_timer.function = pm8xxx_vib_timer_func;
249
250 vib->timed_dev.name = "vibrator";
251 vib->timed_dev.get_time = pm8xxx_vib_get_time;
252 vib->timed_dev.enable = pm8xxx_vib_enable;
253
254 __dump_vib_regs(vib, "boot_vib_default");
255
256 /*
257 * Configure the vibrator, it operates in manual mode
258 * for timed_output framework.
259 */
260 rc = pm8xxx_vib_read_u8(vib, &val, VIB_DRV);
261 if (rc < 0)
262 goto err_read_vib;
263 val &= ~VIB_DRV_EN_MANUAL_MASK;
264 rc = pm8xxx_vib_write_u8(vib, val, VIB_DRV);
265 if (rc < 0)
266 goto err_read_vib;
267
268 vib->reg_vib_drv = val;
269
270 rc = timed_output_dev_register(&vib->timed_dev);
271 if (rc < 0)
272 goto err_read_vib;
273
274 pm8xxx_vib_enable(&vib->timed_dev, pdata->initial_vibrate_ms);
275
276 platform_set_drvdata(pdev, vib);
277
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530278 vib_dev = vib;
279
Anirudh Ghayalfcfbea62011-07-27 11:04:58 +0530280 return 0;
281
282err_read_vib:
283 kfree(vib);
284 return rc;
285}
286
287static int __devexit pm8xxx_vib_remove(struct platform_device *pdev)
288{
289 struct pm8xxx_vib *vib = platform_get_drvdata(pdev);
290
291 cancel_work_sync(&vib->work);
292 hrtimer_cancel(&vib->vib_timer);
293 timed_output_dev_unregister(&vib->timed_dev);
294 platform_set_drvdata(pdev, NULL);
295 kfree(vib);
296
297 return 0;
298}
299
300static struct platform_driver pm8xxx_vib_driver = {
301 .probe = pm8xxx_vib_probe,
302 .remove = __devexit_p(pm8xxx_vib_remove),
303 .driver = {
304 .name = PM8XXX_VIBRATOR_DEV_NAME,
305 .owner = THIS_MODULE,
306#ifdef CONFIG_PM
307 .pm = &pm8xxx_vib_pm_ops,
308#endif
309 },
310};
311
312static int __init pm8xxx_vib_init(void)
313{
314 return platform_driver_register(&pm8xxx_vib_driver);
315}
316module_init(pm8xxx_vib_init);
317
318static void __exit pm8xxx_vib_exit(void)
319{
320 platform_driver_unregister(&pm8xxx_vib_driver);
321}
322module_exit(pm8xxx_vib_exit);
323
324MODULE_ALIAS("platform:" PM8XXX_VIBRATOR_DEV_NAME);
325MODULE_DESCRIPTION("pm8xxx vibrator driver");
326MODULE_LICENSE("GPL v2");