media: video: tegra: Fix kernel warning for DW9718
[linux-2.6.git] / drivers / media / video / tegra / dw9718.c
1 /*
2  * dw9718.c - dw9718 focuser driver
3  *
4  * Copyright (C) 2013, NVIDIA CORPORATION.  All rights reserved.
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms and conditions of the GNU General Public License,
8  * version 2, as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 /* Implementation
19  * --------------
20  * The board level details about the device need to be provided in the board
21  * file with the <device>_platform_data structure.
22  * Standard among NVC kernel drivers in this structure is:
23  * .cfg = Use the NVC_CFG_ defines that are in nvc.h.
24  *  Descriptions of the configuration options are with the defines.
25  *      This value is typically 0.
26  * .num = The number of the instance of the device.  This should start at 1 and
27  *      and increment for each device on the board.  This number will be
28  *      appended to the MISC driver name, Example: /dev/focuser.1
29  *      If not used or 0, then nothing is appended to the name.
30  * .sync = If there is a need to synchronize two devices, then this value is
31  *       the number of the device instance (.num above) this device is to
32  *       sync to.  For example:
33  *       Device 1 platform entries =
34  *       .num = 1,
35  *       .sync = 2,
36  *       Device 2 platfrom entries =
37  *       .num = 2,
38  *       .sync = 1,
39  *       The above example sync's device 1 and 2.
40  *       To disable sync, set .sync = 0.  Note that the .num = 0 device is not
41  *       allowed to be synced to.
42  *       This is typically used for stereo applications.
43  * .dev_name = The MISC driver name the device registers as.  If not used,
44  *       then the part number of the device is used for the driver name.
45  *       If using the NVC user driver then use the name found in this
46  *       driver under _default_pdata.
47  * .gpio_count = The ARRAY_SIZE of the nvc_gpio_pdata table.
48  * .gpio = A pointer to the nvc_gpio_pdata structure's platform GPIO data.
49  *       The GPIO mechanism works by cross referencing the .gpio_type key
50  *       among the nvc_gpio_pdata GPIO data and the driver's nvc_gpio_init
51  *       GPIO data to build a GPIO table the driver can use.  The GPIO's
52  *       defined in the device header file's _gpio_type enum are the
53  *       gpio_type keys for the nvc_gpio_pdata and nvc_gpio_init structures.
54  *       These need to be present in the board file's nvc_gpio_pdata
55  *       structure for the GPIO's that are used.
56  *       The driver's GPIO logic uses assert/deassert throughout until the
57  *       low level _gpio_wr/rd calls where the .assert_high is used to
58  *       convert the value to the correct signal level.
59  *       See the GPIO notes in nvc.h for additional information.
60  *
61  * The following is specific to NVC kernel focus drivers:
62  * .nvc = Pointer to the nvc_focus_nvc structure.  This structure needs to
63  *      be defined and populated if overriding the driver defaults.
64  * .cap = Pointer to the nvc_focus_cap structure.  This structure needs to
65  *      be defined and populated if overriding the driver defaults.
66  *
67  * The following is specific to this NVC kernel focus driver:
68  * .info = Pointer to the dw9718_pdata_info structure.  This structure does
69  *       not need to be defined and populated unless overriding ROM data.
70  *
71  * Power Requirements:
72  * The device's header file defines the voltage regulators needed with the
73  * enumeration <device>_vreg.  The order these are enumerated is the order
74  * the regulators will be enabled when powering on the device.  When the
75  * device is powered off the regulators are disabled in descending order.
76  * The <device>_vregs table in this driver uses the nvc_regulator_init
77  * structure to define the regulator ID strings that go with the regulators
78  * defined with <device>_vreg.  These regulator ID strings (or supply names)
79  * will be used in the regulator_get function in the _vreg_init function.
80  * The board power file and <device>_vregs regulator ID strings must match.
81  */
82
83 #include <linux/fs.h>
84 #include <linux/seq_file.h>
85 #include <linux/debugfs.h>
86 #include <linux/i2c.h>
87 #include <linux/slab.h>
88 #include <linux/delay.h>
89 #include <linux/uaccess.h>
90 #include <linux/list.h>
91 #include <linux/regulator/consumer.h>
92 #include <linux/gpio.h>
93 #include <linux/module.h>
94 #include <media/dw9718.h>
95
96 #define ENABLE_DEBUGFS_INTERFACE
97
98 #define dw9718_ID                       0x04
99 #define dw9718_FOCAL_LENGTH_FLOAT       (4.570f)
100 #define dw9718_FNUMBER_FLOAT            (2.8f)
101 #define dw9718_FOCAL_LENGTH             (0x40923D71) /* 4.570f */
102 #define dw9718_FNUMBER                  (0x40333333) /* 2.8f */
103 #define dw9718_SLEW_RATE                0x0060
104 #define dw9718_ACTUATOR_RANGE           1023
105 #define dw9718_SETTLETIME               30
106 #define dw9718_FOCUS_MACRO              620
107 #define dw9718_FOCUS_INFINITY           70
108 #define dw9718_POS_LOW_DEFAULT          0
109 #define dw9718_POS_HIGH_DEFAULT         1023
110 #define dw9718_POS_CLAMP                0x03ff
111 /* Need to decide exact value of VCM_THRESHOLD and its use */
112 /* define dw9718_VCM_THRESHOLD  20 */
113
114 struct dw9718_info {
115         struct i2c_client *i2c_client;
116         struct dw9718_platform_data *pdata;
117         struct miscdevice miscdev;
118         struct list_head list;
119         struct dw9718_power_rail power;
120         struct dw9718_info *s_info;
121         struct nvc_focus_nvc nvc;
122         struct nvc_focus_cap cap;
123         struct nv_focuser_config nv_config;
124         atomic_t in_use;
125         bool reset_flag;
126         int pwr_api;
127         int pwr_dev;
128         int status;
129         u32 cur_pos;
130         u8 s_mode;
131 };
132
133 /**
134  * The following are default values
135  */
136 static struct nvc_focus_cap dw9718_default_cap = {
137         .version = NVC_FOCUS_CAP_VER2,
138         .slew_rate = dw9718_SLEW_RATE,
139         .actuator_range = dw9718_ACTUATOR_RANGE,
140         .settle_time = dw9718_SETTLETIME,
141         .focus_macro = dw9718_FOCUS_MACRO,
142         .focus_infinity = dw9718_FOCUS_INFINITY,
143         .focus_hyper = dw9718_FOCUS_INFINITY,
144 };
145
146 static struct nvc_focus_nvc dw9718_default_nvc = {
147         .focal_length = dw9718_FOCAL_LENGTH,
148         .fnumber = dw9718_FNUMBER,
149         .max_aperature = dw9718_FNUMBER,
150 };
151
152 static struct dw9718_platform_data dw9718_default_pdata = {
153         .cfg = 0,
154         .num = 0,
155         .sync = 0,
156         .dev_name = "focuser",
157 };
158 static LIST_HEAD(dw9718_info_list);
159 static DEFINE_SPINLOCK(dw9718_spinlock);
160
161 static int dw9718_i2c_wr8(struct dw9718_info *info, u8 reg, u8 val)
162 {
163         struct i2c_msg msg;
164         u8 buf[2];
165         buf[0] = reg;
166         buf[1] = val;
167         msg.addr = info->i2c_client->addr;
168         msg.flags = 0;
169         msg.len = 2;
170         msg.buf = &buf[0];
171         if (i2c_transfer(info->i2c_client->adapter, &msg, 1) != 1)
172                 return -EIO;
173         return 0;
174 }
175
176 static int dw9718_i2c_wr16(struct dw9718_info *info, u8 reg, u16 val)
177 {
178         struct i2c_msg msg;
179         u8 buf[3];
180         buf[0] = reg;
181         buf[1] = (u8)(val >> 8);
182         buf[2] = (u8)(val & 0xff);
183         msg.addr = info->i2c_client->addr;
184         msg.flags = 0;
185         msg.len = 3;
186         msg.buf = &buf[0];
187         if (i2c_transfer(info->i2c_client->adapter, &msg, 1) != 1)
188                 return -EIO;
189         return 0;
190 }
191
192 static int dw9718_i2c_rd8(struct dw9718_info *info, u8 reg, u8 *val)
193 {
194         struct i2c_msg msg[2];
195         u8 buf[2];
196         buf[0] = reg;
197         msg[0].addr = info->i2c_client->addr;
198         msg[1].addr = info->i2c_client->addr;
199         msg[0].flags = 0;
200         msg[0].len = 1;
201         msg[0].buf = &buf[0];
202         msg[1].flags = I2C_M_RD;
203         msg[1].len = 1;
204         msg[1].buf = &buf[1];
205         *val = 0;
206         if (i2c_transfer(info->i2c_client->adapter, msg, 2) != 2)
207                 return -EIO;
208         *val = buf[1];
209         return 0;
210 }
211
212 /**
213  * Below are device specific functions.
214  */
215 static int dw9718_position_wr(struct dw9718_info *info, s32 position)
216 {
217         int err;
218
219         dev_dbg(&info->i2c_client->dev, "%s %d\n", __func__, position);
220         position &= dw9718_POS_CLAMP;
221         err = dw9718_i2c_wr16(info, DW9718_VCM_CODE_MSB, position);
222         if (!err)
223                 info->cur_pos = position;
224         else
225                 dev_err(&info->i2c_client->dev, "%s: ERROR set position %d",
226                         __func__, position);
227         return err;
228 }
229
230 int dw9718_set_arc_mode(struct dw9718_info *info)
231 {
232         int err;
233         u32 sr = info->nv_config.slew_rate;
234
235         dev_dbg(&info->i2c_client->dev, "%s %x\n", __func__, sr);
236         /* set ARC enable */
237         err = dw9718_i2c_wr8(info, DW9718_CONTROL, (sr >> 16) & 0xFF);
238         if (err) {
239                 dev_err(&info->i2c_client->dev,
240                 "%s: CONTROL reg write failed\n", __func__);
241                 goto set_arc_mode_done;
242         }
243         usleep_range(80, 100);
244
245         /* set the ARC RES2 */
246         err = dw9718_i2c_wr8(info, DW9718_SWITCH_MODE, (sr >> 8) & 0xFF);
247         if (err) {
248                 dev_err(&info->i2c_client->dev,
249                 "%s: MODE write failed\n", __func__);
250                 goto set_arc_mode_done;
251         }
252
253         err = dw9718_i2c_wr8(info, DW9718_SACT, sr & 0XFF);
254         if (err) {
255                 dev_err(&info->i2c_client->dev,
256                 "%s: RES write failed\n", __func__);
257                 goto set_arc_mode_done;
258         }
259
260         err = dw9718_position_wr(info, 0);
261
262 set_arc_mode_done:
263         return err;
264 }
265
266 static int dw9718_pm_wr(struct dw9718_info *info, int pwr)
267 {
268         int err = 0;
269         if ((info->pdata->cfg & (NVC_CFG_OFF2STDBY | NVC_CFG_BOOT_INIT)) &&
270                 (pwr == NVC_PWR_OFF || pwr == NVC_PWR_STDBY_OFF))
271                 pwr = NVC_PWR_STDBY;
272
273         if (pwr == info->pwr_dev)
274                 return 0;
275
276         switch (pwr) {
277         case NVC_PWR_OFF_FORCE:
278         case NVC_PWR_OFF:
279                 if (info->pdata && info->pdata->power_off)
280                         info->pdata->power_off(&info->power);
281                 break;
282         case NVC_PWR_STDBY_OFF:
283         case NVC_PWR_STDBY:
284                 if (info->pdata && info->pdata->power_off)
285                         info->pdata->power_off(&info->power);
286                 break;
287         case NVC_PWR_COMM:
288         case NVC_PWR_ON:
289                 if (info->pdata && info->pdata->power_on)
290                         info->pdata->power_on(&info->power);
291                 dw9718_set_arc_mode(info);
292                 break;
293         default:
294                 err = -EINVAL;
295                 break;
296         }
297
298         if (err < 0) {
299                 dev_err(&info->i2c_client->dev, "%s err %d\n", __func__, err);
300                 pwr = NVC_PWR_ERR;
301         }
302
303         info->pwr_dev = pwr;
304         dev_dbg(&info->i2c_client->dev, "%s pwr_dev=%d\n", __func__,
305                 info->pwr_dev);
306
307         return err;
308 }
309
310 static int dw9718_power_put(struct dw9718_power_rail *pw)
311 {
312         if (unlikely(!pw))
313                 return -EFAULT;
314
315         if (likely(pw->vdd))
316                 regulator_put(pw->vdd);
317
318         if (likely(pw->vdd_i2c))
319                 regulator_put(pw->vdd_i2c);
320
321         pw->vdd = NULL;
322         pw->vdd_i2c = NULL;
323
324         return 0;
325 }
326
327 static int dw9718_regulator_get(struct dw9718_info *info,
328         struct regulator **vreg, char vreg_name[])
329 {
330         struct regulator *reg = NULL;
331         int err = 0;
332
333         reg = regulator_get(&info->i2c_client->dev, vreg_name);
334         if (unlikely(IS_ERR(reg))) {
335                 dev_err(&info->i2c_client->dev, "%s %s ERR: %d\n",
336                         __func__, vreg_name, (int)reg);
337                 err = PTR_ERR(reg);
338                 reg = NULL;
339         } else
340                 dev_dbg(&info->i2c_client->dev, "%s: %s\n",
341                         __func__, vreg_name);
342
343         *vreg = reg;
344         return err;
345 }
346
347 static int dw9718_power_get(struct dw9718_info *info)
348 {
349         struct dw9718_power_rail *pw = &info->power;
350
351         dw9718_regulator_get(info, &pw->vdd, "vdd");
352         dw9718_regulator_get(info, &pw->vdd_i2c, "vdd_i2c");
353
354         return 0;
355 }
356
357 static int dw9718_pm_api_wr(struct dw9718_info *info, int pwr)
358 {
359         int err = 0;
360
361         if (!pwr || (pwr > NVC_PWR_ON))
362                 return 0;
363
364         if (pwr > info->pwr_dev)
365                 err = dw9718_pm_wr(info, pwr);
366         if (!err)
367                 info->pwr_api = pwr;
368         else
369                 info->pwr_api = NVC_PWR_ERR;
370         if (info->pdata->cfg & NVC_CFG_NOERR)
371                 return 0;
372
373         return err;
374 }
375
376 static int dw9718_pm_dev_wr(struct dw9718_info *info, int pwr)
377 {
378         if (pwr < info->pwr_api)
379                 pwr = info->pwr_api;
380         return dw9718_pm_wr(info, pwr);
381 }
382
383 static void dw9718_pm_exit(struct dw9718_info *info)
384 {
385         dw9718_pm_dev_wr(info, NVC_PWR_OFF_FORCE);
386         dw9718_power_put(&info->power);
387 }
388
389 static int dw9718_reset(struct dw9718_info *info, u32 level)
390 {
391         int err = 0;
392
393         if (level == NVC_RESET_SOFT) {
394                 err = dw9718_i2c_wr8(info, DW9718_POWER_DN, 0x01);
395                 usleep_range(200, 220);
396                 err |= dw9718_i2c_wr8(info, DW9718_POWER_DN, 0x00);
397                 usleep_range(100, 120);
398         } else
399                 err = dw9718_pm_dev_wr(info, NVC_PWR_OFF_FORCE);
400         err |= dw9718_pm_wr(info, info->pwr_api);
401         return err;
402 }
403
404 static int dw9718_detect(struct dw9718_info *info)
405 {
406         u8 val = 0;
407         int err;
408
409         dw9718_pm_dev_wr(info, NVC_PWR_COMM);
410         err = dw9718_i2c_rd8(info, 0, &val);
411         dw9718_pm_dev_wr(info, NVC_PWR_OFF);
412         return err;
413 }
414
415 static void dw9718_get_focuser_capabilities(struct dw9718_info *info)
416 {
417         memset(&info->nv_config, 0, sizeof(info->nv_config));
418
419         dev_dbg(&info->i2c_client->dev, "%s\n", __func__);
420         info->nv_config.focal_length = info->nvc.focal_length;
421         info->nv_config.fnumber = info->nvc.fnumber;
422         info->nv_config.max_aperture = info->nvc.fnumber;
423         info->nv_config.range_ends_reversed = 0;
424
425         info->nv_config.pos_working_low = info->cap.focus_infinity;
426         info->nv_config.pos_working_high = info->cap.focus_macro;
427         info->nv_config.pos_actual_low = dw9718_POS_LOW_DEFAULT;
428         info->nv_config.pos_actual_high = dw9718_POS_HIGH_DEFAULT;
429
430         info->nv_config.slew_rate = info->cap.slew_rate;
431         info->nv_config.circle_of_confusion = -1;
432         info->nv_config.num_focuser_sets = 1;
433         info->nv_config.focuser_set[0].macro = info->cap.focus_macro;
434         info->nv_config.focuser_set[0].hyper = info->cap.focus_hyper;
435         info->nv_config.focuser_set[0].inf = info->cap.focus_infinity;
436         info->nv_config.focuser_set[0].settle_time = info->cap.settle_time;
437 }
438
439 static int dw9718_set_focuser_capabilities(struct dw9718_info *info,
440                                         struct nvc_param *params)
441 {
442         dev_dbg(&info->i2c_client->dev, "%s\n", __func__);
443         if (copy_from_user(&info->nv_config,
444                 (const void __user *)params->p_value,
445                 sizeof(struct nv_focuser_config))) {
446                         dev_err(&info->i2c_client->dev,
447                         "%s Error: copy_from_user bytes %d\n",
448                         __func__, sizeof(struct nv_focuser_config));
449                         return -EFAULT;
450         }
451
452         /* set pre-set value, as currently ODM sets incorrect value */
453         if (info->pdata->cap->settle_time)
454                 info->cap.settle_time = info->pdata->cap->settle_time;
455         else
456                 info->cap.settle_time = dw9718_SETTLETIME;
457
458         dev_dbg(&info->i2c_client->dev,
459                 "%s: copy_from_user bytes %d info->cap.settle_time %d\n",
460                 __func__, sizeof(struct nv_focuser_config),
461                 info->cap.settle_time);
462
463         return 0;
464 }
465
466 static int dw9718_param_rd(struct dw9718_info *info, unsigned long arg)
467 {
468         struct nvc_param params;
469         const void *data_ptr = NULL;
470         u32 data_size = 0;
471
472         dev_dbg(&info->i2c_client->dev, "%s %lx\n", __func__, arg);
473         if (copy_from_user(&params,
474                 (const void __user *)arg,
475                 sizeof(struct nvc_param))) {
476                 dev_err(&info->i2c_client->dev, "%s %d copy_from_user err\n",
477                         __func__, __LINE__);
478                 return -EFAULT;
479         }
480         if (info->s_mode == NVC_SYNC_SLAVE)
481                 info = info->s_info;
482         switch (params.param) {
483         case NVC_PARAM_LOCUS:
484                 data_ptr = &info->cur_pos;
485                 data_size = sizeof(info->cur_pos);
486                 dev_dbg(&info->i2c_client->dev, "%s LOCUS: %d\n",
487                         __func__, info->cur_pos);
488                 break;
489         case NVC_PARAM_FOCAL_LEN:
490                 info->nvc.focal_length = dw9718_FOCAL_LENGTH;
491                 data_ptr = &info->nvc.focal_length;
492                 data_size = sizeof(info->nvc.focal_length);
493                 break;
494         case NVC_PARAM_MAX_APERTURE:
495                 data_ptr = &info->nvc.max_aperature;
496                 data_size = sizeof(info->nvc.max_aperature);
497                 dev_dbg(&info->i2c_client->dev, "%s MAX_APERTURE: %x\n",
498                                 __func__, info->nvc.max_aperature);
499                 break;
500         case NVC_PARAM_FNUMBER:
501                 data_ptr = &info->nvc.fnumber;
502                 data_size = sizeof(info->nvc.fnumber);
503                 dev_dbg(&info->i2c_client->dev, "%s FNUMBER: %u\n",
504                                 __func__, info->nvc.fnumber);
505                 break;
506         case NVC_PARAM_CAPS:
507                 /* send back just what's requested or our max size */
508                 dw9718_get_focuser_capabilities(info);
509                 data_ptr = &info->nv_config;
510                 data_size = sizeof(info->nv_config);
511                 dev_err(&info->i2c_client->dev, "%s CAPS\n", __func__);
512                 break;
513         case NVC_PARAM_STS:
514                 data_ptr = &info->status;
515                 data_size = sizeof(info->status);
516                 dev_dbg(&info->i2c_client->dev, "%s\n", __func__);
517                 break;
518         case NVC_PARAM_STEREO:
519                 data_ptr = &info->s_mode;
520                 data_size = sizeof(info->s_mode);
521                 dev_err(&info->i2c_client->dev, "%s STEREO: %d\n", __func__,
522                         info->s_mode);
523                 break;
524         default:
525                 dev_err(&info->i2c_client->dev,
526                         "%s unsupported parameter: %d\n",
527                         __func__, params.param);
528                 return -EINVAL;
529         }
530         if (params.sizeofvalue < data_size) {
531                 dev_err(&info->i2c_client->dev,
532                         "%s data size mismatch %d != %d Param: %d\n",
533                         __func__, params.sizeofvalue, data_size, params.param);
534                 return -EINVAL;
535         }
536         if (copy_to_user((void __user *)params.p_value, data_ptr, data_size)) {
537                 dev_err(&info->i2c_client->dev, "%s copy_to_user err line %d\n",
538                         __func__, __LINE__);
539                 return -EFAULT;
540         }
541         return 0;
542 }
543
544 static int dw9718_param_wr_s(struct dw9718_info *info,
545                 struct nvc_param *params, s32 s32val)
546 {
547         int err = 0;
548
549         switch (params->param) {
550         case NVC_PARAM_LOCUS:
551                 dev_dbg(&info->i2c_client->dev, "%s LOCUS: %d\n",
552                         __func__, s32val);
553                 err = dw9718_position_wr(info, s32val);
554                 break;
555         case NVC_PARAM_RESET:
556                 err = dw9718_reset(info, s32val);
557                 dev_dbg(&info->i2c_client->dev, "%s RESET\n", __func__);
558                 break;
559         case NVC_PARAM_SELF_TEST:
560                 dev_dbg(&info->i2c_client->dev, "%s SELF_TEST\n", __func__);
561                 break;
562         default:
563                 dev_dbg(&info->i2c_client->dev,
564                         "%s unsupported parameter: %d\n",
565                         __func__, params->param);
566                 err = -EINVAL;
567                 break;
568         }
569
570         if (err)
571                 dev_err(&info->i2c_client->dev, "ERROR! %d\n", err);
572         return err;
573 }
574
575 static int dw9718_param_wr(struct dw9718_info *info, unsigned long arg)
576 {
577         struct nvc_param params;
578         u8 u8val;
579         s32 s32val;
580         int err = 0;
581         if (copy_from_user(&params, (const void __user *)arg,
582                 sizeof(struct nvc_param))) {
583                 dev_err(&info->i2c_client->dev,
584                         "%s copy_from_user err line %d\n",
585                         __func__, __LINE__);
586                 return -EFAULT;
587         }
588         if (copy_from_user(&s32val,
589                 (const void __user *)params.p_value, sizeof(s32val))) {
590                 dev_err(&info->i2c_client->dev, "%s %d copy_from_user err\n",
591                         __func__, __LINE__);
592                 return -EFAULT;
593         }
594         u8val = (u8)s32val;
595         /* parameters independent of sync mode */
596         switch (params.param) {
597         case NVC_PARAM_STEREO:
598                 dev_dbg(&info->i2c_client->dev, "%s STEREO: %d\n",
599                         __func__, u8val);
600                 if (u8val == info->s_mode)
601                         return 0;
602                 switch (u8val) {
603                 case NVC_SYNC_OFF:
604                         info->s_mode = u8val;
605                         break;
606                 case NVC_SYNC_MASTER:
607                         info->s_mode = u8val;
608                         break;
609                 case NVC_SYNC_SLAVE:
610                         if (info->s_info != NULL) {
611                                 /* default slave lens position */
612                                 err = dw9718_position_wr(info->s_info,
613                                         info->s_info->cap.focus_infinity);
614                                 if (!err) {
615                                         info->s_mode = u8val;
616                                         info->s_info->s_mode = u8val;
617                                 } else {
618                                         if (info->s_mode != NVC_SYNC_STEREO)
619                                                 dw9718_pm_wr(info->s_info,
620                                                 NVC_PWR_OFF);
621                                                 err = -EIO;
622                                 }
623                         } else {
624                                 err = -EINVAL;
625                         }
626                         break;
627                 case NVC_SYNC_STEREO:
628                         if (info->s_info != NULL) {
629                                 /* sync power */
630                                 info->s_info->pwr_api = info->pwr_api;
631                                 /* move slave lens to master position */
632                                 err = dw9718_position_wr(info->s_info,
633                                         (s32)info->cur_pos);
634                                 if (!err) {
635                                         info->s_mode = u8val;
636                                         info->s_info->s_mode = u8val;
637                                 } else {
638                                         if (info->s_mode != NVC_SYNC_SLAVE)
639                                                 dw9718_pm_wr(info->s_info,
640                                                         NVC_PWR_OFF);
641                                         err = -EIO;
642                                 }
643                         } else {
644                                 err = -EINVAL;
645                         }
646                         break;
647                 default:
648                         err = -EINVAL;
649                 }
650                 if (info->pdata->cfg & NVC_CFG_NOERR)
651                         return 0;
652                 return err;
653
654         case NVC_PARAM_CAPS:
655                 if (dw9718_set_focuser_capabilities(info, &params)) {
656                         dev_err(&info->i2c_client->dev,
657                                 "%s: Error: copy_from_user bytes %d\n",
658                                 __func__, params.sizeofvalue);
659                         return -EFAULT;
660                 }
661                 return 0;
662
663         default:
664                 /* parameters dependent on sync mode */
665                 switch (info->s_mode) {
666                 case NVC_SYNC_OFF:
667                 case NVC_SYNC_MASTER:
668                         return dw9718_param_wr_s(info, &params, s32val);
669                 case NVC_SYNC_SLAVE:
670                         return dw9718_param_wr_s(info->s_info, &params, s32val);
671                 case NVC_SYNC_STEREO:
672                         err = dw9718_param_wr_s(info, &params, s32val);
673                         if (!(info->pdata->cfg & NVC_CFG_SYNC_I2C_MUX))
674                                 err |= dw9718_param_wr_s(info->s_info,
675                                                 &params, s32val);
676                         return err;
677                 default:
678                         dev_err(&info->i2c_client->dev, "%s %d internal err\n",
679                                         __func__, __LINE__);
680                         return -EINVAL;
681                 }
682         }
683 }
684
685 static long dw9718_ioctl(struct file *file,
686                                         unsigned int cmd,
687                                         unsigned long arg)
688 {
689         struct dw9718_info *info = file->private_data;
690         int pwr;
691         int err = 0;
692         switch (cmd) {
693         case NVC_IOCTL_PARAM_WR:
694                 dw9718_pm_dev_wr(info, NVC_PWR_ON);
695                 err = dw9718_param_wr(info, arg);
696                 dw9718_pm_dev_wr(info, NVC_PWR_OFF);
697                 return err;
698         case NVC_IOCTL_PARAM_RD:
699                 dw9718_pm_dev_wr(info, NVC_PWR_ON);
700                 err = dw9718_param_rd(info, arg);
701                 dw9718_pm_dev_wr(info, NVC_PWR_OFF);
702                 return err;
703         case NVC_IOCTL_PWR_WR:
704                 /* This is a Guaranteed Level of Service (GLOS) call */
705                 pwr = (int)arg * 2;
706                 dev_dbg(&info->i2c_client->dev, "%s PWR_WR: %d\n",
707                                 __func__, pwr);
708                 err = dw9718_pm_api_wr(info, pwr);
709                 return err;
710         case NVC_IOCTL_PWR_RD:
711                 if (info->s_mode == NVC_SYNC_SLAVE)
712                         pwr = info->s_info->pwr_api / 2;
713                 else
714                         pwr = info->pwr_api / 2;
715                 dev_dbg(&info->i2c_client->dev, "%s PWR_RD: %d\n",
716                                 __func__, pwr);
717                 if (copy_to_user((void __user *)arg,
718                         (const void *)&pwr, sizeof(pwr))) {
719                         dev_err(&info->i2c_client->dev,
720                                 "%s copy_to_user err line %d\n",
721                                 __func__, __LINE__);
722                         return -EFAULT;
723                 }
724                 return 0;
725         default:
726                 dev_dbg(&info->i2c_client->dev, "%s unsupported ioctl: %x\n",
727                         __func__, cmd);
728         }
729         return -EINVAL;
730 }
731
732
733 static void dw9718_sdata_init(struct dw9718_info *info)
734 {
735         /* set defaults */
736         memcpy(&info->nvc, &dw9718_default_nvc, sizeof(info->nvc));
737         memcpy(&info->cap, &dw9718_default_cap, sizeof(info->cap));
738
739         /* set overrides if any */
740         if (info->pdata->nvc) {
741                 if (info->pdata->nvc->fnumber)
742                         info->nvc.fnumber = info->pdata->nvc->fnumber;
743                 if (info->pdata->nvc->focal_length)
744                         info->nvc.focal_length = info->pdata->nvc->focal_length;
745                 if (info->pdata->nvc->max_aperature)
746                         info->nvc.max_aperature =
747                                 info->pdata->nvc->max_aperature;
748         }
749
750         if (info->pdata->cap) {
751                 if (info->pdata->cap->actuator_range)
752                         info->cap.actuator_range =
753                                 info->pdata->cap->actuator_range;
754                 if (info->pdata->cap->settle_time)
755                         info->cap.settle_time = info->pdata->cap->settle_time;
756                 if (info->pdata->cap->slew_rate)
757                         info->cap.slew_rate = info->pdata->cap->slew_rate;
758                 if (info->pdata->cap->focus_macro)
759                         info->cap.focus_macro = info->pdata->cap->focus_macro;
760                 if (info->pdata->cap->focus_hyper)
761                         info->cap.focus_hyper = info->pdata->cap->focus_hyper;
762                 if (info->pdata->cap->focus_infinity)
763                         info->cap.focus_infinity =
764                                 info->pdata->cap->focus_infinity;
765         }
766 }
767
768 static int dw9718_sync_en(unsigned num, unsigned sync)
769 {
770         struct dw9718_info *master = NULL;
771         struct dw9718_info *slave = NULL;
772         struct dw9718_info *pos = NULL;
773         rcu_read_lock();
774         list_for_each_entry_rcu(pos, &dw9718_info_list, list) {
775                 if (pos->pdata->num == num) {
776                         master = pos;
777                         break;
778                 }
779         }
780         pos = NULL;
781         list_for_each_entry_rcu(pos, &dw9718_info_list, list) {
782                 if (pos->pdata->num == sync) {
783                         slave = pos;
784                         break;
785                 }
786         }
787         rcu_read_unlock();
788         if (master != NULL)
789                 master->s_info = NULL;
790         if (slave != NULL)
791                 slave->s_info = NULL;
792         if (!sync)
793                 return 0; /* no err if sync disabled */
794         if (num == sync)
795                 return -EINVAL; /* err if sync instance is itself */
796         if ((master != NULL) && (slave != NULL)) {
797                 master->s_info = slave;
798                 slave->s_info = master;
799         }
800         return 0;
801 }
802
803 static int dw9718_sync_dis(struct dw9718_info *info)
804 {
805         if (info->s_info != NULL) {
806                 info->s_info->s_mode = 0;
807                 info->s_info->s_info = NULL;
808                 info->s_mode = 0;
809                 info->s_info = NULL;
810                 return 0;
811         }
812         return -EINVAL;
813 }
814
815 static int dw9718_open(struct inode *inode, struct file *file)
816 {
817         struct dw9718_info *info = NULL;
818         struct dw9718_info *pos = NULL;
819         int err;
820         rcu_read_lock();
821         list_for_each_entry_rcu(pos, &dw9718_info_list, list) {
822                 if (pos->miscdev.minor == iminor(inode)) {
823                         info = pos;
824                         break;
825                 }
826         }
827         rcu_read_unlock();
828         if (!info)
829                 return -ENODEV;
830         err = dw9718_sync_en(info->pdata->num, info->pdata->sync);
831         if (err == -EINVAL)
832                 dev_err(&info->i2c_client->dev,
833                         "%s err: invalid num (%u) and sync (%u) instance\n",
834                         __func__, info->pdata->num, info->pdata->sync);
835         if (atomic_xchg(&info->in_use, 1))
836                 return -EBUSY;
837         if (info->s_info != NULL) {
838                 if (atomic_xchg(&info->s_info->in_use, 1))
839                         return -EBUSY;
840         }
841         file->private_data = info;
842         dw9718_pm_dev_wr(info, NVC_PWR_ON);
843         dw9718_position_wr(info, info->cap.focus_infinity);
844         dw9718_pm_dev_wr(info, NVC_PWR_OFF);
845         dev_dbg(&info->i2c_client->dev, "%s\n", __func__);
846
847         return 0;
848 }
849
850 static int dw9718_release(struct inode *inode, struct file *file)
851 {
852         struct dw9718_info *info = file->private_data;
853         dev_dbg(&info->i2c_client->dev, "%s\n", __func__);
854         dw9718_pm_wr(info, NVC_PWR_OFF);
855         file->private_data = NULL;
856         WARN_ON(!atomic_xchg(&info->in_use, 0));
857         if (info->s_info != NULL)
858                 WARN_ON(!atomic_xchg(&info->s_info->in_use, 0));
859         dw9718_sync_dis(info);
860         return 0;
861 }
862
863 static const struct file_operations dw9718_fileops = {
864         .owner = THIS_MODULE,
865         .open = dw9718_open,
866         .unlocked_ioctl = dw9718_ioctl,
867         .release = dw9718_release,
868 };
869
870 static void dw9718_del(struct dw9718_info *info)
871 {
872         dw9718_pm_exit(info);
873         if ((info->s_mode == NVC_SYNC_SLAVE) ||
874                 (info->s_mode == NVC_SYNC_STEREO))
875                 dw9718_pm_exit(info->s_info);
876
877         dw9718_sync_dis(info);
878         spin_lock(&dw9718_spinlock);
879         list_del_rcu(&info->list);
880         spin_unlock(&dw9718_spinlock);
881         synchronize_rcu();
882 }
883
884 static int dw9718_remove(struct i2c_client *client)
885 {
886         struct dw9718_info *info = i2c_get_clientdata(client);
887         dev_dbg(&info->i2c_client->dev, "%s\n", __func__);
888         misc_deregister(&info->miscdev);
889         dw9718_del(info);
890         return 0;
891 }
892
893 static int nvc_debugfs_init(const char *dir_name,
894         struct dentry **d_entry, struct dentry **f_entry, void *info);
895
896 static int dw9718_probe(
897                 struct i2c_client *client,
898                 const struct i2c_device_id *id)
899 {
900         struct dw9718_info *info;
901         char dname[16];
902         int err;
903         dev_dbg(&client->dev, "%s\n", __func__);
904         pr_info("dw9718: probing focuser.\n");
905
906         info = devm_kzalloc(&client->dev, sizeof(*info), GFP_KERNEL);
907         if (info == NULL) {
908                 dev_err(&client->dev, "%s: kzalloc error\n", __func__);
909                 return -ENOMEM;
910         }
911         info->i2c_client = client;
912         if (client->dev.platform_data) {
913                 info->pdata = client->dev.platform_data;
914         } else {
915                 info->pdata = &dw9718_default_pdata;
916                 dev_dbg(&client->dev, "%s No platform data.  Using defaults.\n",
917                         __func__);
918         }
919
920         i2c_set_clientdata(client, info);
921         INIT_LIST_HEAD(&info->list);
922         spin_lock(&dw9718_spinlock);
923         list_add_rcu(&info->list, &dw9718_info_list);
924         spin_unlock(&dw9718_spinlock);
925         dw9718_power_get(info);
926         dw9718_sdata_init(info);
927         info->pwr_api = NVC_PWR_OFF;
928         if (info->pdata->cfg & (NVC_CFG_NODEV | NVC_CFG_BOOT_INIT)) {
929                 err = dw9718_detect(info);
930                 if (err < 0) {
931                         dev_err(&client->dev, "%s device not found\n",
932                                 __func__);
933                         if (info->pdata->cfg & NVC_CFG_NODEV) {
934                                 dw9718_del(info);
935                                 return -ENODEV;
936                         }
937                 } else {
938                         dev_dbg(&client->dev, "%s device found\n", __func__);
939                         if (info->pdata->cfg & NVC_CFG_BOOT_INIT) {
940                                 /* initial move causes full initialization */
941                                 dw9718_pm_dev_wr(info, NVC_PWR_ON);
942                                 dw9718_position_wr(
943                                         info, info->cap.focus_infinity);
944                                 dw9718_pm_dev_wr(info, NVC_PWR_OFF);
945                         }
946                         if (info->pdata->detect)
947                                 info->pdata->detect(NULL, 0);
948                 }
949         }
950
951         if (info->pdata->dev_name != 0)
952                 strcpy(dname, info->pdata->dev_name);
953         else
954                 strcpy(dname, "dw9718");
955
956         if (info->pdata->num)
957                 snprintf(dname, sizeof(dname),
958                         "%s.%u", dname, info->pdata->num);
959
960         info->miscdev.name = dname;
961         info->miscdev.fops = &dw9718_fileops;
962         info->miscdev.minor = MISC_DYNAMIC_MINOR;
963         if (misc_register(&info->miscdev)) {
964                 dev_err(&client->dev, "%s unable to register misc device %s\n",
965                         __func__, dname);
966                 dw9718_del(info);
967                 return -ENODEV;
968         }
969
970         nvc_debugfs_init(
971                 info->miscdev.this_device->kobj.name, NULL, NULL, info);
972
973         return 0;
974 }
975
976 #ifdef ENABLE_DEBUGFS_INTERFACE
977 static int nvc_status_show(struct seq_file *s, void *data)
978 {
979         struct dw9718_info *k_info = s->private;
980         struct nv_focuser_config *pcfg = &k_info->nv_config;
981         struct nvc_focus_cap *pcap = &k_info->cap;
982
983         pr_info("%s\n", __func__);
984
985         seq_printf(s, "focuser status:\n"
986                 "    Limit          = (%04d - %04d)\n"
987                 "    Range          = (%04d - %04d)\n"
988                 "    Current Pos    = %04d\n"
989                 "    Settle time    = %04d\n"
990                 "    Macro          = %04d\n"
991                 "    Infinity       = %04d\n"
992                 "    Hyper          = %04d\n"
993                 "    SlewRate       = 0x%06x\n"
994                 ,
995                 pcfg->pos_actual_low, pcfg->pos_actual_high,
996                 pcfg->pos_working_low, pcfg->pos_working_high,
997                 k_info->cur_pos,
998                 pcap->settle_time,
999                 pcap->focus_macro,
1000                 pcap->focus_infinity,
1001                 pcap->focus_hyper,
1002                 pcfg->slew_rate
1003                 );
1004
1005         return 0;
1006 }
1007
1008 static ssize_t nvc_attr_set(struct file *s,
1009         const char __user *user_buf, size_t count, loff_t *ppos)
1010 {
1011         struct dw9718_info *k_info =
1012                 ((struct seq_file *)s->private_data)->private;
1013         struct nv_focuser_config *pcfg = &k_info->nv_config;
1014         char buf[24];
1015         int buf_size;
1016         int err;
1017         u32 val = 0;
1018
1019         pr_info("%s (%d)\n", __func__, count);
1020
1021         if (!user_buf || count <= 1)
1022                 return -EFAULT;
1023
1024         memset(buf, 0, sizeof(buf));
1025         buf_size = min(count, sizeof(buf) - 1);
1026         if (copy_from_user(buf, user_buf, buf_size))
1027                 return -EFAULT;
1028
1029         if (sscanf(buf + 1, "0x%x", &val) == 1)
1030                 goto set_attr;
1031         if (sscanf(buf + 1, "0X%x", &val) == 1)
1032                 goto set_attr;
1033         if (sscanf(buf + 1, "%d", &val) == 1)
1034                 goto set_attr;
1035
1036         pr_err("SYNTAX ERROR: %s\n", buf);
1037         return -EFAULT;
1038
1039 set_attr:
1040         pr_info("new data = %x\n", val);
1041         switch (buf[0]) {
1042         case 'p':
1043                 pr_info("new pos = %d\n", val);
1044                 err = dw9718_position_wr(k_info, val);
1045                 if (err)
1046                         pr_err("ERROR set position %x\n", val);
1047                 break;
1048         case 'h':
1049                 if (val <= pcfg->pos_working_low || val >= 1024) {
1050                         pr_info("new pos_high(%d) out of range\n",
1051                                 val);
1052                         break;
1053                 }
1054                 pr_info("new pos_high = %d\n", val);
1055                 pcfg->pos_working_high = val;
1056                 break;
1057         case 'l':
1058                 if (val >= pcfg->pos_working_high) {
1059                         pr_info("new pos_low(%d) out of range\n",
1060                                 val);
1061                         break;
1062                 }
1063                 pr_info("new pos_low = %d\n", val);
1064                 pcfg->pos_working_low = val;
1065                 break;
1066         case 'm':
1067                 pr_info("new vcm mode = %x\n", val);
1068                 pcfg->slew_rate = val;
1069                 dw9718_set_arc_mode(k_info);
1070                 break;
1071         }
1072
1073         return count;
1074 }
1075
1076 static int nvc_debugfs_open(struct inode *inode, struct file *file)
1077 {
1078         return single_open(file, nvc_status_show, inode->i_private);
1079 }
1080
1081 static const struct file_operations nvc_debugfs_fops = {
1082         .open = nvc_debugfs_open,
1083         .read = seq_read,
1084         .write = nvc_attr_set,
1085         .llseek = seq_lseek,
1086         .release = single_release,
1087 };
1088
1089 static int nvc_debugfs_init(const char *dir_name,
1090         struct dentry **d_entry, struct dentry **f_entry, void *info)
1091 {
1092         struct dentry *dp, *fp;
1093
1094         dp = debugfs_create_dir(dir_name, NULL);
1095         if (dp == NULL) {
1096                 pr_info("%s: debugfs create dir failed\n", __func__);
1097                 return -ENOMEM;
1098         }
1099
1100         fp = debugfs_create_file("d", S_IRUGO|S_IWUSR,
1101                 dp, info, &nvc_debugfs_fops);
1102         if (!fp) {
1103                 pr_info("%s: debugfs create file failed\n", __func__);
1104                 debugfs_remove_recursive(dp);
1105                 return -ENOMEM;
1106         }
1107
1108         if (d_entry)
1109                 *d_entry = dp;
1110         if (f_entry)
1111                 *f_entry = fp;
1112         return 0;
1113 }
1114 #else
1115 static int nvc_debugfs_init(const char *dir_name,
1116         struct dentry **d_entry, struct dentry **f_entry, void *info)
1117 {
1118         return 0;
1119 }
1120 #endif
1121
1122
1123 static const struct i2c_device_id dw9718_id[] = {
1124         { "dw9718", 0 },
1125         { },
1126 };
1127
1128
1129 MODULE_DEVICE_TABLE(i2c, dw9718_id);
1130
1131 static struct i2c_driver dw9718_i2c_driver = {
1132         .driver = {
1133                 .name = "dw9718",
1134                 .owner = THIS_MODULE,
1135         },
1136         .id_table = dw9718_id,
1137         .probe = dw9718_probe,
1138         .remove = dw9718_remove,
1139 };
1140
1141 static int __init dw9718_init(void)
1142 {
1143         return i2c_add_driver(&dw9718_i2c_driver);
1144 }
1145
1146 static void __exit dw9718_exit(void)
1147 {
1148         i2c_del_driver(&dw9718_i2c_driver);
1149 }
1150
1151 module_init(dw9718_init);
1152 module_exit(dw9718_exit);
1153 MODULE_LICENSE("GPL v2");