blob: d7fa87bb43417dba32e5844e606c333782da9e88 [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/*
2 * isa1200.c - Haptic Motor
3 *
4 * Copyright (C) 2009 Samsung Electronics
5 * Kyungmin Park <kyungmin.park@samsung.com>
Chun Zhangd02c50d2014-01-16 22:41:31 -08006 * Copyright (c) 2010-2014, The Linux Foundation. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12
13#include <linux/module.h>
14#include <linux/init.h>
15#include <linux/i2c.h>
16#include <linux/gpio.h>
17#include <linux/delay.h>
18#include <linux/err.h>
19#include <linux/pwm.h>
20#include <linux/workqueue.h>
21#include <linux/slab.h>
Mohan Pallaka73787fa2011-09-09 15:14:20 +053022#include <linux/regulator/consumer.h>
Mohan Pallaka32f20a72012-06-14 14:41:11 +053023#include <linux/clk.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070024#include <linux/i2c/isa1200.h>
25#include "../staging/android/timed_output.h"
Amy Malochee3f29012012-12-07 18:40:04 -080026#include <linux/of_gpio.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070027
28#define ISA1200_HCTRL0 0x30
29#define ISA1200_HCTRL1 0x31
30#define ISA1200_HCTRL5 0x35
31
32#define ISA1200_HCTRL0_RESET 0x01
33#define ISA1200_HCTRL1_RESET 0x4B
34
35#define ISA1200_HCTRL5_VIB_STRT 0xD5
36#define ISA1200_HCTRL5_VIB_STOP 0x6B
Mohan Pallakaace9fa92012-05-11 13:05:40 +053037#define ISA1200_POWER_DOWN_MASK 0x7F
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070038
39struct isa1200_chip {
40 struct i2c_client *client;
41 struct isa1200_platform_data *pdata;
42 struct pwm_device *pwm;
43 struct hrtimer timer;
44 struct timed_output_dev dev;
45 struct work_struct work;
Mohan Pallaka65206ee2012-12-05 16:50:48 +053046 struct mutex lock;
Shantanu Jainba43b442013-09-17 17:03:51 +053047 struct mutex lock_clk;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070048 unsigned int enable;
49 unsigned int period_ns;
Mohan Pallaka73787fa2011-09-09 15:14:20 +053050 bool is_len_gpio_valid;
51 struct regulator **regs;
Mohan Pallakaace9fa92012-05-11 13:05:40 +053052 bool clk_on;
53 u8 hctrl0_val;
Mohan Pallaka32f20a72012-06-14 14:41:11 +053054 struct clk *pwm_clk;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070055};
56
57static int isa1200_read_reg(struct i2c_client *client, int reg)
58{
59 int ret;
60
61 ret = i2c_smbus_read_byte_data(client, reg);
62 if (ret < 0)
63 dev_err(&client->dev, "%s: err %d\n", __func__, ret);
64
65 return ret;
66}
67
68static int isa1200_write_reg(struct i2c_client *client, int reg, u8 value)
69{
70 int ret;
71
72 ret = i2c_smbus_write_byte_data(client, reg, value);
73 if (ret < 0)
74 dev_err(&client->dev, "%s: err %d\n", __func__, ret);
75
76 return ret;
77}
78
79static void isa1200_vib_set(struct isa1200_chip *haptic, int enable)
80{
81 int rc = 0;
82
83 if (enable) {
Mohan Pallakaace9fa92012-05-11 13:05:40 +053084 /* if hen and len are seperate then enable hen
85 * otherwise set normal mode bit */
86 if (haptic->is_len_gpio_valid == true)
87 gpio_set_value_cansleep(haptic->pdata->hap_en_gpio, 1);
88 else {
89 rc = isa1200_write_reg(haptic->client, ISA1200_HCTRL0,
90 haptic->hctrl0_val | ~ISA1200_POWER_DOWN_MASK);
91 if (rc < 0) {
92 pr_err("%s: i2c write failure\n", __func__);
93 return;
94 }
95 }
96
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070097 if (haptic->pdata->mode_ctrl == PWM_INPUT_MODE) {
98 int period_us = haptic->period_ns / 1000;
Mohan Pallakaace9fa92012-05-11 13:05:40 +053099
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700100 rc = pwm_config(haptic->pwm,
101 (period_us * haptic->pdata->duty) / 100,
102 period_us);
Mohan Pallakaace9fa92012-05-11 13:05:40 +0530103 if (rc < 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700104 pr_err("%s: pwm_config fail\n", __func__);
Mohan Pallakaace9fa92012-05-11 13:05:40 +0530105 goto chip_dwn;
106 }
107
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700108 rc = pwm_enable(haptic->pwm);
Mohan Pallakaace9fa92012-05-11 13:05:40 +0530109 if (rc < 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700110 pr_err("%s: pwm_enable fail\n", __func__);
Mohan Pallakaace9fa92012-05-11 13:05:40 +0530111 goto chip_dwn;
112 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700113 } else if (haptic->pdata->mode_ctrl == PWM_GEN_MODE) {
Mohan Pallaka32f20a72012-06-14 14:41:11 +0530114 /* check for board specific clk callback */
115 if (haptic->pdata->clk_enable) {
Mohan Pallakaace9fa92012-05-11 13:05:40 +0530116 rc = haptic->pdata->clk_enable(true);
117 if (rc < 0) {
Mohan Pallaka32f20a72012-06-14 14:41:11 +0530118 pr_err("%s: clk enable cb failed\n",
119 __func__);
120 goto chip_dwn;
121 }
122 }
123
Shantanu Jainba43b442013-09-17 17:03:51 +0530124 mutex_lock(&haptic->lock_clk);
Mohan Pallaka32f20a72012-06-14 14:41:11 +0530125 /* vote for clock */
126 if (haptic->pdata->need_pwm_clk && !haptic->clk_on) {
Amy Malochee3f29012012-12-07 18:40:04 -0800127 rc = clk_prepare_enable(haptic->pwm_clk);
Mohan Pallaka32f20a72012-06-14 14:41:11 +0530128 if (rc < 0) {
Mohan Pallakaace9fa92012-05-11 13:05:40 +0530129 pr_err("%s: clk enable failed\n",
130 __func__);
Shantanu Jainba43b442013-09-17 17:03:51 +0530131 mutex_unlock(&haptic->lock_clk);
Mohan Pallaka32f20a72012-06-14 14:41:11 +0530132 goto dis_clk_cb;
Mohan Pallakaace9fa92012-05-11 13:05:40 +0530133 }
134 haptic->clk_on = true;
135 }
Shantanu Jainba43b442013-09-17 17:03:51 +0530136 mutex_unlock(&haptic->lock_clk);
Mohan Pallakaace9fa92012-05-11 13:05:40 +0530137
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700138 rc = isa1200_write_reg(haptic->client,
139 ISA1200_HCTRL5,
140 ISA1200_HCTRL5_VIB_STRT);
Mohan Pallakaace9fa92012-05-11 13:05:40 +0530141 if (rc < 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700142 pr_err("%s: start vibartion fail\n", __func__);
Mohan Pallakaace9fa92012-05-11 13:05:40 +0530143 goto dis_clk;
144 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700145 }
146 } else {
Mohan Pallakaace9fa92012-05-11 13:05:40 +0530147 /* if hen and len are seperate then pull down hen
148 * otherwise set power down bit */
149 if (haptic->is_len_gpio_valid == true)
150 gpio_set_value_cansleep(haptic->pdata->hap_en_gpio, 0);
151 else {
152 rc = isa1200_write_reg(haptic->client, ISA1200_HCTRL0,
153 haptic->hctrl0_val & ISA1200_POWER_DOWN_MASK);
154 if (rc < 0) {
155 pr_err("%s: i2c write failure\n", __func__);
156 return;
157 }
158 }
159
160 if (haptic->pdata->mode_ctrl == PWM_INPUT_MODE) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700161 pwm_disable(haptic->pwm);
Mohan Pallakaace9fa92012-05-11 13:05:40 +0530162 } else if (haptic->pdata->mode_ctrl == PWM_GEN_MODE) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700163 rc = isa1200_write_reg(haptic->client,
164 ISA1200_HCTRL5,
165 ISA1200_HCTRL5_VIB_STOP);
166 if (rc < 0)
167 pr_err("%s: stop vibartion fail\n", __func__);
Mohan Pallakaace9fa92012-05-11 13:05:40 +0530168
Shantanu Jainba43b442013-09-17 17:03:51 +0530169 mutex_lock(&haptic->lock_clk);
Mohan Pallakaace9fa92012-05-11 13:05:40 +0530170 /* de-vote clock */
Mohan Pallaka32f20a72012-06-14 14:41:11 +0530171 if (haptic->pdata->need_pwm_clk && haptic->clk_on) {
Amy Maloche62158232012-12-07 18:40:04 -0800172 clk_disable_unprepare(haptic->pwm_clk);
Mohan Pallakaace9fa92012-05-11 13:05:40 +0530173 haptic->clk_on = false;
174 }
Shantanu Jainba43b442013-09-17 17:03:51 +0530175 mutex_unlock(&haptic->lock_clk);
Mohan Pallaka32f20a72012-06-14 14:41:11 +0530176 /* check for board specific clk callback */
177 if (haptic->pdata->clk_enable) {
178 rc = haptic->pdata->clk_enable(false);
179 if (rc < 0)
180 pr_err("%s: clk disable cb failed\n",
181 __func__);
182 }
Mohan Pallakaace9fa92012-05-11 13:05:40 +0530183 }
184 }
185
186 return;
187
188dis_clk:
Shantanu Jainba43b442013-09-17 17:03:51 +0530189 mutex_lock(&haptic->lock_clk);
Mohan Pallaka32f20a72012-06-14 14:41:11 +0530190 if (haptic->pdata->need_pwm_clk && haptic->clk_on) {
Amy Maloche62158232012-12-07 18:40:04 -0800191 clk_disable_unprepare(haptic->pwm_clk);
Mohan Pallakaace9fa92012-05-11 13:05:40 +0530192 haptic->clk_on = false;
193 }
Shantanu Jainba43b442013-09-17 17:03:51 +0530194 mutex_unlock(&haptic->lock_clk);
Mohan Pallaka32f20a72012-06-14 14:41:11 +0530195
196dis_clk_cb:
197 if (haptic->pdata->clk_enable) {
198 rc = haptic->pdata->clk_enable(false);
199 if (rc < 0)
200 pr_err("%s: clk disable cb failed\n", __func__);
201 }
202
Mohan Pallakaace9fa92012-05-11 13:05:40 +0530203chip_dwn:
204 if (haptic->is_len_gpio_valid == true)
205 gpio_set_value_cansleep(haptic->pdata->hap_en_gpio, 0);
206 else {
207 rc = isa1200_write_reg(haptic->client, ISA1200_HCTRL0,
208 haptic->hctrl0_val & ISA1200_POWER_DOWN_MASK);
209 if (rc < 0) {
210 pr_err("%s: i2c write failure\n", __func__);
211 return;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700212 }
213 }
214}
215
216static void isa1200_chip_work(struct work_struct *work)
217{
218 struct isa1200_chip *haptic;
219
220 haptic = container_of(work, struct isa1200_chip, work);
221 isa1200_vib_set(haptic, haptic->enable);
222}
223
224static void isa1200_chip_enable(struct timed_output_dev *dev, int value)
225{
226 struct isa1200_chip *haptic = container_of(dev, struct isa1200_chip,
227 dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700228
Mohan Pallaka65206ee2012-12-05 16:50:48 +0530229 mutex_lock(&haptic->lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700230 hrtimer_cancel(&haptic->timer);
231 if (value == 0)
232 haptic->enable = 0;
233 else {
234 value = (value > haptic->pdata->max_timeout ?
235 haptic->pdata->max_timeout : value);
236 haptic->enable = 1;
237 hrtimer_start(&haptic->timer,
238 ktime_set(value / 1000, (value % 1000) * 1000000),
239 HRTIMER_MODE_REL);
240 }
Mohan Pallaka65206ee2012-12-05 16:50:48 +0530241 mutex_unlock(&haptic->lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700242 schedule_work(&haptic->work);
243}
244
245static int isa1200_chip_get_time(struct timed_output_dev *dev)
246{
247 struct isa1200_chip *haptic = container_of(dev, struct isa1200_chip,
248 dev);
249
250 if (hrtimer_active(&haptic->timer)) {
251 ktime_t r = hrtimer_get_remaining(&haptic->timer);
252 struct timeval t = ktime_to_timeval(r);
253 return t.tv_sec * 1000 + t.tv_usec / 1000;
254 } else
255 return 0;
256}
257
258static enum hrtimer_restart isa1200_vib_timer_func(struct hrtimer *timer)
259{
260 struct isa1200_chip *haptic = container_of(timer, struct isa1200_chip,
261 timer);
262 haptic->enable = 0;
263 schedule_work(&haptic->work);
264
265 return HRTIMER_NORESTART;
266}
267
268static void dump_isa1200_reg(char *str, struct i2c_client *client)
269{
270 pr_debug("%s reg0x%x=0x%x, reg0x%x=0x%x, reg0x%x=0x%x\n", str,
271 ISA1200_HCTRL0, isa1200_read_reg(client, ISA1200_HCTRL0),
272 ISA1200_HCTRL1, isa1200_read_reg(client, ISA1200_HCTRL1),
273 ISA1200_HCTRL5, isa1200_read_reg(client, ISA1200_HCTRL5));
274}
275
276static int isa1200_setup(struct i2c_client *client)
277{
278 struct isa1200_chip *haptic = i2c_get_clientdata(client);
Mohan Pallakaace9fa92012-05-11 13:05:40 +0530279 int temp, rc;
280 u8 value;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700281
282 gpio_set_value_cansleep(haptic->pdata->hap_en_gpio, 0);
Mohan Pallaka73787fa2011-09-09 15:14:20 +0530283 if (haptic->is_len_gpio_valid == true)
284 gpio_set_value_cansleep(haptic->pdata->hap_len_gpio, 0);
285
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700286 udelay(250);
Mohan Pallaka73787fa2011-09-09 15:14:20 +0530287
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700288 gpio_set_value_cansleep(haptic->pdata->hap_en_gpio, 1);
Mohan Pallaka73787fa2011-09-09 15:14:20 +0530289 if (haptic->is_len_gpio_valid == true)
290 gpio_set_value_cansleep(haptic->pdata->hap_len_gpio, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700291
292 value = (haptic->pdata->smart_en << 3) |
293 (haptic->pdata->is_erm << 5) |
294 (haptic->pdata->ext_clk_en << 7);
295
296 rc = isa1200_write_reg(client, ISA1200_HCTRL1, value);
297 if (rc < 0) {
298 pr_err("%s: i2c write failure\n", __func__);
Mohan Pallakaacdca0f2011-11-21 13:08:00 +0530299 goto reset_gpios;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700300 }
301
302 if (haptic->pdata->mode_ctrl == PWM_GEN_MODE) {
303 temp = haptic->pdata->pwm_fd.pwm_div;
304 if (temp < 128 || temp > 1024 || temp % 128) {
305 pr_err("%s: Invalid divider\n", __func__);
306 goto reset_hctrl1;
307 }
308 value = ((temp >> 7) - 1);
309 } else if (haptic->pdata->mode_ctrl == PWM_INPUT_MODE) {
310 temp = haptic->pdata->pwm_fd.pwm_freq;
311 if (temp < 22400 || temp > 172600 || temp % 22400) {
312 pr_err("%s: Invalid frequency\n", __func__);
313 goto reset_hctrl1;
314 }
315 value = ((temp / 22400) - 1);
316 haptic->period_ns = NSEC_PER_SEC / temp;
317 }
318
319 value |= (haptic->pdata->mode_ctrl << 3) |
320 (haptic->pdata->overdrive_high << 5) |
Chun Zhangd02c50d2014-01-16 22:41:31 -0800321 (haptic->pdata->overdrive_en << 6) |
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700322 (haptic->pdata->chip_en << 7);
323
324 rc = isa1200_write_reg(client, ISA1200_HCTRL0, value);
325 if (rc < 0) {
326 pr_err("%s: i2c write failure\n", __func__);
327 goto reset_hctrl1;
328 }
329
Mohan Pallakaace9fa92012-05-11 13:05:40 +0530330 /* if hen and len are seperate then pull down hen
331 * otherwise set power down bit */
332 if (haptic->is_len_gpio_valid == true)
333 gpio_set_value_cansleep(haptic->pdata->hap_en_gpio, 0);
334 else {
335 rc = isa1200_write_reg(client, ISA1200_HCTRL0,
336 value & ISA1200_POWER_DOWN_MASK);
337 if (rc < 0) {
338 pr_err("%s: i2c write failure\n", __func__);
339 goto reset_hctrl1;
340 }
341 }
342
343 haptic->hctrl0_val = value;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700344 dump_isa1200_reg("new:", client);
345 return 0;
346
347reset_hctrl1:
348 i2c_smbus_write_byte_data(client, ISA1200_HCTRL1,
349 ISA1200_HCTRL1_RESET);
Mohan Pallakaacdca0f2011-11-21 13:08:00 +0530350reset_gpios:
351 gpio_set_value_cansleep(haptic->pdata->hap_en_gpio, 0);
352 if (haptic->is_len_gpio_valid == true)
353 gpio_set_value_cansleep(haptic->pdata->hap_len_gpio, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700354 return rc;
355}
356
Mohan Pallaka73787fa2011-09-09 15:14:20 +0530357static int isa1200_reg_power(struct isa1200_chip *haptic, bool on)
358{
359 const struct isa1200_regulator *reg_info =
360 haptic->pdata->regulator_info;
361 u8 i, num_reg = haptic->pdata->num_regulators;
362 int rc;
363
364 for (i = 0; i < num_reg; i++) {
365 rc = regulator_set_optimum_mode(haptic->regs[i],
366 on ? reg_info[i].load_uA : 0);
367 if (rc < 0) {
368 pr_err("%s: regulator_set_optimum_mode failed(%d)\n",
369 __func__, rc);
370 goto regs_fail;
371 }
372
373 rc = on ? regulator_enable(haptic->regs[i]) :
374 regulator_disable(haptic->regs[i]);
375 if (rc < 0) {
376 pr_err("%s: regulator %sable fail %d\n", __func__,
377 on ? "en" : "dis", rc);
378 regulator_set_optimum_mode(haptic->regs[i],
379 !on ? reg_info[i].load_uA : 0);
380 goto regs_fail;
381 }
382 }
383
384 return 0;
385
386regs_fail:
387 while (i--) {
388 regulator_set_optimum_mode(haptic->regs[i],
389 !on ? reg_info[i].load_uA : 0);
390 !on ? regulator_enable(haptic->regs[i]) :
391 regulator_disable(haptic->regs[i]);
392 }
393 return rc;
394}
395
396static int isa1200_reg_setup(struct isa1200_chip *haptic, bool on)
397{
398 const struct isa1200_regulator *reg_info =
399 haptic->pdata->regulator_info;
400 u8 i, num_reg = haptic->pdata->num_regulators;
401 int rc = 0;
402
403 /* put regulators */
404 if (on == false) {
405 i = num_reg;
406 goto put_regs;
407 }
408
409 haptic->regs = kzalloc(num_reg * sizeof(struct regulator *),
410 GFP_KERNEL);
411 if (!haptic->regs) {
412 pr_err("unable to allocate memory\n");
413 return -ENOMEM;
414 }
415
416 for (i = 0; i < num_reg; i++) {
417 haptic->regs[i] = regulator_get(&haptic->client->dev,
Amy Malochee3f29012012-12-07 18:40:04 -0800418 reg_info[i].name);
Mohan Pallaka73787fa2011-09-09 15:14:20 +0530419 if (IS_ERR(haptic->regs[i])) {
420 rc = PTR_ERR(haptic->regs[i]);
Amy Malochee3f29012012-12-07 18:40:04 -0800421 pr_err("%s:regulator get failed(%d)\n", __func__, rc);
Mohan Pallaka73787fa2011-09-09 15:14:20 +0530422 goto put_regs;
423 }
424
425 if (regulator_count_voltages(haptic->regs[i]) > 0) {
426 rc = regulator_set_voltage(haptic->regs[i],
427 reg_info[i].min_uV, reg_info[i].max_uV);
428 if (rc) {
429 pr_err("%s: regulator_set_voltage failed(%d)\n",
430 __func__, rc);
431 regulator_put(haptic->regs[i]);
432 goto put_regs;
433 }
434 }
435 }
436
437 return rc;
438
439put_regs:
440 while (i--) {
441 if (regulator_count_voltages(haptic->regs[i]) > 0)
442 regulator_set_voltage(haptic->regs[i], 0,
443 reg_info[i].max_uV);
444 regulator_put(haptic->regs[i]);
445 }
446 kfree(haptic->regs);
447 return rc;
448}
449
Amy Malochee3f29012012-12-07 18:40:04 -0800450#ifdef CONFIG_OF
451static int isa1200_parse_dt(struct device *dev,
452 struct isa1200_platform_data *pdata)
453{
454 struct device_node *temp, *np = dev->of_node;
455 struct isa1200_regulator *reg_info;
456 enum of_gpio_flags hap_en_flags = OF_GPIO_ACTIVE_LOW;
457 enum of_gpio_flags hap_len_flags = OF_GPIO_ACTIVE_LOW;
458 int rc = 0;
459 u32 temp_val;
460 const char *temp_string;
461
462 rc = of_property_read_string(np, "label", &pdata->name);
463 if (rc) {
464 dev_err(dev, "Unable to read device name\n");
465 return rc;
466 }
467
468 pdata->chip_en = of_property_read_bool(np, "imagis,chip-en");
469 pdata->ext_clk_en = of_property_read_bool(np, "imagis,ext-clk-en");
470 pdata->is_erm = of_property_read_bool(np, "imagis,is-erm");
471 pdata->overdrive_high =
472 of_property_read_bool(np, "imagis,overdrive-high");
473 pdata->overdrive_en = of_property_read_bool(np, "imagis,overdrive-en");
474 pdata->smart_en = of_property_read_bool(np, "imagis,smart-en");
475 pdata->need_pwm_clk = of_property_read_bool(np, "imagis,need-pwm-clk");
476
477 pdata->hap_en_gpio = of_get_named_gpio_flags(np,
478 "imagis,hap-en-gpio", 0, &hap_en_flags);
479 pdata->hap_len_gpio = of_get_named_gpio_flags(np,
480 "imagis,hap-len-gpio", 0, &hap_len_flags);
481
482 rc = of_property_read_u32(np, "imagis,max-timeout",
483 &pdata->max_timeout);
484 if (rc) {
485 dev_err(dev, "Unable to read max timeout\n");
486 return rc;
487 }
488
489 rc = of_property_read_u32(np, "imagis,pwm-div", &pdata->pwm_fd.pwm_div);
490 if (rc && (rc != -EINVAL)) {
491 dev_err(dev, "Unable to read pwm division\n");
492 return rc;
493 }
494
495 rc = of_property_read_u32(np, "imagis,pwm-freq",
496 &pdata->pwm_fd.pwm_freq);
497 if (rc && (rc != -EINVAL)) {
498 dev_err(dev, "Unable to read pwm frequency\n");
499 return rc;
500 }
501
502 rc = of_property_read_u32(np, "imagis,pwm-ch-id", &pdata->pwm_ch_id);
503 if (rc && (rc != -EINVAL)) {
504 dev_err(dev, "Unable to read pwm channel id\n");
505 return rc;
506 }
507
508 rc = of_property_read_u32(np, "imagis,mode-ctrl", &pdata->mode_ctrl);
509 if (rc) {
510 dev_err(dev, "Unable to read control mode\n");
511 return rc;
512 }
513
514 rc = of_property_read_u32(np, "imagis,duty", &pdata->duty);
515 if (rc && (rc != -EINVAL)) {
516 dev_err(dev, "Unable to read duty cycle\n");
517 return rc;
518 }
519
520 pdata->num_regulators = 0;
521 temp = NULL;
522 while ((temp = of_get_next_child(np, temp)))
523 pdata->num_regulators++;
524
525 if (!pdata->num_regulators)
526 return 0;
527
528 reg_info = devm_kzalloc(dev, pdata->num_regulators *
529 sizeof(struct isa1200_regulator), GFP_KERNEL);
530 if (!reg_info)
531 return -ENOMEM;
532
533 pdata->regulator_info = reg_info;
534
535 for_each_child_of_node(np, temp) {
536 rc = of_property_read_string(temp,
537 "regulator-name", &temp_string);
538 if (rc) {
539 dev_err(dev, "Unable to read regulator name\n");
540 return rc;
541 } else
542 reg_info->name = temp_string;
543
544 rc = of_property_read_u32(temp, "regulator-max-microvolt",
545 &temp_val);
546 if (rc) {
547 dev_err(dev, "Unable to read max uV\n");
548 return rc;
549 } else
550 reg_info->max_uV = temp_val;
551
552 rc = of_property_read_u32(temp, "regulator-min-microvolt",
553 &temp_val);
554 if (rc) {
555 dev_err(dev, "Unable to read min uV\n");
556 return rc;
557 } else
558 reg_info->min_uV = temp_val;
559
560 rc = of_property_read_u32(temp, "regulator-max-microamp",
561 &temp_val);
562 if (rc) {
563 dev_err(dev, "Unable to read load uA\n");
564 return rc;
565 } else
566 reg_info->load_uA = temp_val;
567
568 reg_info++;
569 }
570
571 return 0;
572}
573#else
574static int isa1200_parse_dt(struct device *dev,
575 struct isa1200_platform_data *pdata)
576{
577 return -ENODEV;
578}
579#endif
580
581
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700582static int __devinit isa1200_probe(struct i2c_client *client,
583 const struct i2c_device_id *id)
584{
585 struct isa1200_chip *haptic;
586 struct isa1200_platform_data *pdata;
587 int ret;
588
589 if (!i2c_check_functionality(client->adapter,
590 I2C_FUNC_SMBUS_BYTE_DATA)) {
591 dev_err(&client->dev, "%s: no support for i2c read/write"
592 "byte data\n", __func__);
593 return -EIO;
594 }
595
Amy Malochee3f29012012-12-07 18:40:04 -0800596 if (client->dev.of_node) {
597 pdata = devm_kzalloc(&client->dev,
598 sizeof(struct isa1200_platform_data), GFP_KERNEL);
599 if (!pdata) {
600 dev_err(&client->dev, "Failed to allocate memory\n");
601 return -ENOMEM;
602 }
603
604 ret = isa1200_parse_dt(&client->dev, pdata);
605 if (ret) {
606 dev_err(&client->dev, "Parsing DT failed(%d)", ret);
607 return ret;
608 }
609 } else
610 pdata = client->dev.platform_data;
611
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700612 if (!pdata) {
613 dev_err(&client->dev, "%s: no platform data\n", __func__);
614 return -EINVAL;
615 }
616
617 if (pdata->dev_setup) {
618 ret = pdata->dev_setup(true);
619 if (ret < 0) {
620 dev_err(&client->dev, "dev setup failed\n");
621 return -EINVAL;
622 }
623 }
624
625 haptic = kzalloc(sizeof(struct isa1200_chip), GFP_KERNEL);
626 if (!haptic) {
627 ret = -ENOMEM;
628 goto mem_alloc_fail;
629 }
630 haptic->client = client;
631 haptic->enable = 0;
632 haptic->pdata = pdata;
633
Mohan Pallaka73787fa2011-09-09 15:14:20 +0530634 if (pdata->regulator_info) {
635 ret = isa1200_reg_setup(haptic, true);
636 if (ret) {
637 dev_err(&client->dev, "%s: regulator setup failed\n",
638 __func__);
639 goto reg_setup_fail;
640 }
641
642 ret = isa1200_reg_power(haptic, true);
643 if (ret) {
644 dev_err(&client->dev, "%s: regulator power failed\n",
645 __func__);
646 goto reg_pwr_fail;
647 }
648 }
649
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700650 if (pdata->power_on) {
651 ret = pdata->power_on(1);
652 if (ret) {
653 dev_err(&client->dev, "%s: power-up failed\n",
654 __func__);
655 goto pwr_up_fail;
656 }
657 }
658
Mohan Pallaka65206ee2012-12-05 16:50:48 +0530659 mutex_init(&haptic->lock);
Shantanu Jainba43b442013-09-17 17:03:51 +0530660 mutex_init(&haptic->lock_clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700661 INIT_WORK(&haptic->work, isa1200_chip_work);
Mohan Pallakaace9fa92012-05-11 13:05:40 +0530662 haptic->clk_on = false;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700663
664 hrtimer_init(&haptic->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
665 haptic->timer.function = isa1200_vib_timer_func;
666
667 /*register with timed output class*/
668 haptic->dev.name = pdata->name;
669 haptic->dev.get_time = isa1200_chip_get_time;
670 haptic->dev.enable = isa1200_chip_enable;
671 ret = timed_output_dev_register(&haptic->dev);
672 if (ret < 0)
673 goto timed_reg_fail;
674
675 i2c_set_clientdata(client, haptic);
676
677 ret = gpio_is_valid(pdata->hap_en_gpio);
678 if (ret) {
Mohan Pallaka73787fa2011-09-09 15:14:20 +0530679 ret = gpio_request(pdata->hap_en_gpio, "haptic_en_gpio");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700680 if (ret) {
681 dev_err(&client->dev, "%s: gpio %d request failed\n",
682 __func__, pdata->hap_en_gpio);
Mohan Pallaka73787fa2011-09-09 15:14:20 +0530683 goto hen_gpio_fail;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700684 }
685 } else {
686 dev_err(&client->dev, "%s: Invalid gpio %d\n", __func__,
687 pdata->hap_en_gpio);
Mohan Pallaka73787fa2011-09-09 15:14:20 +0530688 goto hen_gpio_fail;
689 }
690
691 haptic->is_len_gpio_valid = true;
692 ret = gpio_is_valid(haptic->pdata->hap_len_gpio);
693 if (ret) {
694 ret = gpio_request(pdata->hap_len_gpio,
695 "haptic_ldo_gpio");
696 if (ret) {
697 dev_err(&client->dev,
698 "%s: gpio %d request failed\n",
699 __func__, pdata->hap_len_gpio);
700 goto len_gpio_fail;
701 }
702 } else {
703 dev_err(&client->dev, "%s: gpio is not used/Invalid %d\n",
704 __func__, pdata->hap_len_gpio);
705 haptic->is_len_gpio_valid = false;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700706 }
707
708 ret = isa1200_setup(client);
709 if (ret) {
710 dev_err(&client->dev, "%s: setup fail %d\n", __func__, ret);
Mohan Pallaka73787fa2011-09-09 15:14:20 +0530711 goto setup_fail;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700712 }
713
714 if (haptic->pdata->mode_ctrl == PWM_INPUT_MODE) {
715 haptic->pwm = pwm_request(pdata->pwm_ch_id, id->name);
716 if (IS_ERR(haptic->pwm)) {
717 dev_err(&client->dev, "%s: pwm request failed\n",
718 __func__);
719 ret = PTR_ERR(haptic->pwm);
720 goto reset_hctrl0;
721 }
Mohan Pallaka32f20a72012-06-14 14:41:11 +0530722 } else if (haptic->pdata->need_pwm_clk) {
723 haptic->pwm_clk = clk_get(&client->dev, "pwm_clk");
724 if (IS_ERR(haptic->pwm_clk)) {
725 dev_err(&client->dev, "pwm_clk get failed\n");
726 ret = PTR_ERR(haptic->pwm_clk);
727 goto reset_hctrl0;
728 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700729 }
730
731 printk(KERN_INFO "%s: %s registered\n", __func__, id->name);
732 return 0;
733
734reset_hctrl0:
Mohan Pallakaacdca0f2011-11-21 13:08:00 +0530735 gpio_set_value_cansleep(haptic->pdata->hap_en_gpio, 0);
736 if (haptic->is_len_gpio_valid == true)
737 gpio_set_value_cansleep(haptic->pdata->hap_len_gpio, 0);
738 i2c_smbus_write_byte_data(client, ISA1200_HCTRL1,
739 ISA1200_HCTRL1_RESET);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700740 i2c_smbus_write_byte_data(client, ISA1200_HCTRL0,
741 ISA1200_HCTRL0_RESET);
Mohan Pallaka73787fa2011-09-09 15:14:20 +0530742setup_fail:
743 if (haptic->is_len_gpio_valid == true)
744 gpio_free(pdata->hap_len_gpio);
745len_gpio_fail:
746 gpio_free(pdata->hap_en_gpio);
747hen_gpio_fail:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700748 timed_output_dev_unregister(&haptic->dev);
749timed_reg_fail:
Mohan Pallaka65206ee2012-12-05 16:50:48 +0530750 mutex_destroy(&haptic->lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700751 if (pdata->power_on)
752 pdata->power_on(0);
753pwr_up_fail:
Mohan Pallaka73787fa2011-09-09 15:14:20 +0530754 if (pdata->regulator_info)
755 isa1200_reg_power(haptic, false);
756reg_pwr_fail:
757 if (pdata->regulator_info)
758 isa1200_reg_setup(haptic, false);
759reg_setup_fail:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700760 kfree(haptic);
761mem_alloc_fail:
762 if (pdata->dev_setup)
763 pdata->dev_setup(false);
764 return ret;
765}
766
767static int __devexit isa1200_remove(struct i2c_client *client)
768{
769 struct isa1200_chip *haptic = i2c_get_clientdata(client);
770
771 hrtimer_cancel(&haptic->timer);
772 cancel_work_sync(&haptic->work);
773
774 /* turn-off current vibration */
775 isa1200_vib_set(haptic, 0);
776
777 if (haptic->pdata->mode_ctrl == PWM_INPUT_MODE)
778 pwm_free(haptic->pwm);
779
780 timed_output_dev_unregister(&haptic->dev);
Mohan Pallaka73787fa2011-09-09 15:14:20 +0530781
Mohan Pallakaacdca0f2011-11-21 13:08:00 +0530782 gpio_set_value_cansleep(haptic->pdata->hap_en_gpio, 0);
783 if (haptic->is_len_gpio_valid == true)
784 gpio_set_value_cansleep(haptic->pdata->hap_len_gpio, 0);
785
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700786 gpio_free(haptic->pdata->hap_en_gpio);
Mohan Pallaka73787fa2011-09-09 15:14:20 +0530787 if (haptic->is_len_gpio_valid == true)
788 gpio_free(haptic->pdata->hap_len_gpio);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700789
790 /* reset hardware registers */
791 i2c_smbus_write_byte_data(client, ISA1200_HCTRL0,
792 ISA1200_HCTRL0_RESET);
793 i2c_smbus_write_byte_data(client, ISA1200_HCTRL1,
794 ISA1200_HCTRL1_RESET);
795
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700796
Mohan Pallaka65206ee2012-12-05 16:50:48 +0530797 /* destroy mutex */
798 mutex_destroy(&haptic->lock);
Shantanu Jainba43b442013-09-17 17:03:51 +0530799 mutex_destroy(&haptic->lock_clk);
Mohan Pallaka65206ee2012-12-05 16:50:48 +0530800
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700801 /* power-off the chip */
Mohan Pallaka73787fa2011-09-09 15:14:20 +0530802 if (haptic->pdata->regulator_info) {
803 isa1200_reg_power(haptic, false);
804 isa1200_reg_setup(haptic, false);
805 }
806
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700807 if (haptic->pdata->power_on)
808 haptic->pdata->power_on(0);
809
Mohan Pallaka73787fa2011-09-09 15:14:20 +0530810 if (haptic->pdata->dev_setup)
811 haptic->pdata->dev_setup(false);
812
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700813 kfree(haptic);
814 return 0;
815}
816
817#ifdef CONFIG_PM
818static int isa1200_suspend(struct i2c_client *client, pm_message_t mesg)
819{
820 struct isa1200_chip *haptic = i2c_get_clientdata(client);
821 int ret;
822
823 hrtimer_cancel(&haptic->timer);
824 cancel_work_sync(&haptic->work);
825 /* turn-off current vibration */
826 isa1200_vib_set(haptic, 0);
827
Mohan Pallaka73787fa2011-09-09 15:14:20 +0530828 gpio_set_value_cansleep(haptic->pdata->hap_en_gpio, 0);
829 if (haptic->is_len_gpio_valid == true)
830 gpio_set_value_cansleep(haptic->pdata->hap_len_gpio, 0);
831
832 if (haptic->pdata->regulator_info)
833 isa1200_reg_power(haptic, false);
834
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700835 if (haptic->pdata->power_on) {
836 ret = haptic->pdata->power_on(0);
837 if (ret) {
838 dev_err(&client->dev, "power-down failed\n");
839 return ret;
840 }
841 }
842
843 return 0;
844}
845
846static int isa1200_resume(struct i2c_client *client)
847{
848 struct isa1200_chip *haptic = i2c_get_clientdata(client);
849 int ret;
850
Mohan Pallaka73787fa2011-09-09 15:14:20 +0530851 if (haptic->pdata->regulator_info)
852 isa1200_reg_power(haptic, true);
853
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700854 if (haptic->pdata->power_on) {
855 ret = haptic->pdata->power_on(1);
856 if (ret) {
857 dev_err(&client->dev, "power-up failed\n");
858 return ret;
859 }
860 }
861
862 isa1200_setup(client);
863 return 0;
864}
865#else
866#define isa1200_suspend NULL
867#define isa1200_resume NULL
868#endif
869
870static const struct i2c_device_id isa1200_id[] = {
871 { "isa1200_1", 0 },
872 { },
873};
874MODULE_DEVICE_TABLE(i2c, isa1200_id);
Amy Malochee3f29012012-12-07 18:40:04 -0800875#ifdef CONFIG_OF
876static struct of_device_id isa1200_match_table[] = {
877 { .compatible = "imagis,isa1200",},
878 { },
879};
880#else
881#define isa1200_match_table NULL
882#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700883
884static struct i2c_driver isa1200_driver = {
885 .driver = {
886 .name = "isa1200",
Amy Malochee3f29012012-12-07 18:40:04 -0800887 .of_match_table = isa1200_match_table,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700888 },
889 .probe = isa1200_probe,
890 .remove = __devexit_p(isa1200_remove),
891 .suspend = isa1200_suspend,
892 .resume = isa1200_resume,
893 .id_table = isa1200_id,
894};
895
896static int __init isa1200_init(void)
897{
898 return i2c_add_driver(&isa1200_driver);
899}
900
901static void __exit isa1200_exit(void)
902{
903 i2c_del_driver(&isa1200_driver);
904}
905
906module_init(isa1200_init);
907module_exit(isa1200_exit);
908
909MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>");
910MODULE_DESCRIPTION("ISA1200 Haptic Motor driver");
911MODULE_LICENSE("GPL");