media: video: tegra: sh532u focuser driver
[linux-3.10.git] / drivers / media / video / tegra / sh532u.c
1 /*
2  * SH532U focuser driver.
3  *
4  * Copyright (C) 2011 NVIDIA Corporation.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
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., 59 Temple Place, Suite 330, Boston, MA
18  * 02111-1307, USA
19  */
20
21 /* Implementation
22  * --------------
23  * The board level details about the device need to be provided in the board
24  * file with the sh532u_platform_data structure.
25  * Standard among NVC kernel drivers in this structure is:
26  * .cfg = Use the NVC_CFG_ defines that are in nvc.h.
27  *        Descriptions of the configuration options are with the defines.
28  *        This value is typically 0.
29  * .num = The number of the instance of the device.  This should start at 1 and
30  *        and increment for each device on the board.  This number will be
31  *        appended to the MISC driver name, Example: /dev/focuser.1
32  *        If not used or 0, then nothing is appended to the name.
33  * .sync = If there is a need to synchronize two devices, then this value is
34  *         the number of the device instance (.num above) this device is to
35  *         sync to.  For example:
36  *         Device 1 platform entries =
37  *         .num = 1,
38  *         .sync = 2,
39  *         Device 2 platfrom entries =
40  *         .num = 2,
41  *         .sync = 1,
42  *         The above example sync's device 1 and 2.
43  *         This is typically used for stereo applications.
44  * .dev_name = The MISC driver name the device registers as.  If not used,
45  *             then the part number of the device is used for the driver name.
46  *             If using the NVC user driver then use the name found in this
47  *             driver under _default_pdata.
48  *
49  * The following is specific to NVC kernel focus drivers:
50  * .nvc = Pointer to the nvc_focus_nvc structure.  This structure needs to
51  *        be defined and populated if overriding the driver defaults.
52  * .cap = Pointer to the nvc_focus_cap structure.  This structure needs to
53  *        be defined and populated if overriding the driver defaults.
54  *
55  * The following is specific to only this NVC kernel focus driver:
56  * .info = Pointer to the sh532u_pdata_info structure.  This structure does
57  *         not need to be defined and populated unless overriding ROM data.
58 .* .i2c_addr_rom = The I2C address of the onboard ROM.
59  * .gpio_reset = The GPIO connected to the devices reset.  If not used then
60  *               leave blank.
61  * .gpio_en = Due to a Linux limitation, a GPIO is defined to "enable" the
62  *            device.  This workaround is for when the device's power GPIO's
63  *            are behind an I2C expander.  The Linux limitation doesn't allow
64  *            the I2C GPIO expander to be ready for use when this device is
65  *            probed.  When this problem is solved, this driver needs to
66  *            remove the gpio_en WAR.
67  *
68  * Power Requirements
69  * The board power file must contain the following labels for the power
70  * regulator(s) of this device:
71  * "vdd" = the power regulator for the device's power.
72  * "vdd_i2c" = the power regulator for the I2C power.
73  *
74  * The above values should be all that is needed to use the device with this
75  * driver.  Modifications of this driver should not be needed.
76  */
77
78
79 #include <linux/fs.h>
80 #include <linux/i2c.h>
81 #include <linux/miscdevice.h>
82 #include <linux/slab.h>
83 #include <linux/delay.h>
84 #include <linux/uaccess.h>
85 #include <linux/list.h>
86 #include <linux/jiffies.h>
87 #include <linux/gpio.h>
88 #include <linux/module.h>
89
90 #include <media/nvc.h>
91 #include <media/sh532u.h>
92
93 #define SH532U_ID               0xF0
94 /* defaults if no ROM data */
95 #define SH532U_HYPERFOCAL_RATIO 1836 /* 41.2f/224.4f Ratio source: SEMCO */
96 /* _HYPERFOCAL_RATIO is multiplied and _HYPERFOCAL_DIV divides for float */
97 #define SH532U_HYPERFOCAL_DIV   10000
98 #define SH532U_FOCAL_LENGTH     0x408D70A4
99 #define SH532U_FNUMBER          0x40333333
100 #define SH532U_MAX_APERATURE    0x3FCA0EA1
101 /* SH532U_CAPS_VER = 0: invalid value */
102 /* SH532U_CAPS_VER = 1: added NVC_PARAM_STS */
103 /* SH532U_CAPS_VER = 2: expanded nvc_focus_cap */
104 #define SH532U_CAPS_VER         2
105 #define SH532U_ACTUATOR_RANGE   1000
106 #define SH532U_SETTLETIME       30
107 #define SH532U_FOCUS_MACRO      950
108 #define SH532U_FOCUS_HYPER      250
109 #define SH532U_FOCUS_INFINITY   50
110 #define SH532U_TIMEOUT_MS       200
111 #define SH532U_POS_LOW_DEFAULT  0xA000
112 #define SH532U_POS_HIGH_DEFAULT 0x6000
113
114
115 struct sh532u_info {
116         atomic_t in_use;
117         struct i2c_client *i2c_client;
118         struct sh532u_platform_data *pdata;
119         struct miscdevice miscdev;
120         struct list_head list;
121         int pwr_api;
122         int pwr_dev;
123         struct nvc_regulator vreg_vdd;
124         struct nvc_regulator vreg_i2c;
125         u8 s_mode;
126         struct sh532u_info *s_info;
127         unsigned i2c_addr_rom;
128         struct nvc_focus_nvc nvc;
129         struct nvc_focus_cap cap;
130         enum nvc_focus_sts sts;
131         struct sh532u_pdata_info cfg;
132         bool gpio_flag_reset;
133         bool init_cal_flag;
134         s16 abs_base;
135         u32 abs_range;
136         u32 pos_rel;
137         s16 pos_abs;
138         long pos_time_wr;
139 };
140
141 static struct sh532u_pdata_info sh532u_default_info = {
142         .move_timeoutms = SH532U_TIMEOUT_MS,
143         .focus_hyper_ratio = SH532U_HYPERFOCAL_RATIO,
144         .focus_hyper_div = SH532U_HYPERFOCAL_DIV,
145 };
146
147 static struct nvc_focus_cap sh532u_default_cap = {
148         .version        = SH532U_CAPS_VER,
149         .actuator_range = SH532U_ACTUATOR_RANGE,
150         .settle_time    = SH532U_SETTLETIME,
151         .focus_macro    = SH532U_FOCUS_MACRO,
152         .focus_hyper    = SH532U_FOCUS_HYPER,
153         .focus_infinity = SH532U_FOCUS_INFINITY,
154 };
155
156 static struct nvc_focus_nvc sh532u_default_nvc = {
157         .focal_length   = SH532U_FOCAL_LENGTH,
158         .fnumber        = SH532U_FNUMBER,
159         .max_aperature  = SH532U_MAX_APERATURE,
160 };
161
162 static struct sh532u_platform_data sh532u_default_pdata = {
163         .cfg            = 0,
164         .num            = 0,
165         .sync           = 0,
166         .dev_name       = "focuser",
167         .i2c_addr_rom   = 0x50,
168 };
169
170 static u32 sh532u_a2buf[] = {
171         0x0018019c,
172         0x0018019d,
173         0x0000019e,
174         0x007f0192,
175         0x00000194,
176         0x00f00184,
177         0x00850187,
178         0x0000018a,
179         0x00fd7187,
180         0x007f7183,
181         0x0008025a,
182         0x05042218,
183         0x80010216,
184         0x000601a0,
185         0x00808183,
186         0xffffffff
187 };
188
189 static LIST_HEAD(sh532u_info_list);
190 static DEFINE_SPINLOCK(sh532u_spinlock);
191
192
193 static int sh532u_i2c_rd8(struct sh532u_info *info, u8 addr, u8 reg, u8 *val)
194 {
195         struct i2c_msg msg[2];
196         u8 buf[2];
197
198         buf[0] = reg;
199         if (addr) {
200                 msg[0].addr = addr;
201                 msg[1].addr = addr;
202         } else {
203                 msg[0].addr = info->i2c_client->addr;
204                 msg[1].addr = info->i2c_client->addr;
205         }
206         msg[0].flags = 0;
207         msg[0].len = 1;
208         msg[0].buf = &buf[0];
209         msg[1].flags = I2C_M_RD;
210         msg[1].len = 1;
211         msg[1].buf = &buf[1];
212         *val = 0;
213         if (i2c_transfer(info->i2c_client->adapter, msg, 2) != 2)
214                 return -EIO;
215
216         *val = buf[1];
217         return 0;
218 }
219
220 static int sh532u_i2c_wr8(struct sh532u_info *info, u8 reg, u8 val)
221 {
222         struct i2c_msg msg;
223         u8 buf[2];
224
225         buf[0] = reg;
226         buf[1] = val;
227         msg.addr = info->i2c_client->addr;
228         msg.flags = 0;
229         msg.len = 2;
230         msg.buf = &buf[0];
231         if (i2c_transfer(info->i2c_client->adapter, &msg, 1) != 1)
232                 return -EIO;
233
234         return 0;
235 }
236
237 static int sh532u_i2c_rd16(struct sh532u_info *info, u8 reg, u16 *val)
238 {
239         struct i2c_msg msg[2];
240         u8 buf[3];
241
242         buf[0] = reg;
243         msg[0].addr = info->i2c_client->addr;
244         msg[0].flags = 0;
245         msg[0].len = 1;
246         msg[0].buf = &buf[0];
247         msg[1].addr = info->i2c_client->addr;
248         msg[1].flags = I2C_M_RD;
249         msg[1].len = 2;
250         msg[1].buf = &buf[1];
251         if (i2c_transfer(info->i2c_client->adapter, msg, 2) != 2)
252                 return -EIO;
253
254         *val = (((u16)buf[1] << 8) | (u16)buf[2]);
255         return 0;
256 }
257
258
259 static int sh532u_i2c_wr16(struct sh532u_info *info, u8 reg, u16 val)
260 {
261         struct i2c_msg msg;
262         u8 buf[3];
263
264         buf[0] = reg;
265         buf[1] = (u8)(val >> 8);
266         buf[2] = (u8)(val & 0xff);
267         msg.addr = info->i2c_client->addr;
268         msg.flags = 0;
269         msg.len = 3;
270         msg.buf = &buf[0];
271         if (i2c_transfer(info->i2c_client->adapter, &msg, 1) != 1)
272                 return -EIO;
273
274         return 0;
275 }
276
277 static int sh532u_i2c_rd32(struct sh532u_info *info, u8 addr, u8 reg, u32 *val)
278 {
279         struct i2c_msg msg[2];
280         u8 buf[5];
281
282         buf[0] = reg;
283         if (addr) {
284                 msg[0].addr = addr;
285                 msg[1].addr = addr;
286         } else {
287                 msg[0].addr = info->i2c_client->addr;
288                 msg[1].addr = info->i2c_client->addr;
289         }
290         msg[0].flags = 0;
291         msg[0].len = 1;
292         msg[0].buf = &buf[0];
293         msg[1].flags = I2C_M_RD;
294         msg[1].len = 4;
295         msg[1].buf = &buf[1];
296         if (i2c_transfer(info->i2c_client->adapter, msg, 2) != 2)
297                 return -EIO;
298
299         *val = (((u32)buf[4] << 24) | ((u32)buf[3] << 16) |
300                         ((u32)buf[2] << 8) | ((u32)buf[1]));
301         return 0;
302 }
303
304 static void sh532u_gpio_en(struct sh532u_info *info, int val)
305 {
306         if (info->pdata->gpio_en)
307                 gpio_set_value_cansleep(info->pdata->gpio_en, val);
308 }
309
310 static void sh532u_gpio_reset(struct sh532u_info *info, int val)
311 {
312         if (val) {
313                 if (!info->gpio_flag_reset && info->pdata->gpio_reset) {
314                         gpio_set_value_cansleep(info->pdata->gpio_reset, 0);
315                         mdelay(1);
316                         gpio_set_value_cansleep(info->pdata->gpio_reset, 1);
317                         mdelay(10); /* delay for device startup */
318                         info->gpio_flag_reset = 1;
319                 }
320         } else {
321                 info->gpio_flag_reset = 0;
322         }
323 }
324
325 static void sh532u_pm_regulator_put(struct nvc_regulator *sreg)
326 {
327         regulator_put(sreg->vreg);
328         sreg->vreg = NULL;
329 }
330
331 static int sh532u_pm_regulator_get(struct sh532u_info *info,
332                                    struct nvc_regulator *sreg,
333                                    char vreg_name[])
334 {
335         int err = 0;
336
337         sreg->vreg_flag = 0;
338         sreg->vreg = regulator_get(&info->i2c_client->dev, vreg_name);
339         if (IS_ERR_OR_NULL(sreg->vreg)) {
340                 dev_err(&info->i2c_client->dev,
341                                 "%s err for regulator: %s err: %d\n",
342                                 __func__, vreg_name, (int)sreg->vreg);
343                 err = PTR_ERR(sreg->vreg);
344                 sreg->vreg = NULL;
345         } else {
346                 sreg->vreg_name = vreg_name;
347                 dev_dbg(&info->i2c_client->dev,
348                                 "%s vreg_name: %s\n",
349                                 __func__, sreg->vreg_name);
350         }
351         return err;
352 }
353
354 static int sh532u_pm_regulator_en(struct sh532u_info *info,
355                                   struct nvc_regulator *sreg)
356 {
357         int err = 0;
358
359         if (!sreg->vreg_flag && (sreg->vreg != NULL)) {
360                 err = regulator_enable(sreg->vreg);
361                 if (!err) {
362                         dev_dbg(&info->i2c_client->dev,
363                                         "%s vreg_name: %s\n",
364                                         __func__, sreg->vreg_name);
365                         sreg->vreg_flag = 1;
366                         err = 1; /* flag regulator state change */
367                 } else {
368                         dev_err(&info->i2c_client->dev,
369                                         "%s err, regulator: %s\n",
370                                         __func__, sreg->vreg_name);
371                 }
372         }
373         return err;
374 }
375
376 static int sh532u_pm_regulator_dis(struct sh532u_info *info,
377                                    struct nvc_regulator *sreg)
378 {
379         int err = 0;
380
381         if (sreg->vreg_flag && (sreg->vreg != NULL)) {
382                 err = regulator_disable(sreg->vreg);
383                 if (err)
384                         dev_err(&info->i2c_client->dev,
385                                         "%s err, regulator: %s\n",
386                                         __func__, sreg->vreg_name);
387         }
388         sreg->vreg_flag = 0;
389         return err;
390 }
391
392 static int sh532u_pm_wr(struct sh532u_info *info, int pwr)
393 {
394         int err = 0;
395
396         if ((info->pdata->cfg & (NVC_CFG_OFF2STDBY | NVC_CFG_BOOT_INIT)) &&
397                         (pwr == NVC_PWR_OFF ||
398                          pwr == NVC_PWR_STDBY_OFF))
399                 pwr = NVC_PWR_STDBY;
400
401         if (pwr == info->pwr_dev)
402                 return 0;
403
404         switch (pwr) {
405         case NVC_PWR_OFF_FORCE:
406         case NVC_PWR_OFF:
407                 sh532u_gpio_en(info, 0);
408                 err = sh532u_pm_regulator_dis(info, &info->vreg_vdd);
409                 err |= sh532u_pm_regulator_dis(info, &info->vreg_i2c);
410                 sh532u_gpio_reset(info, 0);
411                 break;
412
413         case NVC_PWR_STDBY_OFF:
414         case NVC_PWR_STDBY:
415                 err = sh532u_pm_regulator_en(info, &info->vreg_vdd);
416                 err |= sh532u_pm_regulator_en(info, &info->vreg_i2c);
417                 sh532u_gpio_en(info, 1);
418                 sh532u_gpio_reset(info, 1);
419                 err |= sh532u_i2c_wr8(info, STBY_211, 0x80);
420                 err |= sh532u_i2c_wr8(info, CLKSEL_211, 0x38);
421                 err |= sh532u_i2c_wr8(info, CLKSEL_211, 0x39);
422                 break;
423
424         case NVC_PWR_COMM:
425         case NVC_PWR_ON:
426                 err = sh532u_pm_regulator_en(info, &info->vreg_vdd);
427                 err |= sh532u_pm_regulator_en(info, &info->vreg_i2c);
428                 sh532u_gpio_en(info, 1);
429                 sh532u_gpio_reset(info, 1);
430                 err |= sh532u_i2c_wr8(info, CLKSEL_211, 0x38);
431                 err |= sh532u_i2c_wr8(info, CLKSEL_211, 0x34);
432                 err |= sh532u_i2c_wr8(info, STBY_211, 0xF0);
433                 break;
434
435         default:
436                 err = -EINVAL;
437                 break;
438         }
439
440         if (err < 0) {
441                 dev_err(&info->i2c_client->dev, "%s pwr err: %d\n",
442                                 __func__, pwr);
443                 pwr = NVC_PWR_ERR;
444         }
445         info->pwr_dev = pwr;
446         if (err > 0)
447                 return 0;
448
449         return err;
450 }
451
452 static int sh532u_pm_wr_s(struct sh532u_info *info, int pwr)
453 {
454         int err1 = 0;
455         int err2 = 0;
456
457         if ((info->s_mode == NVC_SYNC_OFF) ||
458                         (info->s_mode == NVC_SYNC_MASTER) ||
459                         (info->s_mode == NVC_SYNC_STEREO))
460                 err1 = sh532u_pm_wr(info, pwr);
461         if ((info->s_mode == NVC_SYNC_SLAVE) ||
462                         (info->s_mode == NVC_SYNC_STEREO))
463                 err2 = sh532u_pm_wr(info->s_info, pwr);
464         return err1 | err2;
465 }
466
467 static int sh532u_pm_api_wr(struct sh532u_info *info, int pwr)
468 {
469         int err = 0;
470
471         if (!pwr || (pwr > NVC_PWR_ON))
472                 return 0;
473
474         if (pwr > info->pwr_dev)
475                 err = sh532u_pm_wr_s(info, pwr);
476         if (!err)
477                 info->pwr_api = pwr;
478         else
479                 info->pwr_api = NVC_PWR_ERR;
480         if (info->pdata->cfg & NVC_CFG_NOERR)
481                 return 0;
482
483         return err;
484 }
485
486 static int sh532u_pm_dev_wr(struct sh532u_info *info, int pwr)
487 {
488         if (pwr < info->pwr_api)
489                 pwr = info->pwr_api;
490         if (info->sts == NVC_FOCUS_STS_WAIT_FOR_MOVE_END)
491                 pwr = NVC_PWR_ON;
492         return sh532u_pm_wr(info, pwr);
493 }
494
495 static void sh532u_pm_exit(struct sh532u_info *info)
496 {
497         sh532u_pm_wr(info, NVC_PWR_OFF_FORCE);
498         sh532u_pm_regulator_put(&info->vreg_vdd);
499         sh532u_pm_regulator_put(&info->vreg_i2c);
500         if (info->s_info != NULL) {
501                 sh532u_pm_wr(info->s_info, NVC_PWR_OFF_FORCE);
502                 sh532u_pm_regulator_put(&info->s_info->vreg_vdd);
503                 sh532u_pm_regulator_put(&info->s_info->vreg_i2c);
504         }
505 }
506
507 static void sh532u_pm_init(struct sh532u_info *info)
508 {
509         sh532u_pm_regulator_get(info, &info->vreg_vdd, "vdd");
510         sh532u_pm_regulator_get(info, &info->vreg_i2c, "vdd_i2c");
511 }
512
513 static int sh532u_dev_id(struct sh532u_info *info)
514 {
515         u8 val;
516         int err;
517
518         err = sh532u_i2c_rd8(info, 0, HVCA_DEVICE_ID, &val);
519         if (!err && (val == SH532U_ID))
520                 return 0;
521
522         return -ENODEV;
523 }
524
525 static void sh532u_sts_rd(struct sh532u_info *info)
526 {
527         u8 us_tmp;
528         u16 us_smv_fin;
529         int err;
530
531         if (info->sts == NVC_FOCUS_STS_INITIALIZING)
532                 return;
533
534         info->sts = NVC_FOCUS_STS_NO_DEVICE; /* assume I2C err */
535         err = sh532u_i2c_rd8(info, 0, STMVEN_211, &us_tmp);
536         err |= sh532u_i2c_rd16(info, RZ_211H, &us_smv_fin);
537         if (err)
538                 return;
539
540         /* StepMove Error Handling, Unexpected Position */
541         if ((us_smv_fin == 0x7FFF) || (us_smv_fin == 0x8001))
542                 /* Stop StepMove Operation */
543                 sh532u_i2c_wr8(info, STMVEN_211, us_tmp & 0xFE);
544         if (us_tmp & STMVEN_ON) {
545                 err = sh532u_i2c_rd8(info, 0, MSSET_211, &us_tmp);
546                 if (!err) {
547                         if (us_tmp & CHTGST_ON)
548                                 info->sts = NVC_FOCUS_STS_WAIT_FOR_SETTLE;
549                         else
550                                 info->sts = NVC_FOCUS_STS_LENS_SETTLED;
551                 }
552         } else {
553                 info->sts = NVC_FOCUS_STS_WAIT_FOR_MOVE_END;
554         }
555 }
556
557 static s16 sh532u_rel2abs(struct sh532u_info *info, u32 rel_position)
558 {
559         s16 abs_pos;
560
561         if (rel_position > info->cap.actuator_range)
562                 rel_position = info->cap.actuator_range;
563         rel_position = info->cap.actuator_range - rel_position;
564         if (rel_position) {
565                 rel_position *= info->abs_range;
566                 rel_position /= info->cap.actuator_range;
567         }
568         abs_pos = (s16)(info->abs_base + rel_position);
569         if (abs_pos < info->cfg.limit_low)
570                 abs_pos = info->cfg.limit_low;
571         if (abs_pos > info->cfg.limit_high)
572                 abs_pos = info->cfg.limit_high;
573         return abs_pos;
574 }
575
576 static u32 sh532u_abs2rel(struct sh532u_info *info, s16 abs_position)
577 {
578         u32 rel_pos;
579
580         if (abs_position > info->cfg.limit_high)
581                 abs_position = info->cfg.limit_high;
582         if (abs_position < info->abs_base)
583                 abs_position = info->abs_base;
584         rel_pos = (u32)(abs_position - info->abs_base);
585         rel_pos *= info->cap.actuator_range;
586         rel_pos /= info->abs_range;
587         if (rel_pos > info->cap.actuator_range)
588                 rel_pos = info->cap.actuator_range;
589         rel_pos = info->cap.actuator_range - rel_pos;
590         return rel_pos;
591 }
592
593 static int sh532u_abs_pos_rd(struct sh532u_info *info, s16 *position)
594 {
595         int err;
596         u16 abs_pos = 0;
597
598         err = sh532u_i2c_rd16(info, RZ_211H, &abs_pos);
599         *position = (s16)abs_pos;
600         return err;
601 }
602
603 static int sh532u_rel_pos_rd(struct sh532u_info *info, u32 *position)
604 {
605         s16 abs_pos;
606         long msec;
607         int pos;
608         int err;
609
610         err = sh532u_abs_pos_rd(info, &abs_pos);
611         if (err)
612                 return -EINVAL;
613
614         if ((abs_pos >= (info->pos_abs - STMV_SIZE)) &&
615                         (abs_pos <= (info->pos_abs + STMV_SIZE))) {
616                 pos = (int)info->pos_rel;
617         } else {
618                 msec = jiffies;
619                 msec -= info->pos_time_wr;
620                 msec = msec * 1000 / HZ;
621                 sh532u_sts_rd(info);
622                 if ((info->sts == NVC_FOCUS_STS_LENS_SETTLED) ||
623                                 (msec > info->cfg.move_timeoutms)) {
624                         pos = (int)info->pos_rel;
625                 } else {
626                         pos = (int)sh532u_abs2rel(info, abs_pos);
627                         if ((pos == (info->pos_rel - 1)) ||
628                                         (pos == (info->pos_rel + 1)))
629                                 pos = (int)info->pos_rel;
630                 }
631         }
632         if (pos < 0)
633                 pos = 0;
634         *position = (u32)pos;
635         return 0;
636 }
637
638 static int sh532u_calibration(struct sh532u_info *info, bool use_defaults)
639 {
640         u8 reg;
641         s16 abs_top;
642         u32 rel_range;
643         u32 rel_lo;
644         u32 rel_hi;
645         u32 step;
646         u32 loop_limit;
647         u32 i;
648         int err;
649         int ret = 0;
650
651         if (info->init_cal_flag)
652                 return 0;
653
654         /* set defaults */
655         memcpy(&info->cfg, &sh532u_default_info, sizeof(info->cfg));
656         memcpy(&info->nvc, &sh532u_default_nvc, sizeof(info->nvc));
657         memcpy(&info->cap, &sh532u_default_cap, sizeof(info->cap));
658         if (info->pdata->i2c_addr_rom)
659                 info->i2c_addr_rom = info->pdata->i2c_addr_rom;
660         else
661                 info->i2c_addr_rom = sh532u_default_pdata.i2c_addr_rom;
662         /* set overrides if any */
663         if (info->pdata->nvc) {
664                 if (info->pdata->nvc->fnumber)
665                         info->nvc.fnumber = info->pdata->nvc->fnumber;
666                 if (info->pdata->nvc->focal_length)
667                         info->nvc.focal_length =
668                                         info->pdata->nvc->focal_length;
669                 if (info->pdata->nvc->max_aperature)
670                         info->nvc.max_aperature =
671                                         info->pdata->nvc->max_aperature;
672         }
673         if (info->pdata->cap) {
674                 if (info->pdata->cap->actuator_range)
675                         info->cap.actuator_range =
676                                         info->pdata->cap->actuator_range;
677                 if (info->pdata->cap->settle_time)
678                         info->cap.settle_time = info->pdata->cap->settle_time;
679                 if (info->pdata->cap->focus_macro)
680                         info->cap.focus_macro = info->pdata->cap->focus_macro;
681                 if (info->pdata->cap->focus_hyper)
682                         info->cap.focus_hyper = info->pdata->cap->focus_hyper;
683                 if (info->pdata->cap->focus_infinity)
684                         info->cap.focus_infinity =
685                                         info->pdata->cap->focus_infinity;
686         }
687         /*
688          * Get Inf1, Mac1
689          * Inf1 and Mac1 are the mechanical limit position.
690          * Inf1: top limit.
691          * Mac1: bottom limit.
692          */
693         err = sh532u_i2c_rd8(info, info->i2c_addr_rom, addrMac1, &reg);
694         if (!err && (reg != 0) && (reg != 0xFF))
695                 info->cfg.limit_low = (reg<<8) & 0xff00;
696         ret = err;
697         err = sh532u_i2c_rd8(info, info->i2c_addr_rom, addrInf1, &reg);
698         if (!err && (reg != 0) && (reg != 0xFF))
699                 info->cfg.limit_high = (reg<<8) & 0xff00;
700         ret |= err;
701         /*
702          * Get Inf2, Mac2
703          * Inf2 and Mac2 are the calibration data for SEMCO AF lens.
704          * Inf2: Best focus (lens position) when object distance is 1.2M.
705          * Mac2: Best focus (lens position) when object distance is 10cm.
706          */
707         err = sh532u_i2c_rd8(info, info->i2c_addr_rom, addrMac2, &reg);
708         if (!err && (reg != 0) && (reg != 0xFF))
709                 info->cfg.pos_low = (reg << 8) & 0xff00;
710         ret |= err;
711         err = sh532u_i2c_rd8(info, info->i2c_addr_rom, addrInf2, &reg);
712         if (!err && (reg != 0) && (reg != 0xFF))
713                 info->cfg.pos_high = (reg << 8) & 0xff00;
714         ret |= err;
715         /* set overrides */
716         if (info->pdata->info) {
717                 if (info->pdata->info->pos_low)
718                         info->cfg.pos_low = info->pdata->info->pos_low;
719                 if (info->pdata->info->pos_high)
720                         info->cfg.pos_high = info->pdata->info->pos_high;
721                 if (info->pdata->info->limit_low)
722                         info->cfg.limit_low = info->pdata->info->limit_low;
723                 if (info->pdata->info->limit_high)
724                         info->cfg.limit_high = info->pdata->info->limit_high;
725                 if (info->pdata->info->move_timeoutms)
726                         info->cfg.move_timeoutms =
727                                         info->pdata->info->move_timeoutms;
728                 if (info->pdata->info->focus_hyper_ratio)
729                         info->cfg.focus_hyper_ratio =
730                                         info->pdata->info->focus_hyper_ratio;
731                 if (info->pdata->info->focus_hyper_div)
732                         info->cfg.focus_hyper_div =
733                                         info->pdata->info->focus_hyper_div;
734         }
735         /*
736          * There is known to be many sh532u devices with no EPROM data.
737          * Using default data is known to reduce the sh532u performance since
738          * the defaults may no where be close to the correct values that
739          * should be used.  However, we don't want to prevent the camera from
740          * starting due to the lack of the EPROM data.
741          * The following truth table shows the action to take at this point:
742          * DFLT = the use_defaults flag (used after multiple attempts)
743          * I2C = the I2C transactions to get the data.
744          * DATA = the needed data either from the EPROM or board file.
745          * DFLT   I2C   DATA   Action
746          * --------------------------
747          *  0     FAIL  FAIL   Exit with -EIO
748          *  0     FAIL  PASS   Continue to calculations
749          *  0     PASS  FAIL   Use defaults
750          *  0     PASS  PASS   Continue to calculations
751          *  1     FAIL  FAIL   Use defaults
752          *  1     FAIL  PASS   Continue to calculations
753          *  1     PASS  FAIL   Use defaults
754          *  1     PASS  PASS   Continue to calculations
755          */
756         /* err = DATA where FAIL = 1 */
757         if (!info->cfg.pos_low || !info->cfg.pos_high ||
758                         !info->cfg.limit_low || !info->cfg.limit_high)
759                 err = 1;
760         else
761                 err = 0;
762         /* Exit with -EIO */
763         if (!use_defaults && ret && err) {
764                 dev_err(&info->i2c_client->dev, "%s ERR\n", __func__);
765                 return -EIO;
766         }
767
768         /* Use defaults */
769         if (err) {
770                 info->cfg.pos_low = SH532U_POS_LOW_DEFAULT;
771                 info->cfg.pos_high = SH532U_POS_HIGH_DEFAULT;
772                 info->cfg.limit_low = SH532U_POS_LOW_DEFAULT;
773                 info->cfg.limit_high = SH532U_POS_HIGH_DEFAULT;
774                 dev_err(&info->i2c_client->dev, "%s ERR: ERPOM data is void!  "
775                             "Focuser will use defaults that will cause "
776                             "reduced functionality!\n", __func__);
777         }
778         if (info->cfg.pos_low < info->cfg.limit_low)
779                 info->cfg.pos_low = info->cfg.limit_low;
780         if (info->cfg.pos_high > info->cfg.limit_high)
781                 info->cfg.pos_high = info->cfg.limit_high;
782         dev_dbg(&info->i2c_client->dev, "%s pos_low=%d\n", __func__,
783                                 (int)info->cfg.pos_low);
784         dev_dbg(&info->i2c_client->dev, "%s pos_high=%d\n", __func__,
785                                 (int)info->cfg.pos_high);
786         dev_dbg(&info->i2c_client->dev, "%s limit_low=%d\n", __func__,
787                                 (int)info->cfg.limit_low);
788         dev_dbg(&info->i2c_client->dev, "%s limit_high=%d\n", __func__,
789                                 (int)info->cfg.limit_high);
790         /*
791          * calculate relative and absolute positions
792          * Note that relative values, what upper SW uses, are the
793          * abstraction of HW (absolute) values.
794          * |<--limit_low                                  limit_high-->|
795          * | |<-------------------_ACTUATOR_RANGE------------------->| |
796          *              -focus_inf                        -focus_mac
797          *   |<---RI--->|                                 |<---RM--->|
798          *   -abs_base  -pos_low                          -pos_high  -abs_top
799          *
800          * The pos_low and pos_high are fixed absolute positions and correspond
801          * to the relative focus_infinity and focus_macro, respectively.  We'd
802          * like to have "wiggle" room (RI and RM) around these relative
803          * positions so the loop below finds the best fit for RI and RM without
804          * passing the absolute limits.
805          * We want our _ACTUATOR_RANGE to be infinity on the 0 end and macro
806          * on the max end.  However, the focuser HW is opposite this.
807          * Therefore we use the rel(ative)_lo/hi variables in the calculation
808          * loop and assign them the focus_infinity and focus_macro values.
809          */
810         rel_lo = (info->cap.actuator_range - info->cap.focus_macro);
811         rel_hi = info->cap.focus_infinity;
812         info->abs_range = (u32)(info->cfg.pos_high - info->cfg.pos_low);
813         loop_limit = (rel_lo > rel_hi) ? rel_lo : rel_hi;
814         for (i = 0; i <= loop_limit; i++) {
815                 rel_range = info->cap.actuator_range - (rel_lo + rel_hi);
816                 step = info->abs_range / rel_range;
817                 info->abs_base = info->cfg.pos_low - (step * rel_lo);
818                 abs_top = info->cfg.pos_high + (step * rel_hi);
819                 if (info->abs_base < info->cfg.limit_low) {
820                         if (rel_lo > 0)
821                                 rel_lo--;
822                 }
823                 if (abs_top > info->cfg.limit_high) {
824                         if (rel_hi > 0)
825                                 rel_hi--;
826                 }
827                 if (info->abs_base >= info->cfg.limit_low &&
828                                         abs_top <= info->cfg.limit_high)
829                         break;
830         }
831         info->cap.focus_hyper = info->abs_range;
832         info->abs_range = (u32)(abs_top - info->abs_base);
833         /* calculate absolute hyperfocus position */
834         info->cap.focus_hyper *= info->cfg.focus_hyper_ratio;
835         info->cap.focus_hyper /= info->cfg.focus_hyper_div;
836         abs_top = (s16)(info->cfg.pos_high - info->cap.focus_hyper);
837         /* update actual relative positions */
838         info->cap.focus_hyper = sh532u_abs2rel(info, abs_top);
839         info->cap.focus_infinity = sh532u_abs2rel(info, info->cfg.pos_high);
840         info->cap.focus_macro = sh532u_abs2rel(info, info->cfg.pos_low);
841         dev_dbg(&info->i2c_client->dev, "%s focus_macro=%u\n", __func__,
842                                         info->cap.focus_macro);
843         dev_dbg(&info->i2c_client->dev, "%s focus_infinity=%u\n", __func__,
844                                         info->cap.focus_infinity);
845         dev_dbg(&info->i2c_client->dev, "%s focus_hyper=%u\n", __func__,
846                                         info->cap.focus_hyper);
847         info->init_cal_flag = 1;
848         dev_dbg(&info->i2c_client->dev, "%s complete\n", __func__);
849         return 0;
850 }
851
852         /* Write 1 byte data to the HVCA Drive IC by data type */
853 static int sh532u_hvca_wr1(struct sh532u_info *info,
854                            u8 ep_type, u8 ep_data1, u8 ep_addr)
855 {
856         u8 us_data;
857         int err = 0;
858
859         switch (ep_type & 0xF0) {
860         case DIRECT_MODE:
861                 us_data = ep_data1;
862                 break;
863
864         case INDIRECT_EEPROM:
865                 err = sh532u_i2c_rd8(info,
866                                      info->i2c_addr_rom,
867                                      ep_data1,
868                                      &us_data);
869                 break;
870
871         case INDIRECT_HVCA:
872                 err = sh532u_i2c_rd8(info, 0, ep_data1, &us_data);
873                 break;
874
875         case MASK_AND:
876                 err = sh532u_i2c_rd8(info, 0, ep_addr, &us_data);
877                 us_data &= ep_data1;
878                 break;
879
880         case MASK_OR:
881                 err = sh532u_i2c_rd8(info, 0, ep_addr, &us_data);
882                 us_data |= ep_data1;
883                 break;
884
885         default:
886                 err = -EINVAL;
887         }
888         if (!err)
889                 err = sh532u_i2c_wr8(info, ep_addr, us_data);
890         return err;
891 }
892
893         /* Write 2 byte data to the HVCA Drive IC by data type */
894 static int sh532u_hvca_wr2(struct sh532u_info *info, u8 ep_type,
895                                 u8 ep_data1, u8 ep_data2, u8 ep_addr)
896 {
897         u8 uc_data1;
898         u8 uc_data2;
899         u16 us_data;
900         int err = 0;
901
902         switch (ep_type & 0xF0) {
903         case DIRECT_MODE:
904                 us_data = (((u16)ep_data1 << 8) & 0xFF00) |
905                         ((u16)ep_data2 & 0x00FF);
906                 break;
907
908         case INDIRECT_EEPROM:
909                 err = sh532u_i2c_rd8(info,
910                                      info->i2c_addr_rom,
911                                      ep_data1,
912                                      &uc_data1);
913                 err |= sh532u_i2c_rd8(info,
914                                       info->i2c_addr_rom,
915                                       ep_data2,
916                                       &uc_data2);
917                 us_data = (((u16)uc_data1 << 8) & 0xFF00) |
918                                 ((u16)uc_data2 & 0x00FF);
919                 break;
920
921         case INDIRECT_HVCA:
922                 err = sh532u_i2c_rd8(info, 0, ep_data1, &uc_data1);
923                 err |= sh532u_i2c_rd8(info, 0, ep_data2, &uc_data2);
924                 us_data = (((u16)uc_data1 << 8) & 0xFF00) |
925                                 ((u16)uc_data2 & 0x00FF);
926                 break;
927
928         case MASK_AND:
929                 err = sh532u_i2c_rd16(info, ep_addr, &us_data);
930                 us_data &= ((((u16)ep_data1 << 8) & 0xFF00) |
931                             ((u16)ep_data2 & 0x00FF));
932                 break;
933
934         case MASK_OR:
935                 err = sh532u_i2c_rd16(info, ep_addr, &us_data);
936                 us_data |= ((((u16)ep_data1 << 8) & 0xFF00) |
937                             ((u16)ep_data2 & 0x00FF));
938                 break;
939
940         default:
941                 err = -EINVAL;
942         }
943         if (!err)
944                 err = sh532u_i2c_wr16(info, ep_addr, us_data);
945         return err;
946 }
947
948 static int sh532u_dev_init(struct sh532u_info *info)
949 {
950         int eeprom_reg;
951         unsigned eeprom_data = 0;
952         u8 ep_addr;
953         u8 ep_type;
954         u8 ep_data1;
955         u8 ep_data2;
956         int err;
957         int ret = 0;
958
959         err = sh532u_i2c_rd8(info, 0, SWTCH_211, &ep_data1);
960         ep_data2 = ep_data1;
961         err |= sh532u_i2c_rd8(info, 0, ANA1_211, &ep_data1);
962         ep_data2 |= ep_data1;
963         if (!err && ep_data2)
964                 return 0; /* Already initialized */
965
966         info->sts = NVC_FOCUS_STS_INITIALIZING;
967         for (eeprom_reg = 0x30; eeprom_reg <= 0x013C; eeprom_reg += 4) {
968                 if (eeprom_reg > 0xFF) {
969                         /* use hardcoded data instead */
970                         eeprom_data = sh532u_a2buf[(eeprom_reg & 0xFF) / 4];
971                 } else {
972                         err = (sh532u_i2c_rd32(info,
973                                             info->i2c_addr_rom,
974                                             eeprom_reg & 0xFF,
975                                             &eeprom_data));
976                         if (err) {
977                                 ret |= err;
978                                 continue;
979                         }
980                 }
981
982                 /* HVCA Address to write eeprom Data1,Data2 by the Data type */
983                 ep_addr = (u8)(eeprom_data & 0x000000ff);
984                 ep_type = (u8)((eeprom_data & 0x0000ff00) >> 8);
985                 ep_data1 = (u8)((eeprom_data & 0x00ff0000) >> 16);
986                 ep_data2 = (u8)((eeprom_data & 0xff000000) >> 24);
987                 if (ep_addr == 0xFF)
988                         break;
989
990                 if (ep_addr == 0xDD) {
991                         mdelay((unsigned int)((ep_data1 << 8) | ep_data2));
992                 } else {
993                         if ((ep_type & 0x0F) == DATA_1BYTE) {
994                                 err = sh532u_hvca_wr1(info,
995                                                       ep_type,
996                                                       ep_data1,
997                                                       ep_addr);
998                         } else {
999                                 err = sh532u_hvca_wr2(info,
1000                                                       ep_type,
1001                                                       ep_data1,
1002                                                       ep_data2,
1003                                                       ep_addr);
1004                         }
1005                 }
1006                 ret |= err;
1007         }
1008
1009         err = ret;
1010         if (err)
1011                 dev_err(&info->i2c_client->dev, "%s programming err=%d\n",
1012                                 __func__, err);
1013         err |= sh532u_calibration(info, false);
1014         info->sts = NVC_FOCUS_STS_LENS_SETTLED;
1015         return err;
1016 }
1017
1018 static int sh532u_pos_abs_wr(struct sh532u_info *info, s16 tar_pos)
1019 {
1020         s16 cur_pos;
1021         s16 move_step;
1022         u16 move_distance;
1023         int err;
1024
1025         sh532u_pm_dev_wr(info, NVC_PWR_ON);
1026         err = sh532u_dev_init(info);
1027         if (err)
1028                 return err;
1029
1030         /* Read Current Position */
1031         err = sh532u_abs_pos_rd(info, &cur_pos);
1032         if (err)
1033                 return err;
1034
1035         dev_dbg(&info->i2c_client->dev, "%s cur_pos=%d tar_pos=%d\n",
1036                         __func__, (int)cur_pos, (int)tar_pos);
1037         info->sts = NVC_FOCUS_STS_WAIT_FOR_MOVE_END;
1038         /* Check move distance to Target Position */
1039         move_distance = abs((int)cur_pos - (int)tar_pos);
1040         /* if move distance is shorter than MS1Z12(=Step width) */
1041         if (move_distance <= STMV_SIZE) {
1042                 err = sh532u_i2c_wr8(info, MSSET_211,
1043                                      (INI_MSSET_211 | 0x01));
1044                 err |= sh532u_i2c_wr16(info, MS1Z22_211H, tar_pos);
1045         } else {
1046                 if (cur_pos < tar_pos)
1047                         move_step = STMV_SIZE;
1048                 else
1049                         move_step = -STMV_SIZE;
1050                 /* Set StepMove Target Positon */
1051                 err = sh532u_i2c_wr16(info, MS1Z12_211H, move_step);
1052                 err |= sh532u_i2c_wr16(info, STMVENDH_211, tar_pos);
1053                 /* Start StepMove */
1054                 err |= sh532u_i2c_wr8(info, STMVEN_211,
1055                                       (STMCHTG_ON |
1056                                        STMSV_ON |
1057                                        STMLFF_OFF |
1058                                        STMVEN_ON));
1059         }
1060         return err;
1061 }
1062
1063 static int sh532u_move_wait(struct sh532u_info *info)
1064 {
1065         u16 us_smv_fin;
1066         u8 moveTime;
1067         u8 ucParMod;
1068         u8 tmp;
1069         int err;
1070
1071         moveTime = 0;
1072         do {
1073                 mdelay(1);
1074                 err = sh532u_i2c_rd8(info, 0, STMVEN_211, &ucParMod);
1075                 err |= sh532u_i2c_rd16(info, RZ_211H, &us_smv_fin);
1076                 if (err)
1077                         return err;
1078
1079                 /* StepMove Error Handling, Unexpected Position */
1080                 if ((us_smv_fin == 0x7FFF) || (us_smv_fin == 0x8001)) {
1081                         /* Stop StepMove Operation */
1082                         err = sh532u_i2c_wr8(info, STMVEN_211,
1083                                              ucParMod & 0xFE);
1084                         if (err)
1085                                 return err;
1086                 }
1087
1088                 moveTime++;
1089                 /* Wait StepMove operation end */
1090         } while ((ucParMod & STMVEN_ON) && (moveTime < 50));
1091
1092         moveTime = 0;
1093         if ((ucParMod & 0x08) == STMCHTG_ON) {
1094                 mdelay(5);
1095                 do {
1096                         mdelay(1);
1097                         moveTime++;
1098                         err = sh532u_i2c_rd8(info, 0, MSSET_211, &tmp);
1099                         if (err)
1100                                 return err;
1101
1102                 } while ((tmp & CHTGST_ON) && (moveTime < 15));
1103         }
1104         return err;
1105 }
1106
1107 static int sh532u_move_pulse(struct sh532u_info *info, s16 position)
1108 {
1109         int err;
1110
1111         err = sh532u_pos_abs_wr(info, position);
1112         err |= sh532u_move_wait(info);
1113         return err;
1114 }
1115
1116 static int sh532u_hvca_pos_init(struct sh532u_info *info)
1117 {
1118         s16 limit_bottom;
1119         s16 limit_top;
1120         int err;
1121
1122         limit_bottom = (((int)info->cfg.limit_low * 5) >> 3) & 0xFFC0;
1123         if (limit_bottom < info->cfg.limit_low)
1124                 limit_bottom = info->cfg.limit_low;
1125         limit_top = (((int)info->cfg.limit_high * 5) >> 3) & 0xFFC0;
1126         if (limit_top > info->cfg.limit_high)
1127                 limit_top = info->cfg.limit_high;
1128         err = sh532u_move_pulse(info, limit_bottom);
1129         err |= sh532u_move_pulse(info, limit_top);
1130         err |= sh532u_move_pulse(info, info->cfg.pos_high);
1131         return err;
1132 }
1133
1134 static int sh532u_pos_rel_wr(struct sh532u_info *info, u32 position)
1135 {
1136         s16 abs_pos;
1137
1138         if (position > info->cap.actuator_range) {
1139                 dev_err(&info->i2c_client->dev, "%s invalid position %u\n",
1140                                 __func__, position);
1141                 return -EINVAL;
1142         }
1143
1144         abs_pos = sh532u_rel2abs(info, position);
1145         info->pos_rel = position;
1146         info->pos_abs = abs_pos;
1147         info->pos_time_wr = jiffies;
1148         return sh532u_pos_abs_wr(info, abs_pos);
1149 }
1150
1151
1152 static int sh532u_param_rd(struct sh532u_info *info, unsigned long arg)
1153 {
1154         struct nvc_param params;
1155         const void *data_ptr;
1156         u32 data_size = 0;
1157         u32 position;
1158         int err;
1159
1160         if (copy_from_user(&params,
1161                         (const void __user *)arg,
1162                         sizeof(struct nvc_param))) {
1163                 dev_err(&info->i2c_client->dev, "%s %d copy_from_user err\n",
1164                                 __func__, __LINE__);
1165                 return -EFAULT;
1166         }
1167
1168         if (info->s_mode == NVC_SYNC_SLAVE)
1169                 info = info->s_info;
1170         switch (params.param) {
1171         case NVC_PARAM_LOCUS:
1172                 sh532u_pm_dev_wr(info, NVC_PWR_COMM);
1173                 err = sh532u_rel_pos_rd(info, &position);
1174                 if (err && !(info->pdata->cfg & NVC_CFG_NOERR))
1175                         return -EINVAL;
1176
1177                 data_ptr = &position;
1178                 data_size = sizeof(position);
1179                 sh532u_pm_dev_wr(info, NVC_PWR_STDBY);
1180                 dev_dbg(&info->i2c_client->dev, "%s LOCUS: %d\n",
1181                                 __func__, position);
1182                 break;
1183
1184         case NVC_PARAM_FOCAL_LEN:
1185                 data_ptr = &info->nvc.focal_length;
1186                 data_size = sizeof(info->nvc.focal_length);
1187                 dev_dbg(&info->i2c_client->dev, "%s FOCAL_LEN: %x\n",
1188                                 __func__, info->nvc.focal_length);
1189                 break;
1190
1191         case NVC_PARAM_MAX_APERTURE:
1192                 data_ptr = &info->nvc.max_aperature;
1193                 data_size = sizeof(info->nvc.max_aperature);
1194                 dev_dbg(&info->i2c_client->dev, "%s MAX_APERTURE: %x\n",
1195                                 __func__, info->nvc.max_aperature);
1196                 break;
1197
1198         case NVC_PARAM_FNUMBER:
1199                 data_ptr = &info->nvc.fnumber;
1200                 data_size = sizeof(info->nvc.fnumber);
1201                 dev_dbg(&info->i2c_client->dev, "%s FNUMBER: %x\n",
1202                                 __func__, info->nvc.fnumber);
1203                 break;
1204
1205         case NVC_PARAM_CAPS:
1206                 sh532u_pm_dev_wr(info, NVC_PWR_COMM);
1207                 err = sh532u_calibration(info, true);
1208                 sh532u_pm_dev_wr(info, NVC_PWR_STDBY);
1209                 if (err)
1210                         return -EIO;
1211
1212                 data_ptr = &info->cap;
1213                 /* there are different sizes depending on the version */
1214                 /* send back just what's requested or our max size */
1215                 if (params.sizeofvalue < sizeof(info->cap))
1216                         data_size = params.sizeofvalue;
1217                 else
1218                         data_size = sizeof(info->cap);
1219                 dev_dbg(&info->i2c_client->dev, "%s CAPS\n",
1220                                 __func__);
1221                 break;
1222
1223         case NVC_PARAM_STS:
1224                 data_ptr = &info->sts;
1225                 data_size = sizeof(info->sts);
1226                 dev_dbg(&info->i2c_client->dev, "%s STS: %d\n",
1227                                 __func__, info->sts);
1228                 break;
1229
1230         case NVC_PARAM_STEREO:
1231                 data_ptr = &info->s_mode;
1232                 data_size = sizeof(info->s_mode);
1233                 dev_dbg(&info->i2c_client->dev, "%s STEREO: %d\n",
1234                                 __func__, info->s_mode);
1235                 break;
1236
1237         default:
1238                 dev_err(&info->i2c_client->dev,
1239                                 "%s unsupported parameter: %d\n",
1240                                 __func__, params.param);
1241                 return -EINVAL;
1242         }
1243
1244         if (params.sizeofvalue < data_size) {
1245                 dev_err(&info->i2c_client->dev, "%s %d data size err\n",
1246                                 __func__, __LINE__);
1247                 return -EINVAL;
1248         }
1249
1250         if (copy_to_user((void __user *)params.p_value,
1251                          data_ptr,
1252                          data_size)) {
1253                 dev_err(&info->i2c_client->dev, "%s %d copy_to_user err\n",
1254                                 __func__, __LINE__);
1255                 return -EFAULT;
1256         }
1257
1258         return 0;
1259 }
1260
1261 static int sh532u_param_wr_s(struct sh532u_info *info,
1262                              struct nvc_param *params,
1263                              u32 u32_val)
1264 {
1265         int err;
1266
1267         switch (params->param) {
1268         case NVC_PARAM_LOCUS:
1269                 dev_dbg(&info->i2c_client->dev, "%s LOCUS: %u\n",
1270                                 __func__, u32_val);
1271                 err = sh532u_pos_rel_wr(info, u32_val);
1272                 return err;
1273
1274         case NVC_PARAM_RESET:
1275                 err = sh532u_pm_wr(info, NVC_PWR_OFF);
1276                 err |= sh532u_pm_wr(info, NVC_PWR_ON);
1277                 err |= sh532u_pm_wr(info, info->pwr_api);
1278                 dev_dbg(&info->i2c_client->dev, "%s RESET: %d\n",
1279                                 __func__, err);
1280                 return err;
1281
1282         case NVC_PARAM_SELF_TEST:
1283                 err = sh532u_hvca_pos_init(info);
1284                 dev_dbg(&info->i2c_client->dev, "%s SELF_TEST: %d\n",
1285                                 __func__, err);
1286                 return err;
1287
1288         default:
1289                 dev_err(&info->i2c_client->dev,
1290                                 "%s unsupported parameter: %d\n",
1291                                 __func__, params->param);
1292                 return -EINVAL;
1293         }
1294 }
1295
1296 static int sh532u_param_wr(struct sh532u_info *info, unsigned long arg)
1297 {
1298         struct nvc_param params;
1299         u8 val;
1300         u32 u32_val;
1301         int err = 0;
1302
1303         if (copy_from_user(&params,
1304                                 (const void __user *)arg,
1305                                 sizeof(struct nvc_param))) {
1306                 dev_err(&info->i2c_client->dev, "%s %d copy_from_user err\n",
1307                                 __func__, __LINE__);
1308                 return -EFAULT;
1309         }
1310
1311         if (copy_from_user(&u32_val, (const void __user *)params.p_value,
1312                            sizeof(u32_val))) {
1313                 dev_err(&info->i2c_client->dev, "%s %d copy_from_user err\n",
1314                                 __func__, __LINE__);
1315                 return -EFAULT;
1316         }
1317
1318         /* parameters independent of sync mode */
1319         switch (params.param) {
1320         case NVC_PARAM_STEREO:
1321                 dev_dbg(&info->i2c_client->dev, "%s STEREO: %u\n",
1322                                 __func__, u32_val);
1323                 val = (u8)u32_val;
1324                 if (val == info->s_mode)
1325                         return 0;
1326
1327                 switch (val) {
1328                 case NVC_SYNC_OFF:
1329                         info->s_mode = val;
1330                         if (info->s_info != NULL) {
1331                                 info->s_info->s_mode = val;
1332                                 sh532u_pm_wr(info->s_info, NVC_PWR_OFF);
1333                         }
1334                         break;
1335
1336                 case NVC_SYNC_MASTER:
1337                         info->s_mode = val;
1338                         if (info->s_info != NULL)
1339                                 info->s_info->s_mode = val;
1340                         break;
1341
1342                 case NVC_SYNC_SLAVE:
1343                         if (info->s_info != NULL) {
1344                                 /* default slave lens position */
1345                                 err = sh532u_pos_rel_wr(info->s_info,
1346                                              info->s_info->cap.focus_infinity);
1347                                 if (!err) {
1348                                         info->s_mode = val;
1349                                         info->s_info->s_mode = val;
1350                                 } else {
1351                                         if (info->s_mode != NVC_SYNC_STEREO)
1352                                                 sh532u_pm_wr(info->s_info,
1353                                                              NVC_PWR_OFF);
1354                                         err = -EIO;
1355                                 }
1356                         } else {
1357                                 err = -EINVAL;
1358                         }
1359                         break;
1360
1361                 case NVC_SYNC_STEREO:
1362                         if (info->s_info != NULL) {
1363                                 /* sync power */
1364                                 info->s_info->pwr_api = info->pwr_api;
1365                                 /* move slave lens to master position */
1366                                 err = sh532u_pos_rel_wr(info->s_info,
1367                                                          info->pos_rel);
1368                                 if (!err) {
1369                                         info->s_mode = val;
1370                                         info->s_info->s_mode = val;
1371                                 } else {
1372                                         if (info->s_mode != NVC_SYNC_SLAVE)
1373                                                 sh532u_pm_wr(info->s_info,
1374                                                              NVC_PWR_OFF);
1375                                         err = -EIO;
1376                                 }
1377                         } else {
1378                                 err = -EINVAL;
1379                         }
1380                         break;
1381
1382                 default:
1383                         err = -EINVAL;
1384                 }
1385                 if (info->pdata->cfg & NVC_CFG_NOERR)
1386                         return 0;
1387
1388                 return err;
1389
1390         default:
1391         /* parameters dependent on sync mode */
1392                 switch (info->s_mode) {
1393                 case NVC_SYNC_OFF:
1394                 case NVC_SYNC_MASTER:
1395                         return sh532u_param_wr_s(info, &params, u32_val);
1396
1397                 case NVC_SYNC_SLAVE:
1398                         return sh532u_param_wr_s(info->s_info,
1399                                                  &params,
1400                                                  u32_val);
1401
1402                 case NVC_SYNC_STEREO:
1403                         err = sh532u_param_wr_s(info, &params, u32_val);
1404                         if (!(info->pdata->cfg & NVC_CFG_SYNC_I2C_MUX))
1405                                 err |= sh532u_param_wr_s(info->s_info,
1406                                                          &params,
1407                                                          u32_val);
1408                         return err;
1409
1410                 default:
1411                         dev_err(&info->i2c_client->dev, "%s %d internal err\n",
1412                                         __func__, __LINE__);
1413                         return -EINVAL;
1414                 }
1415         }
1416 }
1417
1418 static long sh532u_ioctl(struct file *file,
1419                 unsigned int cmd,
1420                 unsigned long arg)
1421 {
1422         struct sh532u_info *info = file->private_data;
1423         int pwr;
1424
1425         switch (cmd) {
1426         case NVC_IOCTL_PARAM_WR:
1427                 return sh532u_param_wr(info, arg);
1428
1429         case NVC_IOCTL_PARAM_RD:
1430                 return sh532u_param_rd(info, arg);
1431
1432         case NVC_IOCTL_PWR_WR:
1433                 /* This is a Guaranteed Level of Service (GLOS) call */
1434                 pwr = (int)arg * 2;
1435                 dev_dbg(&info->i2c_client->dev, "%s PWR: %d\n",
1436                                 __func__, pwr);
1437                 return sh532u_pm_api_wr(info, pwr);
1438
1439         case NVC_IOCTL_PWR_RD:
1440                 if (info->s_mode == NVC_SYNC_SLAVE)
1441                         pwr = info->s_info->pwr_api / 2;
1442                 else
1443                         pwr = info->pwr_api / 2;
1444                 dev_dbg(&info->i2c_client->dev, "%s PWR_RD: %d\n",
1445                                 __func__, pwr);
1446                 if (copy_to_user((void __user *)arg, (const void *)&pwr,
1447                                  sizeof(pwr))) {
1448                         dev_err(&info->i2c_client->dev,
1449                                         "%s copy_to_user err line %d\n",
1450                                         __func__, __LINE__);
1451                         return -EFAULT;
1452                 }
1453
1454                 return 0;
1455
1456         default:
1457                 dev_err(&info->i2c_client->dev, "%s unsupported ioctl: %x\n",
1458                                 __func__, cmd);
1459                 return -EINVAL;
1460         }
1461 }
1462
1463 static int sh532u_sync_en(int dev1, int dev2)
1464 {
1465         struct sh532u_info *sync1 = NULL;
1466         struct sh532u_info *sync2 = NULL;
1467         struct sh532u_info *pos = NULL;
1468
1469         rcu_read_lock();
1470         list_for_each_entry_rcu(pos, &sh532u_info_list, list) {
1471                 if (pos->pdata->num == dev1) {
1472                         sync1 = pos;
1473                         break;
1474                 }
1475         }
1476         pos = NULL;
1477         list_for_each_entry_rcu(pos, &sh532u_info_list, list) {
1478                 if (pos->pdata->num == dev2) {
1479                         sync2 = pos;
1480                         break;
1481                 }
1482         }
1483         rcu_read_unlock();
1484         if (sync1 != NULL)
1485                 sync1->s_info = NULL;
1486         if (sync2 != NULL)
1487                 sync2->s_info = NULL;
1488         if (!dev1 && !dev2)
1489                 return 0; /* no err if default instance 0's used */
1490
1491         if (dev1 == dev2)
1492                 return -EINVAL; /* err if sync instance is itself */
1493
1494         if ((sync1 != NULL) && (sync2 != NULL)) {
1495                 sync1->s_info = sync2;
1496                 sync2->s_info = sync1;
1497         }
1498         return 0;
1499 }
1500
1501 static int sh532u_sync_dis(struct sh532u_info *info)
1502 {
1503         if (info->s_info != NULL) {
1504                 info->s_info->s_mode = 0;
1505                 info->s_info->s_info = NULL;
1506                 info->s_mode = 0;
1507                 info->s_info = NULL;
1508                 return 0;
1509         }
1510
1511         return -EINVAL;
1512 }
1513
1514 static int sh532u_open(struct inode *inode, struct file *file)
1515 {
1516         struct sh532u_info *info = NULL;
1517         struct sh532u_info *pos = NULL;
1518         int err;
1519
1520         rcu_read_lock();
1521         list_for_each_entry_rcu(pos, &sh532u_info_list, list) {
1522                 if (pos->miscdev.minor == iminor(inode)) {
1523                         info = pos;
1524                         break;
1525                 }
1526         }
1527         rcu_read_unlock();
1528         if (!info)
1529                 return -ENODEV;
1530
1531         err = sh532u_sync_en(info->pdata->num, info->pdata->sync);
1532         if (err == -EINVAL)
1533                 dev_err(&info->i2c_client->dev,
1534                          "%s err: invalid num (%u) and sync (%u) instance\n",
1535                          __func__, info->pdata->num, info->pdata->sync);
1536         if (atomic_xchg(&info->in_use, 1))
1537                 return -EBUSY;
1538
1539         if (info->s_info != NULL) {
1540                 if (atomic_xchg(&info->s_info->in_use, 1))
1541                         return -EBUSY;
1542         }
1543
1544         file->private_data = info;
1545         dev_dbg(&info->i2c_client->dev, "%s\n", __func__);
1546         sh532u_pos_rel_wr(info, info->cap.focus_infinity);
1547         return 0;
1548 }
1549
1550 int sh532u_release(struct inode *inode, struct file *file)
1551 {
1552         struct sh532u_info *info = file->private_data;
1553
1554         dev_dbg(&info->i2c_client->dev, "%s\n", __func__);
1555         sh532u_pm_wr_s(info, NVC_PWR_OFF);
1556         file->private_data = NULL;
1557         WARN_ON(!atomic_xchg(&info->in_use, 0));
1558         if (info->s_info != NULL)
1559                 WARN_ON(!atomic_xchg(&info->s_info->in_use, 0));
1560         sh532u_sync_dis(info);
1561         return 0;
1562 }
1563
1564 static const struct file_operations sh532u_fileops = {
1565         .owner = THIS_MODULE,
1566         .open = sh532u_open,
1567         .unlocked_ioctl = sh532u_ioctl,
1568         .release = sh532u_release,
1569 };
1570
1571 static void sh532u_del(struct sh532u_info *info)
1572 {
1573         sh532u_pm_exit(info);
1574         sh532u_sync_dis(info);
1575         spin_lock(&sh532u_spinlock);
1576         list_del_rcu(&info->list);
1577         spin_unlock(&sh532u_spinlock);
1578         synchronize_rcu();
1579 }
1580
1581 static int sh532u_remove(struct i2c_client *client)
1582 {
1583         struct sh532u_info *info = i2c_get_clientdata(client);
1584
1585         dev_dbg(&info->i2c_client->dev, "%s\n", __func__);
1586         misc_deregister(&info->miscdev);
1587         sh532u_del(info);
1588         return 0;
1589 }
1590
1591 static int sh532u_probe(
1592         struct i2c_client *client,
1593         const struct i2c_device_id *id)
1594 {
1595         struct sh532u_info *info = NULL;
1596         char dname[16];
1597         int err;
1598
1599         dev_dbg(&client->dev, "%s\n", __func__);
1600         info = devm_kzalloc(&client->dev, sizeof(*info), GFP_KERNEL);
1601         if (info == NULL) {
1602                 dev_err(&client->dev, "%s: kzalloc error\n", __func__);
1603                 return -ENOMEM;
1604         }
1605
1606         info->i2c_client = client;
1607         if (client->dev.platform_data) {
1608                 info->pdata = client->dev.platform_data;
1609         } else {
1610                 info->pdata = &sh532u_default_pdata;
1611                 dev_dbg(&client->dev,
1612                                 "%s No platform data.  Using defaults.\n",
1613                                 __func__);
1614         }
1615         i2c_set_clientdata(client, info);
1616         INIT_LIST_HEAD(&info->list);
1617         spin_lock(&sh532u_spinlock);
1618         list_add_rcu(&info->list, &sh532u_info_list);
1619         spin_unlock(&sh532u_spinlock);
1620         sh532u_pm_init(info);
1621         sh532u_pm_dev_wr(info, NVC_PWR_COMM);
1622         err = sh532u_dev_id(info);
1623         if (err < 0) {
1624                 dev_err(&client->dev, "%s device not found\n", __func__);
1625                 sh532u_pm_wr(info, NVC_PWR_OFF);
1626                 if (info->pdata->cfg & NVC_CFG_NODEV) {
1627                         sh532u_del(info);
1628                         return -ENODEV;
1629                 }
1630         } else {
1631                 dev_dbg(&client->dev, "%s device found\n", __func__);
1632                 sh532u_calibration(info, false);
1633                 if (info->pdata->cfg & NVC_CFG_BOOT_INIT) {
1634                         /* initial move causes full initialization */
1635                         sh532u_pos_rel_wr(info, info->cap.focus_infinity);
1636                 } else {
1637                         sh532u_pm_wr(info, NVC_PWR_OFF);
1638                 }
1639         }
1640
1641         if (info->pdata->dev_name != 0)
1642                 strcpy(dname, info->pdata->dev_name);
1643         else
1644                 strcpy(dname, "sh532u");
1645         if (info->pdata->num)
1646                 snprintf(dname, sizeof(dname), "%s.%u",
1647                          dname, info->pdata->num);
1648         info->miscdev.name = dname;
1649         info->miscdev.fops = &sh532u_fileops;
1650         info->miscdev.minor = MISC_DYNAMIC_MINOR;
1651         if (misc_register(&info->miscdev)) {
1652                 dev_err(&client->dev, "%s unable to register misc device %s\n",
1653                                 __func__, dname);
1654                 sh532u_del(info);
1655                 return -ENODEV;
1656         }
1657
1658         return 0;
1659 }
1660
1661 static const struct i2c_device_id sh532u_id[] = {
1662         { "sh532u", 0 },
1663         { },
1664 };
1665
1666 MODULE_DEVICE_TABLE(i2c, sh532u_id);
1667
1668 static struct i2c_driver sh532u_i2c_driver = {
1669         .driver = {
1670                 .name = "sh532u",
1671                 .owner = THIS_MODULE,
1672         },
1673         .id_table = sh532u_id,
1674         .probe = sh532u_probe,
1675         .remove = sh532u_remove,
1676 };
1677
1678 static int __init sh532u_init(void)
1679 {
1680         return i2c_add_driver(&sh532u_i2c_driver);
1681 }
1682
1683 static void __exit sh532u_exit(void)
1684 {
1685         i2c_del_driver(&sh532u_i2c_driver);
1686 }
1687
1688 module_init(sh532u_init);
1689 module_exit(sh532u_exit);
1690