aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/input/misc/isa1200.c
blob: deafa5ca2f7b5a341a96370dc4d87dc24fe6e21d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*
 * ISA1200 linear virbrator driver
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA  02110-1301, USA.
 */
#include <linux/delay.h>
#include <linux/errno.h>
#include <linux/i2c.h>
#include <linux/init.h>
#include <linux/hrtimer.h>
#include <linux/platform_device.h>
#include <linux/device.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/types.h>
#include <linux/gpio.h>
#include <linux/earlysuspend.h>
#include <linux/input/isa1200.h>
#include <linux/pwm.h>

#include <plat/gpio-cfg.h>
#include "../../staging/android/timed_output.h"

#define ISA1200_VERSION  "1.0.0"
#define ISA1200_NAME "isa1200"

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
/*
 * driver private data
 */
struct isa1200_chip {
	struct i2c_client *i2c;
	struct isa1200_platform_data *pdata;

	struct pwm_device *pwm;
	struct timed_output_dev timed_output;

	int period;
	int duty;
	int pwm_id;
};

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
enum
{
	HCTRL_0 = 0x30,
	HCTRL_1,
	HCTRL_2,
	HCTRL_3,
	HCTRL_4,
	HCTRL_5,
	HCTRL_6,
	HCTRL_7,
	HCTRL_8,
	HCTRL_9,
	HCTRL_A,
	HCTRL_B,
	HCTRL_C,
	HCTRL_D,
	HCTRL_E,
	HCTRL_F,
	HCTRL_MAX = 0x40
};

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
static int isa1200_hw_init(struct isa1200_chip *chip)
{
	struct i2c_client *client = chip->i2c;
	int ret =0;
	unsigned val=0;

	// gpio_hen enable
	ret = gpio_is_valid(chip->pdata->gpio_hen);
	if (ret) {
		ret = gpio_request(chip->pdata->gpio_hen, "gpio_hen");
		if (ret) {
			dev_err(&client->dev, "gpio %d request failed\n",
					chip->pdata->gpio_hen);
			return -EIO;
		}
		ret = gpio_direction_output(chip->pdata->gpio_hen, 1);
		if (ret) {
			dev_err(&client->dev, "gpio %d set direction failed\n",
						chip->pdata->gpio_hen);
			return -EIO;
		}
		gpio_free(chip->pdata->gpio_hen);
	}
	else return -EIO;

	mdelay(1);

	// gpio_len enable
	ret = gpio_is_valid(chip->pdata->gpio_len);
	if (ret) {
		ret = gpio_request(chip->pdata->gpio_len, "gpio_len");
		if (ret) {
			dev_err(&client->dev, "gpio %d request failed\n",
					chip->pdata->gpio_len);
			return -EIO;
		}
		ret = gpio_direction_output(chip->pdata->gpio_len, 1);
		if (ret) {
			dev_err(&client->dev, "gpio %d set direction failed\n",
						chip->pdata->gpio_len);
			return -EIO;
		}
		gpio_free(chip->pdata->gpio_len);
	}
	else return -EIO;
	
	//pwm port pin_func init
	if (gpio_is_valid(chip->pdata->pwm_gpio)) {
		ret = gpio_request(chip->pdata->pwm_gpio, "pwm_gpio");
		if (ret)
			printk(KERN_ERR "failed to get GPIO for PWM0\n");
		s3c_gpio_cfgpin(chip->pdata->pwm_gpio, chip->pdata->pwm_func);
		gpio_free(chip->pdata->pwm_gpio);
    }
    
    mdelay (1);
	i2c_smbus_write_byte_data(client, HCTRL_2, 0x80); //Software reset enable
	mdelay (1);
	i2c_smbus_write_byte_data(client, HCTRL_2, 0x00); //Software reset disable
	mdelay (1);
    i2c_smbus_write_byte_data(client, HCTRL_0, 0x88); // PWM_INPUT Mode

	val = i2c_smbus_read_byte_data(client, 0x00);
	printk("%s : read val = 0x%02x\n",__func__,val);
	
	return 0;
}

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
static void isa1200_vibrator_enable (struct timed_output_dev *dev, int value)
{
	struct isa1200_chip *chip = container_of(dev, struct isa1200_chip, timed_output);

	if(10<value) {

		if(5<value)	value -=5;
		pwm_disable(chip->pwm);
		pwm_config(chip->pwm, chip->duty * chip->period / 255, chip->period);
		pwm_enable(chip->pwm);
		msleep_interruptible(value);
		pwm_disable(chip->pwm);
	}
	return;
}

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
static int isa1200_vibrator_get_time (struct timed_output_dev *dev)
{
	return 0;
}

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
static struct timed_output_dev isa1200_vibrator = {
  .name = "vibrator",
  .get_time = isa1200_vibrator_get_time,
  .enable = isa1200_vibrator_enable,
};

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
static int isa1200_i2c_probe (struct i2c_client *client, const struct i2c_device_id *id)
{
	struct isa1200_chip *chip;
	struct device 	*dev = &client->dev;
	int err;
	
	/* setup i2c client */
	if (!i2c_check_functionality (client->adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
		dev_err(&client->dev, "i2c byte data not supported\n");
		return -EIO;
	}
	if (!client->dev.platform_data) {
		dev_err(&client->dev, "pdata is not available\n");
		return -EINVAL;
	}

	chip = kzalloc (sizeof (struct isa1200_chip), GFP_KERNEL);
	if (!chip)
		return -ENOMEM;

	chip->pdata = client->dev.platform_data;
	chip->i2c = client;
	i2c_set_clientdata (client, chip);

	chip->pwm = pwm_request(chip->pdata->pwm_id, id->name);
	chip->period = chip->pdata->pwm_periode_ns;
	chip->duty = chip->pdata->pwm_duty;

	pwm_disable(chip->pwm);

	isa1200_hw_init(chip);

	chip->timed_output = isa1200_vibrator;
	err = timed_output_dev_register (&chip->timed_output);
	if (err < 0)
		goto error;

	dev_set_drvdata(dev, chip);

	return 0;
	
error:
	kfree (chip);
	return -1;
}

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
static int isa1200_i2c_remove (struct i2c_client *client)
{
	struct isa1200_chip *isa1200 = i2c_get_clientdata (client);
	
	timed_output_dev_unregister (&isa1200->timed_output);
	i2c_set_clientdata (client, NULL);
	kfree (isa1200);
	
	return 0;
}

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
#ifdef CONFIG_PM
#ifdef CONFIG_HAS_EARLYSUSPEND
static void isa1200_early_suspend(struct early_suspend *h)
{
	printk("\t%s [%d]\n",__FUNCTION__,__LINE__);
	return;
}

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
static void isa1200_late_resume(struct early_suspend *h)
{
	printk("%s\n",__FUNCTION__);
	return;
}

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
static struct early_suspend isa1200_early_suspend_desc = {
     .level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN,
     .suspend = isa1200_early_suspend,
     .resume = isa1200_late_resume
};
#endif //CONFIG_HAS_EARLYSUSPEND
#endif //CONFIG_PM

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
static const struct i2c_device_id isa1200_id[] = {
  {ISA1200_NAME, 0},
  {},
};
MODULE_DEVICE_TABLE (i2c, isa1200_id);

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
struct i2c_driver isa1200_driver = {
	.driver = {
	     .name = "isa1200",
	     .owner = THIS_MODULE,
     },
	.probe =    isa1200_i2c_probe,
	.remove =   isa1200_i2c_remove,
	.id_table = isa1200_id,
};

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
static int __init isa1200_init (void)
{
	int ret;

	ret = i2c_add_driver (&isa1200_driver);
	if (ret) {
		printk(KERN_ERR "i2c add driver Failed %d\n", ret);
		return -1;
	}
#ifdef CONFIG_HAS_EARLYSUSPEND
	register_early_suspend(&isa1200_early_suspend_desc);
#endif
	return 0;
}
module_init (isa1200_init);

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
static void __exit isa1200_exit (void)
{
#ifdef CONFIG_HAS_EARLYSUSPEND
	unregister_early_suspend(&isa1200_early_suspend_desc);
#endif
	i2c_del_driver (&isa1200_driver);
}
module_exit (isa1200_exit);


MODULE_LICENSE ("GPL");
MODULE_DESCRIPTION ("ISA1200 linear virbrator driver");
MODULE_VERSION (ISA1200_VERSION);