upload tizen1.0 source
[kernel/linux-2.6.36.git] / drivers / input / touchscreen / wacom / wacom_i2c.c
1 /*
2  *  wacom_i2c.c - Wacom G5 Digitizer Controller (I2C bus)
3  *
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19
20 #include <linux/kernel.h>
21 #include <linux/input.h>
22 #include <linux/i2c.h>
23 #include <linux/wacom_i2c.h>
24
25 #include "wacom_i2c_func.h"
26 #include "wacom_i2c_flash.h"
27
28 struct i2c_client *g_client;
29
30 int wacom_i2c_frequency(char buf)
31 {
32         int ret;
33         if (g_client == NULL) {
34                 printk(KERN_DEBUG "[E-PEN]: failed to modify the frequency\n");
35                 return 0;
36         }
37         ret = i2c_master_send(g_client, &buf, sizeof(buf));
38         if (ret > 0) {
39                 printk(KERN_INFO "buf:%d, sent:%d\n", buf, ret);
40         } else {
41                 printk(KERN_ERR "Digitizer is not active\n");
42                 return -1;
43         }
44         return 0;
45 }
46 EXPORT_SYMBOL(wacom_i2c_frequency);
47
48 static void wacom_i2c_reset_hw(struct wacom_g5_platform_data *wac_pdata)
49 {
50         /* Reset IC */
51         wac_pdata->suspend_platform_hw();
52         msleep(200);
53         wac_pdata->resume_platform_hw();
54         msleep(200);
55 }
56
57 static irqreturn_t wacom_interrupt(int irq, void *dev_id)
58 {
59         struct wacom_i2c *wac_i2c = dev_id;
60         wacom_i2c_coord(wac_i2c);
61         return IRQ_HANDLED;
62 }
63
64 static void wacom_i2c_set_input_values(struct i2c_client *client,
65                                 struct wacom_i2c *wac_i2c,
66                                 struct input_dev *input_dev)
67 {
68         /*Set input values before registering input device*/
69
70         input_dev->name = "sec_e-pen";
71         input_dev->id.bustype = BUS_I2C;
72         input_dev->dev.parent = &client->dev;
73         input_dev->evbit[0] |= BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
74
75         __set_bit(ABS_X, input_dev->absbit);
76         __set_bit(ABS_Y, input_dev->absbit);
77         __set_bit(ABS_PRESSURE, input_dev->absbit);
78         __set_bit(BTN_TOUCH, input_dev->keybit);
79         __set_bit(EPEN_TOOL_PEN, input_dev->keybit);
80         __set_bit(EPEN_TOOL_RUBBER, input_dev->keybit);
81         __set_bit(EPEN_STYLUS, input_dev->keybit);
82         __set_bit(KEY_UNKNOWN, input_dev->keybit);
83         /*  __set_bit(BTN_STYLUS2, input_dev->keybit); */
84         /*  __set_bit(ABS_MISC, input_dev->absbit); */
85 }
86
87 static int wacom_check_emr_prox(struct wacom_g5_callbacks *cb)
88 {
89         struct wacom_i2c *wac = container_of(cb, struct wacom_i2c, callbacks);
90         printk(KERN_DEBUG "[E-PEN]:%s:\n", __func__);
91
92         return wac->pen_prox;
93 }
94
95 static int wacom_i2c_probe(struct i2c_client *client,
96                                 const struct i2c_device_id *id)
97 {
98         struct wacom_i2c *wac_i2c;
99         int i, ret;
100         i = ret = 0;
101
102         printk(KERN_DEBUG "[E-PEN]:%s:\n", __func__);
103
104         /*Check I2C functionality*/
105         if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
106                 goto err3;
107
108         /*Obtain kernel memory space for wacom i2c*/
109         wac_i2c = kzalloc(sizeof(struct wacom_i2c), GFP_KERNEL);
110         wac_i2c->wac_feature = &wacom_feature_EMR;
111
112         /*Initializing for semaphor*/
113         mutex_init(&wac_i2c->lock);
114
115         /*Register platform data*/
116         wac_i2c->wac_pdata = client->dev.platform_data;
117
118         /*Register callbacks*/
119         wac_i2c->callbacks.check_prox = wacom_check_emr_prox;
120         if (wac_i2c->wac_pdata->register_cb)
121                 wac_i2c->wac_pdata->register_cb(&wac_i2c->callbacks);
122
123         /*Register wacom i2c to input device*/
124         wac_i2c->input_dev = input_allocate_device();
125         if (wac_i2c == NULL || wac_i2c->input_dev == NULL)
126                 goto fail;
127         wacom_i2c_set_input_values(client, wac_i2c, wac_i2c->input_dev);
128
129         wac_i2c->client = client;
130         wac_i2c->irq = client->irq;
131
132         /*Change below if irq is needed*/
133         wac_i2c->irq_flag = 1;
134
135         /* Reset IC */
136         wacom_i2c_reset_hw(wac_i2c->wac_pdata);
137
138         wacom_i2c_query(wac_i2c);
139
140         if (wac_i2c->wac_pdata->xy_switch) {
141                 input_set_abs_params(wac_i2c->input_dev, ABS_X, 0,
142                         wac_i2c->wac_feature->y_max, 4, 0);
143                 input_set_abs_params(wac_i2c->input_dev, ABS_Y, 0,
144                         wac_i2c->wac_feature->x_max, 4, 0);
145         } else {
146                 input_set_abs_params(wac_i2c->input_dev, ABS_X, 0,
147                         wac_i2c->wac_feature->x_max, 4, 0);
148                 input_set_abs_params(wac_i2c->input_dev, ABS_Y, 0,
149                         wac_i2c->wac_feature->y_max, 4, 0);
150         }
151
152         input_set_abs_params(wac_i2c->input_dev, ABS_PRESSURE, 0,
153                         wac_i2c->wac_feature->pressure_max, 0, 0);
154         input_set_drvdata(wac_i2c->input_dev, wac_i2c);
155
156         /*Set client data*/
157         i2c_set_clientdata(client, wac_i2c);
158
159         /*Before registering input device, data in each input_dev must be set*/
160         if (input_register_device(wac_i2c->input_dev))
161                 goto err2;
162
163         /*Request IRQ*/
164         if (wac_i2c->irq_flag) {
165                 ret = request_threaded_irq(wac_i2c->irq, NULL,
166                 wacom_interrupt, IRQF_DISABLED|IRQF_TRIGGER_RISING|IRQF_ONESHOT,
167                  wac_i2c->name, wac_i2c);
168                 if (ret < 0)
169                         goto err1;
170         }
171
172         g_client = client;
173
174         /*  if(wac_i2c->irq_flag) */
175         /*   disable_irq(wac_i2c->irq); */
176
177
178         return 0;
179
180 err3:
181         printk(KERN_ERR "[E-PEN]:No I2C functionality found\n");
182         return -ENODEV;
183
184 err2:
185         printk(KERN_ERR "[E-PEN]:err2 occured\n");
186         input_free_device(wac_i2c->input_dev);
187         return -EIO;
188
189 err1:
190         printk(KERN_ERR "[E-PEN]:err1 occured(num:%d)\n", ret);
191         input_free_device(wac_i2c->input_dev);
192         wac_i2c->input_dev = NULL;
193         return -EIO;
194
195 fail:
196         printk(KERN_ERR "[E-PEN]:fail occured\n");
197         return -ENOMEM;
198 }
199
200 static int wacom_i2c_remove(struct i2c_client *client)
201 {
202         struct wacom_i2c *wac_i2c = i2c_get_clientdata(client);
203         free_irq(client->irq, wac_i2c);
204         input_unregister_device(wac_i2c->input_dev);
205         kfree(wac_i2c);
206
207         return 0;
208 }
209
210 static int wacom_i2c_suspend(struct i2c_client *client, pm_message_t mesg)
211 {
212 /*    int ret;
213 //    struct wacom_i2c *wac_i2c = i2c_get_clientdata(client);
214 //    ret = wac_i2c->power(0);
215 //    gpio_request(GPIO_PEN_SLP, "PEN_SLP");
216 //    s3c_gpio_cfgpin(GPIO_PEN_SLP, S3C_GPIO_OUTPUT);
217 //    gpio_direction_output(GPIO_PEN_SLP, 1);
218 */
219         printk(KERN_DEBUG "[E-PEN]:%s:\n", __func__);
220         return 0;
221 }
222
223 static int wacom_i2c_resume(struct i2c_client *client)
224 {
225 /*    gpio_request(GPIO_PEN_SLP, "PEN_SLP");
226 //    s3c_gpio_cfgpin(GPIO_PEN_SLP, S3C_GPIO_OUTPUT);
227 //    gpio_direction_output(GPIO_PEN_SLP, 0);
228 */
229         printk(KERN_DEBUG "[E-PEN]:%s:\n", __func__);
230         return 0;
231 }
232
233 static const struct i2c_device_id wacom_i2c_id[] = {
234         {"wacom_g5sp_i2c", 0},
235         {},
236 };
237
238 /*Create handler for wacom_i2c_driver*/
239 static struct i2c_driver wacom_i2c_driver = {
240         .driver = {
241                 .name = "wacom_g5sp_i2c",
242         },
243         .probe = wacom_i2c_probe,
244         .remove = wacom_i2c_remove,
245         .suspend = wacom_i2c_suspend,
246         .resume = wacom_i2c_resume,
247         .id_table = wacom_i2c_id,
248 };
249
250 static int __init wacom_i2c_init(void)
251 {
252         int ret = 0;
253         ret = i2c_add_driver(&wacom_i2c_driver);
254         if (ret)
255                 printk(KERN_ERR "[E-PEN]: fail to i2c_add_driver\n");
256         return ret;
257
258 }
259
260 static void __exit wacom_i2c_exit(void)
261 {
262         i2c_del_driver(&wacom_i2c_driver);
263 }
264
265
266 late_initcall(wacom_i2c_init);
267 module_exit(wacom_i2c_exit);
268
269 MODULE_AUTHOR("Samsung");
270 MODULE_DESCRIPTION("Driver for Wacom G5SP Digitizer Controller");
271
272 MODULE_LICENSE("GPL");
273