blob: bfe5b3c124afbede91deb1e0be5f6965dd0aed8c [file] [log] [blame]
Duy Truong790f06d2013-02-13 16:38:12 -08001/* Copyright (c) 2010-2011, The Linux Foundation. All rights reserved.
Anirudh Ghayalfcfbea62011-07-27 11:04:58 +05302 *
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
Anirudh Ghayalfcfbea62011-07-27 11:04:58 +0530148retry:
Anirudh Ghayal83ba0ae2011-11-29 14:16:58 +0530149 spin_lock_irqsave(&vib->lock, flags);
Anirudh Ghayalfcfbea62011-07-27 11:04:58 +0530150 if (hrtimer_try_to_cancel(&vib->vib_timer) < 0) {
151 spin_unlock_irqrestore(&vib->lock, flags);
152 cpu_relax();
153 goto retry;
154 }
155
156 if (value == 0)
157 vib->state = 0;
158 else {
159 value = (value > vib->pdata->max_timeout_ms ?
160 vib->pdata->max_timeout_ms : value);
161 vib->state = 1;
162 hrtimer_start(&vib->vib_timer,
163 ktime_set(value / 1000, (value % 1000) * 1000000),
164 HRTIMER_MODE_REL);
165 }
166 spin_unlock_irqrestore(&vib->lock, flags);
167 schedule_work(&vib->work);
168}
169
170static void pm8xxx_vib_update(struct work_struct *work)
171{
172 struct pm8xxx_vib *vib = container_of(work, struct pm8xxx_vib,
173 work);
174
175 pm8xxx_vib_set(vib, vib->state);
176}
177
178static int pm8xxx_vib_get_time(struct timed_output_dev *dev)
179{
180 struct pm8xxx_vib *vib = container_of(dev, struct pm8xxx_vib,
181 timed_dev);
182
183 if (hrtimer_active(&vib->vib_timer)) {
184 ktime_t r = hrtimer_get_remaining(&vib->vib_timer);
185 return (int)ktime_to_us(r);
186 } else
187 return 0;
188}
189
190static enum hrtimer_restart pm8xxx_vib_timer_func(struct hrtimer *timer)
191{
192 struct pm8xxx_vib *vib = container_of(timer, struct pm8xxx_vib,
193 vib_timer);
194
195 vib->state = 0;
196 schedule_work(&vib->work);
197
198 return HRTIMER_NORESTART;
199}
200
201#ifdef CONFIG_PM
202static int pm8xxx_vib_suspend(struct device *dev)
203{
204 struct pm8xxx_vib *vib = dev_get_drvdata(dev);
205
206 hrtimer_cancel(&vib->vib_timer);
207 cancel_work_sync(&vib->work);
208 /* turn-off vibrator */
209 pm8xxx_vib_set(vib, 0);
210
211 return 0;
212}
213
214static const struct dev_pm_ops pm8xxx_vib_pm_ops = {
215 .suspend = pm8xxx_vib_suspend,
216};
217#endif
218
219static int __devinit pm8xxx_vib_probe(struct platform_device *pdev)
220
221{
222 const struct pm8xxx_vibrator_platform_data *pdata =
223 pdev->dev.platform_data;
224 struct pm8xxx_vib *vib;
225 u8 val;
226 int rc;
227
228 if (!pdata)
229 return -EINVAL;
230
231 if (pdata->level_mV < VIB_MIN_LEVEL_mV ||
232 pdata->level_mV > VIB_MAX_LEVEL_mV)
233 return -EINVAL;
234
235 vib = kzalloc(sizeof(*vib), GFP_KERNEL);
236 if (!vib)
237 return -ENOMEM;
238
239 vib->pdata = pdata;
240 vib->level = pdata->level_mV / 100;
241 vib->dev = &pdev->dev;
242
243 spin_lock_init(&vib->lock);
244 INIT_WORK(&vib->work, pm8xxx_vib_update);
245
246 hrtimer_init(&vib->vib_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
247 vib->vib_timer.function = pm8xxx_vib_timer_func;
248
249 vib->timed_dev.name = "vibrator";
250 vib->timed_dev.get_time = pm8xxx_vib_get_time;
251 vib->timed_dev.enable = pm8xxx_vib_enable;
252
253 __dump_vib_regs(vib, "boot_vib_default");
254
255 /*
256 * Configure the vibrator, it operates in manual mode
257 * for timed_output framework.
258 */
259 rc = pm8xxx_vib_read_u8(vib, &val, VIB_DRV);
260 if (rc < 0)
261 goto err_read_vib;
262 val &= ~VIB_DRV_EN_MANUAL_MASK;
263 rc = pm8xxx_vib_write_u8(vib, val, VIB_DRV);
264 if (rc < 0)
265 goto err_read_vib;
266
267 vib->reg_vib_drv = val;
268
269 rc = timed_output_dev_register(&vib->timed_dev);
270 if (rc < 0)
271 goto err_read_vib;
272
273 pm8xxx_vib_enable(&vib->timed_dev, pdata->initial_vibrate_ms);
274
275 platform_set_drvdata(pdev, vib);
276
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530277 vib_dev = vib;
278
Anirudh Ghayalfcfbea62011-07-27 11:04:58 +0530279 return 0;
280
281err_read_vib:
282 kfree(vib);
283 return rc;
284}
285
286static int __devexit pm8xxx_vib_remove(struct platform_device *pdev)
287{
288 struct pm8xxx_vib *vib = platform_get_drvdata(pdev);
289
290 cancel_work_sync(&vib->work);
291 hrtimer_cancel(&vib->vib_timer);
292 timed_output_dev_unregister(&vib->timed_dev);
293 platform_set_drvdata(pdev, NULL);
294 kfree(vib);
295
296 return 0;
297}
298
299static struct platform_driver pm8xxx_vib_driver = {
300 .probe = pm8xxx_vib_probe,
301 .remove = __devexit_p(pm8xxx_vib_remove),
302 .driver = {
303 .name = PM8XXX_VIBRATOR_DEV_NAME,
304 .owner = THIS_MODULE,
305#ifdef CONFIG_PM
306 .pm = &pm8xxx_vib_pm_ops,
307#endif
308 },
309};
310
311static int __init pm8xxx_vib_init(void)
312{
313 return platform_driver_register(&pm8xxx_vib_driver);
314}
315module_init(pm8xxx_vib_init);
316
317static void __exit pm8xxx_vib_exit(void)
318{
319 platform_driver_unregister(&pm8xxx_vib_driver);
320}
321module_exit(pm8xxx_vib_exit);
322
323MODULE_ALIAS("platform:" PM8XXX_VIBRATOR_DEV_NAME);
324MODULE_DESCRIPTION("pm8xxx vibrator driver");
325MODULE_LICENSE("GPL v2");