blob: f4e2c354f5aabba6245f78a4efa856b55282a9eb [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/*
2 * Copyright (C) 2009 Samsung Electronics
3 * Kyungmin Park <kyungmin.park@samsung.com>
4 *
5 * Copyright (c) 2011, Code Aurora Forum. All rights reserved.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 and
9 * only version 2 as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 */
17
18#define pr_fmt(fmt) "%s: " fmt, __func__
19
20#include <linux/module.h>
21#include <linux/init.h>
22#include <linux/i2c.h>
23#include <linux/gpio.h>
24#include <linux/workqueue.h>
25#include <linux/delay.h>
26#include <linux/pwm.h>
27#include <linux/input.h>
28#include <linux/slab.h>
29#include <linux/pm.h>
30#include <linux/i2c/isa1200.h>
31
32#define ISA1200_HCTRL0 0x30
33#define HCTRL0_MODE_CTRL_BIT (3)
34#define HCTRL0_OVERDRIVE_HIGH_BIT (5)
35#define HCTRL0_OVERDRIVE_EN_BIT (6)
36#define HCTRL0_HAP_EN (7)
37#define HCTRL0_RESET 0x01
38#define HCTRL1_RESET 0x4B
39
40#define ISA1200_HCTRL1 0x31
41#define HCTRL1_SMART_ENABLE_BIT (3)
42#define HCTRL1_ERM_BIT (5)
43#define HCTRL1_EXT_CLK_ENABLE_BIT (7)
44
45#define ISA1200_HCTRL5 0x35
46#define HCTRL5_VIB_STRT 0xD5
47#define HCTRL5_VIB_STOP 0x6B
48
49#define DIVIDER_128 (128)
50#define DIVIDER_1024 (1024)
51#define DIVIDE_SHIFTER_128 (7)
52
53#define FREQ_22400 (22400)
54#define FREQ_172600 (172600)
55
56#define POR_DELAY_USEC 250
57
58struct isa1200_chip {
59 const struct isa1200_platform_data *pdata;
60 struct i2c_client *client;
61 struct input_dev *input_device;
62 struct pwm_device *pwm;
63 unsigned int period_ns;
64 unsigned int state;
65 struct work_struct work;
66};
67
68static void isa1200_vib_set(struct isa1200_chip *haptic, int enable)
69{
70 int rc;
71
72 if (enable) {
73 if (haptic->pdata->mode_ctrl == PWM_INPUT_MODE) {
74 int period_us = haptic->period_ns / NSEC_PER_USEC;
75 rc = pwm_config(haptic->pwm,
76 (period_us * haptic->pdata->duty) / 100,
77 period_us);
78 if (rc < 0)
79 pr_err("pwm_config fail\n");
80 rc = pwm_enable(haptic->pwm);
81 if (rc < 0)
82 pr_err("pwm_enable fail\n");
83 } else if (haptic->pdata->mode_ctrl == PWM_GEN_MODE) {
84 rc = i2c_smbus_write_byte_data(haptic->client,
85 ISA1200_HCTRL5,
86 HCTRL5_VIB_STRT);
87 if (rc < 0)
88 pr_err("start vibration fail\n");
89 }
90 } else {
91 if (haptic->pdata->mode_ctrl == PWM_INPUT_MODE)
92 pwm_disable(haptic->pwm);
93 else if (haptic->pdata->mode_ctrl == PWM_GEN_MODE) {
94 rc = i2c_smbus_write_byte_data(haptic->client,
95 ISA1200_HCTRL5,
96 HCTRL5_VIB_STOP);
97 if (rc < 0)
98 pr_err("stop vibration fail\n");
99 }
100 }
101}
102
103static int isa1200_setup(struct i2c_client *client)
104{
105 struct isa1200_chip *haptic = i2c_get_clientdata(client);
106 int value, temp, rc;
107
108 gpio_set_value_cansleep(haptic->pdata->hap_en_gpio, 0);
109 udelay(POR_DELAY_USEC);
110 gpio_set_value_cansleep(haptic->pdata->hap_en_gpio, 1);
111
112 value = (haptic->pdata->smart_en << HCTRL1_SMART_ENABLE_BIT) |
113 (haptic->pdata->is_erm << HCTRL1_ERM_BIT) |
114 (haptic->pdata->ext_clk_en << HCTRL1_EXT_CLK_ENABLE_BIT);
115
116 rc = i2c_smbus_write_byte_data(client, ISA1200_HCTRL1, value);
117 if (rc < 0) {
118 pr_err("i2c write failure\n");
119 return rc;
120 }
121
122 if (haptic->pdata->mode_ctrl == PWM_GEN_MODE) {
123 temp = haptic->pdata->pwm_fd.pwm_div;
124 if (temp < DIVIDER_128 || temp > DIVIDER_1024 ||
125 temp % DIVIDER_128) {
126 pr_err("Invalid divider\n");
127 rc = -EINVAL;
128 goto reset_hctrl1;
129 }
130 value = ((temp >> DIVIDE_SHIFTER_128) - 1);
131 } else if (haptic->pdata->mode_ctrl == PWM_INPUT_MODE) {
132 temp = haptic->pdata->pwm_fd.pwm_freq;
133 if (temp < FREQ_22400 || temp > FREQ_172600 ||
134 temp % FREQ_22400) {
135 pr_err("Invalid frequency\n");
136 rc = -EINVAL;
137 goto reset_hctrl1;
138 }
139 value = ((temp / FREQ_22400) - 1);
140 haptic->period_ns = NSEC_PER_SEC / temp;
141 }
142 value |= (haptic->pdata->mode_ctrl << HCTRL0_MODE_CTRL_BIT) |
143 (haptic->pdata->overdrive_high << HCTRL0_OVERDRIVE_HIGH_BIT) |
144 (haptic->pdata->overdrive_en << HCTRL0_OVERDRIVE_EN_BIT) |
145 (haptic->pdata->chip_en << HCTRL0_HAP_EN);
146
147 rc = i2c_smbus_write_byte_data(client, ISA1200_HCTRL0, value);
148 if (rc < 0) {
149 pr_err("i2c write failure\n");
150 goto reset_hctrl1;
151 }
152
153 return 0;
154
155reset_hctrl1:
156 i2c_smbus_write_byte_data(client, ISA1200_HCTRL1,
157 HCTRL1_RESET);
158 return rc;
159}
160
161static void isa1200_worker(struct work_struct *work)
162{
163 struct isa1200_chip *haptic;
164
165 haptic = container_of(work, struct isa1200_chip, work);
166 isa1200_vib_set(haptic, !!haptic->state);
167}
168
169static int isa1200_play_effect(struct input_dev *dev, void *data,
170 struct ff_effect *effect)
171{
172 struct isa1200_chip *haptic = input_get_drvdata(dev);
173
174 /* support basic vibration */
175 haptic->state = effect->u.rumble.strong_magnitude >> 8;
176 if (!haptic->state)
177 haptic->state = effect->u.rumble.weak_magnitude >> 9;
178
179 schedule_work(&haptic->work);
180
181 return 0;
182}
183
184#ifdef CONFIG_PM
185static int isa1200_suspend(struct device *dev)
186{
187 struct isa1200_chip *haptic = dev_get_drvdata(dev);
188 int rc;
189
190 cancel_work_sync(&haptic->work);
191 /* turn-off current vibration */
192 isa1200_vib_set(haptic, 0);
193
194 if (haptic->pdata->power_on) {
195 rc = haptic->pdata->power_on(0);
196 if (rc) {
197 pr_err("power-down failed\n");
198 return rc;
199 }
200 }
201
202 return 0;
203}
204
205static int isa1200_resume(struct device *dev)
206{
207 struct isa1200_chip *haptic = dev_get_drvdata(dev);
208 int rc;
209
210 if (haptic->pdata->power_on) {
211 rc = haptic->pdata->power_on(1);
212 if (rc) {
213 pr_err("power-up failed\n");
214 return rc;
215 }
216 }
217
218 isa1200_setup(haptic->client);
219 return 0;
220}
221#else
222#define isa1200_suspend NULL
223#define isa1200_resume NULL
224#endif
225
226static int isa1200_open(struct input_dev *dev)
227{
228 struct isa1200_chip *haptic = input_get_drvdata(dev);
229 int rc;
230
231 /* device setup */
232 if (haptic->pdata->dev_setup) {
233 rc = haptic->pdata->dev_setup(true);
234 if (rc < 0) {
235 pr_err("setup failed!\n");
236 return rc;
237 }
238 }
239
240 /* power on */
241 if (haptic->pdata->power_on) {
242 rc = haptic->pdata->power_on(true);
243 if (rc < 0) {
244 pr_err("power failed\n");
245 goto err_setup;
246 }
247 }
248
249 /* request gpio */
250 rc = gpio_is_valid(haptic->pdata->hap_en_gpio);
251 if (rc) {
252 rc = gpio_request(haptic->pdata->hap_en_gpio, "haptic_gpio");
253 if (rc) {
254 pr_err("gpio %d request failed\n",
255 haptic->pdata->hap_en_gpio);
256 goto err_power_on;
257 }
258 } else {
259 pr_err("Invalid gpio %d\n",
260 haptic->pdata->hap_en_gpio);
261 goto err_power_on;
262 }
263
264 rc = gpio_direction_output(haptic->pdata->hap_en_gpio, 0);
265 if (rc) {
266 pr_err("gpio %d set direction failed\n",
267 haptic->pdata->hap_en_gpio);
268 goto err_gpio_free;
269 }
270
271 /* setup registers */
272 rc = isa1200_setup(haptic->client);
273 if (rc < 0) {
274 pr_err("setup fail %d\n", rc);
275 goto err_gpio_free;
276 }
277
278 if (haptic->pdata->mode_ctrl == PWM_INPUT_MODE) {
279 haptic->pwm = pwm_request(haptic->pdata->pwm_ch_id,
280 haptic->client->driver->id_table->name);
281 if (IS_ERR(haptic->pwm)) {
282 pr_err("pwm request failed\n");
283 rc = PTR_ERR(haptic->pwm);
284 goto err_reset_hctrl0;
285 }
286 }
287
288 /* init workqeueue */
289 INIT_WORK(&haptic->work, isa1200_worker);
290 return 0;
291
292err_reset_hctrl0:
293 i2c_smbus_write_byte_data(haptic->client, ISA1200_HCTRL0,
294 HCTRL0_RESET);
295err_gpio_free:
296 gpio_free(haptic->pdata->hap_en_gpio);
297err_power_on:
298 if (haptic->pdata->power_on)
299 haptic->pdata->power_on(0);
300err_setup:
301 if (haptic->pdata->dev_setup)
302 haptic->pdata->dev_setup(false);
303
304 return rc;
305}
306
307static void isa1200_close(struct input_dev *dev)
308{
309 struct isa1200_chip *haptic = input_get_drvdata(dev);
310
311 /* turn-off current vibration */
312 isa1200_vib_set(haptic, 0);
313
314 if (haptic->pdata->mode_ctrl == PWM_INPUT_MODE)
315 pwm_free(haptic->pwm);
316
317 gpio_free(haptic->pdata->hap_en_gpio);
318
319 /* reset hardware registers */
320 i2c_smbus_write_byte_data(haptic->client, ISA1200_HCTRL0,
321 HCTRL0_RESET);
322 i2c_smbus_write_byte_data(haptic->client, ISA1200_HCTRL1,
323 HCTRL1_RESET);
324
325 if (haptic->pdata->dev_setup)
326 haptic->pdata->dev_setup(false);
327
328 /* power-off the chip */
329 if (haptic->pdata->power_on)
330 haptic->pdata->power_on(0);
331}
332
333static int __devinit isa1200_probe(struct i2c_client *client,
334 const struct i2c_device_id *id)
335{
336 struct isa1200_chip *haptic;
337 int rc;
338
339 if (!i2c_check_functionality(client->adapter,
340 I2C_FUNC_SMBUS_BYTE_DATA)) {
341 pr_err("i2c is not supported\n");
342 return -EIO;
343 }
344
345 if (!client->dev.platform_data) {
346 pr_err("pdata is not avaiable\n");
347 return -EINVAL;
348 }
349
350 haptic = kzalloc(sizeof(struct isa1200_chip), GFP_KERNEL);
351 if (!haptic) {
352 pr_err("no memory\n");
353 return -ENOMEM;
354 }
355
356 haptic->pdata = client->dev.platform_data;
357 haptic->client = client;
358
359 i2c_set_clientdata(client, haptic);
360
361 haptic->input_device = input_allocate_device();
362 if (!haptic->input_device) {
363 pr_err("input device alloc failed\n");
364 rc = -ENOMEM;
365 goto err_mem_alloc;
366 }
367
368 input_set_drvdata(haptic->input_device, haptic);
369 haptic->input_device->name = haptic->pdata->name ? :
370 "isa1200-ff-memless";
371
372 haptic->input_device->dev.parent = &client->dev;
373
374 input_set_capability(haptic->input_device, EV_FF, FF_RUMBLE);
375
376 haptic->input_device->open = isa1200_open;
377 haptic->input_device->close = isa1200_close;
378
379 rc = input_ff_create_memless(haptic->input_device, NULL,
380 isa1200_play_effect);
381 if (rc < 0) {
382 pr_err("unable to register with ff\n");
383 goto err_free_dev;
384 }
385
386 rc = input_register_device(haptic->input_device);
387 if (rc < 0) {
388 pr_err("unable to register input device\n");
389 goto err_ff_destroy;
390 }
391
392 return 0;
393
394err_ff_destroy:
395 input_ff_destroy(haptic->input_device);
396err_free_dev:
397 input_free_device(haptic->input_device);
398err_mem_alloc:
399 kfree(haptic);
400 return rc;
401}
402
403static int __devexit isa1200_remove(struct i2c_client *client)
404{
405 struct isa1200_chip *haptic = i2c_get_clientdata(client);
406
407 input_unregister_device(haptic->input_device);
408 kfree(haptic);
409
410 return 0;
411}
412
413static const struct i2c_device_id isa1200_id_table[] = {
414 {"isa1200_1", 0},
415 { },
416};
417MODULE_DEVICE_TABLE(i2c, isa1200_id_table);
418
419static const struct dev_pm_ops isa1200_pm_ops = {
420 .suspend = isa1200_suspend,
421 .resume = isa1200_resume,
422};
423
424static struct i2c_driver isa1200_driver = {
425 .driver = {
426 .name = "isa1200-ff-memless",
427 .owner = THIS_MODULE,
428 .pm = &isa1200_pm_ops,
429 },
430 .probe = isa1200_probe,
431 .remove = __devexit_p(isa1200_remove),
432 .id_table = isa1200_id_table,
433};
434
435static int __init isa1200_init(void)
436{
437 return i2c_add_driver(&isa1200_driver);
438}
439module_init(isa1200_init);
440
441static void __exit isa1200_exit(void)
442{
443 i2c_del_driver(&isa1200_driver);
444}
445module_exit(isa1200_exit);
446
447MODULE_DESCRIPTION("isa1200 based vibrator chip driver");
448MODULE_LICENSE("GPL v2");
449MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>");