Revert "media: camera config changes"
[linux-2.6.git] / drivers / media / video / tegra / sh532u.c
1 /*
2  * SH532U focuser driver.
3  *
4  * Copyright (C) 2011-2013 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 <device>_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  *         To disable sync, set .sync = 0.  Note that the .num = 0 device is
44  *         not allowed to be synced to.
45  *         This is typically used for stereo applications.
46  * .dev_name = The MISC driver name the device registers as.  If not used,
47  *             then the part number of the device is used for the driver name.
48  *             If using the NVC user driver then use the name found in this
49  *             driver under _default_pdata.
50  * .gpio_count = The ARRAY_SIZE of the nvc_gpio_pdata table.
51  * .gpio = A pointer to the nvc_gpio_pdata structure's platform GPIO data.
52  *         The GPIO mechanism works by cross referencing the .gpio_type key
53  *         among the nvc_gpio_pdata GPIO data and the driver's nvc_gpio_init
54  *         GPIO data to build a GPIO table the driver can use.  The GPIO's
55  *         defined in the device header file's _gpio_type enum are the
56  *         gpio_type keys for the nvc_gpio_pdata and nvc_gpio_init structures.
57  *         These need to be present in the board file's nvc_gpio_pdata
58  *         structure for the GPIO's that are used.
59  *         The driver's GPIO logic uses assert/deassert throughout until the
60  *         low level _gpio_wr/rd calls where the .assert_high is used to
61  *         convert the value to the correct signal level.
62  *         See the GPIO notes in nvc.h for additional information.
63  *
64  * The following is specific to NVC kernel focus drivers:
65  * .nvc = Pointer to the nvc_focus_nvc structure.  This structure needs to
66  *        be defined and populated if overriding the driver defaults.
67  * .cap = Pointer to the nvc_focus_cap structure.  This structure needs to
68  *        be defined and populated if overriding the driver defaults.
69  *
70  * The following is specific to this NVC kernel focus driver:
71  * .info = Pointer to the sh532u_pdata_info structure.  This structure does
72  *         not need to be defined and populated unless overriding ROM data.
73 .* .i2c_addr_rom = The I2C address of the onboard ROM.
74  *
75  * Power Requirements:
76  * The device's header file defines the voltage regulators needed with the
77  * enumeration <device>_vreg.  The order these are enumerated is the order
78  * the regulators will be enabled when powering on the device.  When the
79  * device is powered off the regulators are disabled in descending order.
80  * The <device>_vregs table in this driver uses the nvc_regulator_init
81  * structure to define the regulator ID strings that go with the regulators
82  * defined with <device>_vreg.  These regulator ID strings (or supply names)
83  * will be used in the regulator_get function in the _vreg_init function.
84  * The board power file and <device>_vregs regulator ID strings must match.
85  */
86
87
88 #include <linux/fs.h>
89 #include <linux/i2c.h>
90 #include <linux/miscdevice.h>
91 #include <linux/slab.h>
92 #include <linux/delay.h>
93 #include <linux/uaccess.h>
94 #include <linux/list.h>
95 #include <linux/jiffies.h>
96 #include <linux/gpio.h>
97 #include <media/sh532u.h>
98
99 #define SH532U_ID               0x0532
100 #define SH532U_STARTUP_DELAY_MS 10
101 /* defaults if no ROM data */
102 #define SH532U_HYPERFOCAL_RATIO 1836 /* 41.2f/224.4f Ratio source: SEMCO */
103 /* _HYPERFOCAL_RATIO is multiplied and _HYPERFOCAL_DIV divides for float */
104 #define SH532U_HYPERFOCAL_DIV   10000
105 #define SH532U_FOCAL_LENGTH     0x408D70A4
106 #define SH532U_FNUMBER          0x40333333
107 #define SH532U_MAX_APERATURE    0x3FCA0EA1
108 #define SH532U_ACTUATOR_RANGE   1000
109 #define SH532U_SETTLETIME       30
110 #define SH532U_FOCUS_MACRO      950
111 #define SH532U_FOCUS_HYPER      250
112 #define SH532U_FOCUS_INFINITY   50
113 #define SH532U_TIMEOUT_MS       200
114 #define SH532U_POS_LOW_DEFAULT  0xA000
115 #define SH532U_POS_HIGH_DEFAULT 0x6000
116 #define SH532U_SLEW_RATE                        1
117 #define SH532U_POS_TRANSLATE            0
118 #define SH532U_POS_SIGN_CHANGER         (-1)
119
120 static u8 sh532u_ids[] = {
121         0xF0,
122 };
123
124 static struct nvc_gpio_init sh532u_gpios[] = {
125         { SH532U_GPIO_RESET, GPIOF_OUT_INIT_LOW, "reset", false, true, },
126         { SH532U_GPIO_I2CMUX, 0, "i2c_mux", 0, false, },
127         { SH532U_GPIO_GP1, 0, "gp1", 0, false, },
128         { SH532U_GPIO_GP2, 0, "gp2", 0, false, },
129         { SH532U_GPIO_GP3, 0, "gp3", 0, false, },
130 };
131
132 static struct nvc_regulator_init sh532u_vregs[] = {
133         { SH532U_VREG_AVDD, "avdd", },
134         { SH532U_VREG_DVDD, "dvdd", },
135 };
136
137 struct sh532u_info {
138         atomic_t in_use;
139         struct i2c_client *i2c_client;
140         struct sh532u_platform_data *pdata;
141         struct miscdevice miscdev;
142         struct list_head list;
143         struct nvc_gpio gpio[ARRAY_SIZE(sh532u_gpios)];
144         struct nvc_regulator vreg[ARRAY_SIZE(sh532u_vregs)];
145         int pwr_api;
146         int pwr_dev;
147         u8 s_mode;
148         struct sh532u_info *s_info;
149         u8 id_minor;
150         unsigned i2c_addr_rom;
151         struct nvc_focus_nvc nvc;
152         struct nvc_focus_cap cap;
153         struct nv_focuser_config config;
154         enum nvc_focus_sts sts;
155         struct sh532u_pdata_info cfg;
156         bool reset_flag;
157         bool init_cal_flag;
158         s16 abs_base;
159         u32 abs_range;
160         u32 pos_rel;
161         s16 pos_abs;
162         long pos_time_wr;
163 };
164
165 static struct sh532u_pdata_info sh532u_default_info = {
166         .move_timeoutms = SH532U_TIMEOUT_MS,
167         .focus_hyper_ratio = SH532U_HYPERFOCAL_RATIO,
168         .focus_hyper_div = SH532U_HYPERFOCAL_DIV,
169 };
170
171 static struct nvc_focus_cap sh532u_default_cap = {
172         .version        = NVC_FOCUS_CAP_VER2,
173         .actuator_range = SH532U_ACTUATOR_RANGE,
174         .settle_time    = SH532U_SETTLETIME,
175         .focus_macro    = SH532U_FOCUS_MACRO,
176         .focus_hyper    = SH532U_FOCUS_HYPER,
177         .focus_infinity = SH532U_FOCUS_INFINITY,
178         .slew_rate        = SH532U_SLEW_RATE,
179         .position_translate = SH532U_POS_TRANSLATE,
180 };
181
182 static struct nvc_focus_nvc sh532u_default_nvc = {
183         .focal_length   = SH532U_FOCAL_LENGTH,
184         .fnumber        = SH532U_FNUMBER,
185         .max_aperature  = SH532U_MAX_APERATURE,
186 };
187
188 static struct sh532u_platform_data sh532u_default_pdata = {
189         .cfg            = 0,
190         .num            = 0,
191         .sync           = 0,
192         .dev_name       = "focuser",
193         .i2c_addr_rom   = 0x50,
194 };
195
196 static u32 sh532u_a2buf[] = {
197         0x0018019c,
198         0x0018019d,
199         0x0000019e,
200         0x007f0192,
201         0x00000194,
202         0x00f00184,
203         0x00850187,
204         0x0000018a,
205         0x00fd7187,
206         0x007f7183,
207         0x0008025a,
208         0x05042218,
209         0x80010216,
210         0x000601a0,
211         0x00808183,
212         0xffffffff
213 };
214
215 static LIST_HEAD(sh532u_info_list);
216 static DEFINE_SPINLOCK(sh532u_spinlock);
217
218
219 static int sh532u_i2c_rd8(struct sh532u_info *info, u8 addr, u8 reg, u8 *val)
220 {
221         struct i2c_msg msg[2];
222         u8 buf[2];
223
224         buf[0] = reg;
225         if (addr) {
226                 msg[0].addr = addr;
227                 msg[1].addr = addr;
228         } else {
229                 msg[0].addr = info->i2c_client->addr;
230                 msg[1].addr = info->i2c_client->addr;
231         }
232         msg[0].flags = 0;
233         msg[0].len = 1;
234         msg[0].buf = &buf[0];
235         msg[1].flags = I2C_M_RD;
236         msg[1].len = 1;
237         msg[1].buf = &buf[1];
238         *val = 0;
239         if (i2c_transfer(info->i2c_client->adapter, msg, 2) != 2)
240                 return -EIO;
241
242         *val = buf[1];
243         return 0;
244 }
245
246 static int sh532u_i2c_wr8(struct sh532u_info *info, u8 reg, u8 val)
247 {
248         struct i2c_msg msg;
249         u8 buf[2];
250
251         buf[0] = reg;
252         buf[1] = val;
253         msg.addr = info->i2c_client->addr;
254         msg.flags = 0;
255         msg.len = 2;
256         msg.buf = &buf[0];
257         if (i2c_transfer(info->i2c_client->adapter, &msg, 1) != 1)
258                 return -EIO;
259
260         return 0;
261 }
262
263 static int sh532u_i2c_rd16(struct sh532u_info *info, u8 reg, u16 *val)
264 {
265         struct i2c_msg msg[2];
266         u8 buf[3];
267
268         buf[0] = reg;
269         msg[0].addr = info->i2c_client->addr;
270         msg[0].flags = 0;
271         msg[0].len = 1;
272         msg[0].buf = &buf[0];
273         msg[1].addr = info->i2c_client->addr;
274         msg[1].flags = I2C_M_RD;
275         msg[1].len = 2;
276         msg[1].buf = &buf[1];
277         if (i2c_transfer(info->i2c_client->adapter, msg, 2) != 2)
278                 return -EIO;
279
280         *val = (((u16)buf[1] << 8) | (u16)buf[2]);
281         return 0;
282 }
283
284
285 static int sh532u_i2c_wr16(struct sh532u_info *info, u8 reg, u16 val)
286 {
287         struct i2c_msg msg;
288         u8 buf[3];
289
290         buf[0] = reg;
291         buf[1] = (u8)(val >> 8);
292         buf[2] = (u8)(val & 0xff);
293         msg.addr = info->i2c_client->addr;
294         msg.flags = 0;
295         msg.len = 3;
296         msg.buf = &buf[0];
297         if (i2c_transfer(info->i2c_client->adapter, &msg, 1) != 1)
298                 return -EIO;
299
300         return 0;
301 }
302
303 static int sh532u_i2c_rd32(struct sh532u_info *info, u8 addr, u8 reg, u32 *val)
304 {
305         struct i2c_msg msg[2];
306         u8 buf[5];
307
308         buf[0] = reg;
309         if (addr) {
310                 msg[0].addr = addr;
311                 msg[1].addr = addr;
312         } else {
313                 msg[0].addr = info->i2c_client->addr;
314                 msg[1].addr = info->i2c_client->addr;
315         }
316         msg[0].flags = 0;
317         msg[0].len = 1;
318         msg[0].buf = &buf[0];
319         msg[1].flags = I2C_M_RD;
320         msg[1].len = 4;
321         msg[1].buf = &buf[1];
322         if (i2c_transfer(info->i2c_client->adapter, msg, 2) != 2)
323                 return -EIO;
324
325         *val = (((u32)buf[4] << 24) | ((u32)buf[3] << 16) |
326                         ((u32)buf[2] << 8) | ((u32)buf[1]));
327         return 0;
328 }
329
330 static int sh532u_gpio_wr(struct sh532u_info *info,
331                           enum sh532u_gpio i,
332                           int val) /* val: 0=deassert, 1=assert */
333 {
334         int err = -EINVAL;
335
336         if (info->gpio[i].flag) {
337                 if (val)
338                         val = 1;
339                 if (!info->gpio[i].active_high)
340                         val = !val;
341                 val &= 1;
342                 err = val;
343                 gpio_set_value_cansleep(info->gpio[i].gpio, val);
344                 dev_dbg(&info->i2c_client->dev, "%s %u %d\n",
345                         __func__, info->gpio[i].gpio, val);
346         }
347         return err; /* return value written or error */
348 }
349
350 static int sh532u_gpio_reset(struct sh532u_info *info, int val)
351 {
352         int err = 0;
353
354         if (val) {
355                 if (!info->reset_flag) {
356                         info->reset_flag = true;
357                         err = sh532u_gpio_wr(info, SH532U_GPIO_RESET, 1);
358                         if (err < 0)
359                                 return 0; /* flag no reset */
360
361                         mdelay(1);
362                         sh532u_gpio_wr(info, SH532U_GPIO_RESET, 0);
363                         mdelay(SH532U_STARTUP_DELAY_MS); /* startup delay */
364                         err = 1; /* flag that a reset was done */
365                 }
366         } else {
367                 info->reset_flag = false;
368         }
369         return err;
370 }
371
372 static void sh532u_gpio_able(struct sh532u_info *info, int val)
373 {
374         if (val) {
375                 sh532u_gpio_wr(info, SH532U_GPIO_GP1, val);
376                 sh532u_gpio_wr(info, SH532U_GPIO_GP2, val);
377                 sh532u_gpio_wr(info, SH532U_GPIO_GP3, val);
378         } else {
379                 sh532u_gpio_wr(info, SH532U_GPIO_GP3, val);
380                 sh532u_gpio_wr(info, SH532U_GPIO_GP2, val);
381                 sh532u_gpio_wr(info, SH532U_GPIO_GP1, val);
382         }
383 }
384
385 static void sh532u_gpio_exit(struct sh532u_info *info)
386 {
387         unsigned i;
388
389         for (i = 0; i < ARRAY_SIZE(sh532u_gpios); i++) {
390                 if (info->gpio[i].flag && info->gpio[i].own) {
391                         gpio_free(info->gpio[i].gpio);
392                         info->gpio[i].own = false;
393                 }
394         }
395 }
396
397 static void sh532u_gpio_init(struct sh532u_info *info)
398 {
399         char label[32];
400         unsigned long flags;
401         unsigned type;
402         unsigned i;
403         unsigned j;
404         int err;
405
406         for (i = 0; i < ARRAY_SIZE(sh532u_gpios); i++)
407                 info->gpio[i].flag = false;
408         if (!info->pdata->gpio_count || !info->pdata->gpio)
409                 return;
410
411         for (i = 0; i < ARRAY_SIZE(sh532u_gpios); i++) {
412                 type = sh532u_gpios[i].gpio_type;
413                 for (j = 0; j < info->pdata->gpio_count; j++) {
414                         if (type == info->pdata->gpio[j].gpio_type)
415                                 break;
416                 }
417                 if (j == info->pdata->gpio_count)
418                         continue;
419
420                 info->gpio[type].gpio = info->pdata->gpio[j].gpio;
421                 info->gpio[type].flag = true;
422                 if (sh532u_gpios[i].use_flags) {
423                         flags = sh532u_gpios[i].flags;
424                         info->gpio[type].active_high =
425                                                    sh532u_gpios[i].active_high;
426                 } else {
427                         info->gpio[type].active_high =
428                                               info->pdata->gpio[j].active_high;
429                         if (info->gpio[type].active_high)
430                                 flags = GPIOF_OUT_INIT_LOW;
431                         else
432                                 flags = GPIOF_OUT_INIT_HIGH;
433                 }
434                 if (!info->pdata->gpio[j].init_en)
435                         continue;
436
437                 snprintf(label, sizeof(label), "sh532u_%u_%s",
438                          info->pdata->num, sh532u_gpios[i].label);
439                 err = gpio_request_one(info->gpio[type].gpio, flags, label);
440                 if (err) {
441                         dev_err(&info->i2c_client->dev, "%s ERR %s %u\n",
442                                 __func__, label, info->gpio[type].gpio);
443                 } else {
444                         info->gpio[type].own = true;
445                         dev_dbg(&info->i2c_client->dev, "%s %s %u\n",
446                                 __func__, label, info->gpio[type].gpio);
447                 }
448         }
449 }
450
451 static int sh532u_vreg_dis(struct sh532u_info *info,
452                            enum sh532u_vreg i)
453 {
454         int err = 0;
455
456         if (info->vreg[i].vreg_flag && (info->vreg[i].vreg != NULL)) {
457                 err = regulator_disable(info->vreg[i].vreg);
458                 if (!err)
459                         dev_dbg(&info->i2c_client->dev, "%s: %s\n",
460                                 __func__, info->vreg[i].vreg_name);
461                 else
462                         dev_err(&info->i2c_client->dev, "%s %s ERR\n",
463                                 __func__, info->vreg[i].vreg_name);
464         }
465         info->vreg[i].vreg_flag = false;
466         return err;
467 }
468
469 static int sh532u_vreg_dis_all(struct sh532u_info *info)
470 {
471         unsigned i;
472         int err = 0;
473
474         for (i = ARRAY_SIZE(sh532u_vregs); i > 0; i--)
475                 err |= sh532u_vreg_dis(info, (i - 1));
476         return err;
477 }
478
479 static int sh532u_vreg_en(struct sh532u_info *info,
480                           enum sh532u_vreg i)
481 {
482         int err = 0;
483
484         if (!info->vreg[i].vreg_flag && (info->vreg[i].vreg != NULL)) {
485                 err = regulator_enable(info->vreg[i].vreg);
486                 if (!err) {
487                         dev_dbg(&info->i2c_client->dev, "%s: %s\n",
488                                 __func__, info->vreg[i].vreg_name);
489                         info->vreg[i].vreg_flag = true;
490                         err = 1; /* flag regulator state change */
491                 } else {
492                         dev_err(&info->i2c_client->dev, "%s %s ERR\n",
493                                 __func__, info->vreg[i].vreg_name);
494                 }
495         }
496         return err;
497 }
498
499 static int sh532u_vreg_en_all(struct sh532u_info *info)
500 {
501         unsigned i;
502         int err = 0;
503
504         for (i = 0; i < ARRAY_SIZE(sh532u_vregs); i++)
505                 err |= sh532u_vreg_en(info, i);
506         return err;
507 }
508
509 static void sh532u_vreg_exit(struct sh532u_info *info)
510 {
511         unsigned i;
512
513         for (i = 0; i < ARRAY_SIZE(sh532u_vregs); i++) {
514                 regulator_put(info->vreg[i].vreg);
515                 info->vreg[i].vreg = NULL;
516         }
517 }
518
519 static int sh532u_vreg_init(struct sh532u_info *info)
520 {
521         unsigned i;
522         unsigned j;
523         int err = 0;
524
525         for (i = 0; i < ARRAY_SIZE(sh532u_vregs); i++) {
526                 j = sh532u_vregs[i].vreg_num;
527                 info->vreg[j].vreg_name = sh532u_vregs[i].vreg_name;
528                 info->vreg[j].vreg_flag = false;
529                 info->vreg[j].vreg = regulator_get(&info->i2c_client->dev,
530                                                    info->vreg[j].vreg_name);
531                 if (IS_ERR_OR_NULL(info->vreg[j].vreg)) {
532                         if (PTR_ERR(info->vreg[j].vreg) != -ENODEV)
533                                 dev_dbg(&info->i2c_client->dev,
534                                         "%s %s ERR: %d\n",
535                                         __func__, info->vreg[j].vreg_name,
536                                         (int)info->vreg[j].vreg);
537                         else
538                                 dev_info(&info->i2c_client->dev,
539                                          "%s no regulator found for %s. "
540                                          "This board may not have an "
541                                          "independent %s regulator.\n",
542                                          __func__, info->vreg[j].vreg_name,
543                                          info->vreg[j].vreg_name);
544                         err |= PTR_ERR(info->vreg[j].vreg);
545                         info->vreg[j].vreg = NULL;
546                 } else {
547                         dev_dbg(&info->i2c_client->dev, "%s: %s\n",
548                                 __func__, info->vreg[j].vreg_name);
549                 }
550         }
551         return err;
552 }
553
554 static int sh532u_pm_wr(struct sh532u_info *info, int pwr)
555 {
556         int err = 0;
557
558         if ((info->pdata->cfg & (NVC_CFG_OFF2STDBY | NVC_CFG_BOOT_INIT)) &&
559                         (pwr == NVC_PWR_OFF ||
560                          pwr == NVC_PWR_STDBY_OFF))
561                 pwr = NVC_PWR_STDBY;
562
563         if (pwr == info->pwr_dev)
564                 return 0;
565
566         switch (pwr) {
567         case NVC_PWR_OFF_FORCE:
568         case NVC_PWR_OFF:
569                 err = sh532u_vreg_dis_all(info);
570                 sh532u_gpio_able(info, 0);
571                 sh532u_gpio_reset(info, 0);
572                 break;
573
574         case NVC_PWR_STDBY_OFF:
575         case NVC_PWR_STDBY:
576                 err = sh532u_vreg_en_all(info);
577                 sh532u_gpio_able(info, 1);
578                 sh532u_gpio_reset(info, 1);
579                 err |= sh532u_i2c_wr8(info, STBY_211, 0x80);
580                 err |= sh532u_i2c_wr8(info, CLKSEL_211, 0x38);
581                 err |= sh532u_i2c_wr8(info, CLKSEL_211, 0x39);
582                 break;
583
584         case NVC_PWR_COMM:
585         case NVC_PWR_ON:
586                 err = sh532u_vreg_en_all(info);
587                 sh532u_gpio_able(info, 1);
588                 sh532u_gpio_reset(info, 1);
589                 err |= sh532u_i2c_wr8(info, CLKSEL_211, 0x38);
590                 err |= sh532u_i2c_wr8(info, CLKSEL_211, 0x34);
591                 err |= sh532u_i2c_wr8(info, STBY_211, 0xF0);
592                 break;
593
594         default:
595                 err = -EINVAL;
596                 break;
597         }
598
599         if (err < 0) {
600                 dev_err(&info->i2c_client->dev, "%s err %d\n", __func__, err);
601                 pwr = NVC_PWR_ERR;
602         }
603         info->pwr_dev = pwr;
604         dev_dbg(&info->i2c_client->dev, "%s pwr_dev=%d\n",
605                 __func__, info->pwr_dev);
606         if (err > 0)
607                 return 0;
608
609         return err;
610 }
611
612 static int sh532u_pm_wr_s(struct sh532u_info *info, int pwr)
613 {
614         int err1 = 0;
615         int err2 = 0;
616
617         if ((info->s_mode == NVC_SYNC_OFF) ||
618                         (info->s_mode == NVC_SYNC_MASTER) ||
619                         (info->s_mode == NVC_SYNC_STEREO))
620                 err1 = sh532u_pm_wr(info, pwr);
621         if ((info->s_mode == NVC_SYNC_SLAVE) ||
622                         (info->s_mode == NVC_SYNC_STEREO))
623                 err2 = sh532u_pm_wr(info->s_info, pwr);
624         return err1 | err2;
625 }
626
627 static int sh532u_pm_api_wr(struct sh532u_info *info, int pwr)
628 {
629         int err = 0;
630
631         if (!pwr || (pwr > NVC_PWR_ON))
632                 return 0;
633
634         if (pwr > info->pwr_dev)
635                 err = sh532u_pm_wr_s(info, pwr);
636         if (!err)
637                 info->pwr_api = pwr;
638         else
639                 info->pwr_api = NVC_PWR_ERR;
640         if (info->pdata->cfg & NVC_CFG_NOERR)
641                 return 0;
642
643         return err;
644 }
645
646 static int sh532u_pm_dev_wr(struct sh532u_info *info, int pwr)
647 {
648         if (pwr < info->pwr_api)
649                 pwr = info->pwr_api;
650         if (info->sts == NVC_FOCUS_STS_WAIT_FOR_MOVE_END)
651                 pwr = NVC_PWR_ON;
652         return sh532u_pm_wr(info, pwr);
653 }
654
655 static void sh532u_pm_exit(struct sh532u_info *info)
656 {
657         sh532u_pm_wr(info, NVC_PWR_OFF_FORCE);
658         sh532u_vreg_exit(info);
659         sh532u_gpio_exit(info);
660 }
661
662 static void sh532u_pm_init(struct sh532u_info *info)
663 {
664         sh532u_gpio_init(info);
665         sh532u_vreg_init(info);
666 }
667
668 static int sh532u_reset(struct sh532u_info *info, u32 level)
669 {
670         int err;
671
672         if (level == NVC_RESET_SOFT) {
673                 err = sh532u_pm_wr(info, NVC_PWR_COMM);
674                 err |= sh532u_i2c_wr8(info, SFTRST_211, 0xFF); /* SW reset */
675                 mdelay(1);
676                 err |= sh532u_i2c_wr8(info, SFTRST_211, 0);
677         } else {
678                 err = sh532u_pm_wr(info, NVC_PWR_OFF_FORCE);
679         }
680         err |= sh532u_pm_wr(info, info->pwr_api);
681         return err;
682 }
683
684 static int sh532u_dev_id(struct sh532u_info *info)
685 {
686         u8 val = 0;
687         unsigned i;
688         int err;
689
690         sh532u_pm_dev_wr(info, NVC_PWR_COMM);
691         err = sh532u_i2c_rd8(info, 0, HVCA_DEVICE_ID, &val);
692         if (!err) {
693                 dev_dbg(&info->i2c_client->dev, "%s found devId: %x\n",
694                         __func__, val);
695                 info->id_minor = 0;
696                 for (i = 0; i < ARRAY_SIZE(sh532u_ids); i++) {
697                         if (val == sh532u_ids[i]) {
698                                 info->id_minor = val;
699                                 break;
700                         }
701                 }
702                 if (!info->id_minor) {
703                         err = -ENODEV;
704                         dev_dbg(&info->i2c_client->dev, "%s No devId match\n",
705                                 __func__);
706                 }
707         }
708         sh532u_pm_dev_wr(info, NVC_PWR_OFF);
709         return err;
710 }
711
712 static void sh532u_sts_rd(struct sh532u_info *info)
713 {
714         u8 us_tmp;
715         u16 us_smv_fin;
716         int err;
717
718         if (info->sts == NVC_FOCUS_STS_INITIALIZING)
719                 return;
720
721         info->sts = NVC_FOCUS_STS_NO_DEVICE; /* assume I2C err */
722         err = sh532u_i2c_rd8(info, 0, STMVEN_211, &us_tmp);
723         err |= sh532u_i2c_rd16(info, RZ_211H, &us_smv_fin);
724         if (err)
725                 return;
726
727         /* StepMove Error Handling, Unexpected Position */
728         if ((us_smv_fin == 0x7FFF) || (us_smv_fin == 0x8001))
729                 /* Stop StepMove Operation */
730                 sh532u_i2c_wr8(info, STMVEN_211, us_tmp & 0xFE);
731         if (us_tmp & STMVEN_ON) {
732                 err = sh532u_i2c_rd8(info, 0, MSSET_211, &us_tmp);
733                 if (!err) {
734                         if (us_tmp & CHTGST_ON)
735                                 info->sts = NVC_FOCUS_STS_WAIT_FOR_SETTLE;
736                         else
737                                 info->sts = NVC_FOCUS_STS_LENS_SETTLED;
738                 }
739         } else {
740                 info->sts = NVC_FOCUS_STS_WAIT_FOR_MOVE_END;
741         }
742 }
743
744 static s16 sh532u_rel2abs(struct sh532u_info *info, s32 rel_position)
745 {
746         s16 abs_pos;
747
748         if (rel_position > info->cap.actuator_range)
749                 rel_position = info->cap.actuator_range;
750         if (info->cap.position_translate)  {
751                 rel_position = info->cap.actuator_range - rel_position;
752                 if (rel_position) {
753                         rel_position *= info->abs_range;
754                         rel_position /= info->cap.actuator_range;
755                 }
756                 abs_pos = (s16)(info->abs_base + rel_position);
757         } else {
758                 abs_pos = rel_position * SH532U_POS_SIGN_CHANGER;
759         }
760
761         if (abs_pos < info->cfg.limit_low)
762                 abs_pos = info->cfg.limit_low;
763         if (abs_pos > info->cfg.limit_high)
764                 abs_pos = info->cfg.limit_high;
765
766         dev_dbg(&info->i2c_client->dev, "%s: rel_position %d returning abs_pos %d\n",
767                         __func__, rel_position, abs_pos);
768
769         return abs_pos;
770 }
771
772 static u32 sh532u_abs2rel(struct sh532u_info *info, s16 abs_position)
773 {
774         u32 rel_pos;
775
776         if (abs_position > info->cfg.limit_high)
777                 abs_position = info->cfg.limit_high;
778         if (abs_position < info->abs_base)
779                 abs_position = info->abs_base;
780
781         if (info->cap.position_translate) {
782                 rel_pos = (u32)(abs_position - info->abs_base);
783                 rel_pos *= info->cap.actuator_range;
784                 rel_pos /= info->abs_range;
785
786                 if (rel_pos > info->cap.actuator_range)
787                         rel_pos = info->cap.actuator_range;
788                 rel_pos = info->cap.actuator_range - rel_pos;
789         } else {
790                 rel_pos = abs_position * SH532U_POS_SIGN_CHANGER;
791         }
792         dev_dbg(&info->i2c_client->dev, "%s: abs_position %d returning rel_pos %d",
793                         __func__, abs_position, rel_pos);
794
795         return rel_pos;
796 }
797
798 static int sh532u_abs_pos_rd(struct sh532u_info *info, s16 *position)
799 {
800         int err;
801         u16 abs_pos = 0;
802
803         err = sh532u_i2c_rd16(info, RZ_211H, &abs_pos);
804         *position = (s16)abs_pos;
805         return err;
806 }
807
808 static int sh532u_rel_pos_rd(struct sh532u_info *info, s32 *position)
809 {
810         s16 abs_pos;
811         long msec;
812         int pos;
813         int err;
814
815         err = sh532u_abs_pos_rd(info, &abs_pos);
816         if (err)
817                 return -EINVAL;
818
819         if ((abs_pos >= (info->pos_abs - STMV_SIZE)) &&
820                         (abs_pos <= (info->pos_abs + STMV_SIZE))) {
821                 pos = (int)info->pos_rel;
822         } else {
823                 msec = jiffies;
824                 msec -= info->pos_time_wr;
825                 msec = msec * 1000 / HZ;
826                 sh532u_sts_rd(info);
827                 if ((info->sts == NVC_FOCUS_STS_LENS_SETTLED) ||
828                                 (msec > info->cfg.move_timeoutms)) {
829                         pos = (int)info->pos_rel;
830                 } else {
831                         pos = (int)sh532u_abs2rel(info, abs_pos);
832                         if ((pos == (info->pos_rel - 1)) ||
833                                         (pos == (info->pos_rel + 1)))
834                                 pos = (int)info->pos_rel;
835                 }
836         }
837         if (info->cap.position_translate) {
838                 if (pos < 0)
839                         pos = 0;
840         }
841         *position = pos;
842         return 0;
843 }
844
845 static void sh532u_calibration_caps(struct sh532u_info *info)
846 {
847         s16 abs_top;
848         u32 rel_range;
849         u32 rel_lo;
850         u32 rel_hi;
851         u32 step;
852         u32 loop_limit;
853         u32 i;
854
855         /*
856          * calculate relative and absolute positions
857          * Note that relative values, what upper SW uses, are the
858          * abstraction of HW (absolute) values.
859          * |<--limit_low                                  limit_high-->|
860          * | |<-------------------_ACTUATOR_RANGE------------------->| |
861          *              -focus_inf                        -focus_mac
862          *   |<---RI--->|                                 |<---RM--->|
863          *   -abs_base  -pos_low                          -pos_high  -abs_top
864          *
865          * The pos_low and pos_high are fixed absolute positions and correspond
866          * to the relative focus_infinity and focus_macro, respectively.  We'd
867          * like to have "wiggle" room (RI and RM) around these relative
868          * positions so the loop below finds the best fit for RI and RM without
869          * passing the absolute limits.
870          * We want our _ACTUATOR_RANGE to be infinity on the 0 end and macro
871          * on the max end.  However, the focuser HW is opposite this.
872          * Therefore we use the rel(ative)_lo/hi variables in the calculation
873          * loop and assign them the focus_infinity and focus_macro values.
874          */
875         rel_lo = (info->cap.actuator_range - info->cap.focus_macro);
876         rel_hi = info->cap.focus_infinity;
877         info->abs_range = (u32)(info->cfg.pos_high - info->cfg.pos_low);
878         loop_limit = (rel_lo > rel_hi) ? rel_lo : rel_hi;
879         dev_dbg(&info->i2c_client->dev, "%s: rel_lo %d rel_hi %d loop_limit %d\n",
880                                 __func__, rel_lo, rel_hi, loop_limit);
881         for (i = 0; i <= loop_limit; i++) {
882                 rel_range = info->cap.actuator_range - (rel_lo + rel_hi);
883                 step = info->abs_range / rel_range;
884                 info->abs_base = info->cfg.pos_low - (step * rel_lo);
885                 abs_top = info->cfg.pos_high + (step * rel_hi);
886                 if (info->abs_base < info->cfg.limit_low) {
887                         if (rel_lo > 0)
888                                 rel_lo--;
889                 }
890                 if (abs_top > info->cfg.limit_high) {
891                         if (rel_hi > 0)
892                                 rel_hi--;
893                 }
894                 if (info->abs_base >= info->cfg.limit_low &&
895                                         abs_top <= info->cfg.limit_high)
896                         break;
897         }
898         dev_dbg(&info->i2c_client->dev, "%s: info->abs_range %d abs_base %d abs_top %d\n",
899                         __func__, info->abs_range, info->abs_base, abs_top);
900
901         if (!info->cap.position_translate && info->abs_range)
902                 info->cap.actuator_range = info->abs_range;
903
904         info->cap.focus_hyper = info->abs_range;
905         info->abs_range = (u32)(abs_top - info->abs_base);
906         /* calculate absolute hyperfocus position */
907         info->cap.focus_hyper *= info->cfg.focus_hyper_ratio;
908         info->cap.focus_hyper /= info->cfg.focus_hyper_div;
909         abs_top = (s16)(info->cfg.pos_high - info->cap.focus_hyper);
910
911         /* update actual relative positions */
912         info->cap.focus_hyper = sh532u_abs2rel(info, abs_top);
913         dev_dbg(&info->i2c_client->dev, "%s: focus_hyper abs %d rel %d\n",
914                         __func__, abs_top, info->cap.focus_hyper);
915
916         info->cap.focus_infinity = sh532u_abs2rel(info, info->cfg.pos_high);
917         dev_dbg(&info->i2c_client->dev, "%s: focus_infinity abs %d rel %d\n",
918                         __func__, info->cfg.pos_high, info->cap.focus_infinity);
919
920         info->cap.focus_macro = sh532u_abs2rel(info, info->cfg.pos_low);
921         dev_dbg(&info->i2c_client->dev, "%s: focus_macro abs %d rel %d\n",
922                         __func__, info->cfg.pos_low, info->cap.focus_macro);
923
924         dev_dbg(&info->i2c_client->dev, "%s: Version %d actuator_range %d "
925                         "settle_time %d position_traslate %d\n",
926                         __func__, info->cap.version, info->cap.actuator_range,
927                         info->cap.settle_time, info->cap.position_translate);
928 }
929
930 static int sh532u_calibration(struct sh532u_info *info, bool use_defaults)
931 {
932         u8 reg;
933         int err;
934         int ret = 0;
935
936         if (info->init_cal_flag) {
937                 dev_dbg(&info->i2c_client->dev, "%s: Already initialized"
938                                 "Returning\n", __func__);
939                 return 0;
940         }
941
942         /*
943          * Get Inf1, Mac1
944          * Inf1 and Mac1 are the mechanical limit position.
945          * Inf1: top limit.
946          * Mac1: bottom limit.
947          */
948         err = sh532u_i2c_rd8(info, info->i2c_addr_rom, addrMac1, &reg);
949         if (!err && (reg != 0) && (reg != 0xFF))
950                 info->cfg.limit_low = (reg<<8) & 0xff00;
951         ret = err;
952         err = sh532u_i2c_rd8(info, info->i2c_addr_rom, addrInf1, &reg);
953         if (!err && (reg != 0) && (reg != 0xFF))
954                 info->cfg.limit_high = (reg<<8) & 0xff00;
955         ret |= err;
956         /*
957          * Get Inf2, Mac2
958          * Inf2 and Mac2 are the calibration data for SEMCO AF lens.
959          * Inf2: Best focus (lens position) when object distance is 1.2M.
960          * Mac2: Best focus (lens position) when object distance is 10cm.
961          */
962         err = sh532u_i2c_rd8(info, info->i2c_addr_rom, addrMac2, &reg);
963         if (!err && (reg != 0) && (reg != 0xFF))
964                 info->cfg.pos_low = (reg << 8) & 0xff00;
965         ret |= err;
966         err = sh532u_i2c_rd8(info, info->i2c_addr_rom, addrInf2, &reg);
967         if (!err && (reg != 0) && (reg != 0xFF))
968                 info->cfg.pos_high = (reg << 8) & 0xff00;
969         ret |= err;
970         /* set overrides */
971         if (info->pdata->info) {
972                 if (info->pdata->info->pos_low)
973                         info->cfg.pos_low = info->pdata->info->pos_low;
974                 if (info->pdata->info->pos_high)
975                         info->cfg.pos_high = info->pdata->info->pos_high;
976                 if (info->pdata->info->limit_low)
977                         info->cfg.limit_low = info->pdata->info->limit_low;
978                 if (info->pdata->info->limit_high)
979                         info->cfg.limit_high = info->pdata->info->limit_high;
980                 if (info->pdata->info->move_timeoutms)
981                         info->cfg.move_timeoutms =
982                                         info->pdata->info->move_timeoutms;
983                 if (info->pdata->info->focus_hyper_ratio)
984                         info->cfg.focus_hyper_ratio =
985                                         info->pdata->info->focus_hyper_ratio;
986                 if (info->pdata->info->focus_hyper_div)
987                         info->cfg.focus_hyper_div =
988                                         info->pdata->info->focus_hyper_div;
989         }
990         /*
991          * There is known to be many sh532u devices with no EPROM data.
992          * Using default data is known to reduce the sh532u performance since
993          * the defaults may no where be close to the correct values that
994          * should be used.  However, we don't want to prevent the camera from
995          * starting due to the lack of the EPROM data.
996          * The following truth table shows the action to take at this point:
997          * DFLT = the use_defaults flag (used after multiple attempts)
998          * I2C = the I2C transactions to get the data.
999          * DATA = the needed data either from the EPROM or board file.
1000          * DFLT   I2C   DATA   Action
1001          * --------------------------
1002          *  0     FAIL  FAIL   Exit with -EIO
1003          *  0     FAIL  PASS   Continue to calculations
1004          *  0     PASS  FAIL   Use defaults
1005          *  0     PASS  PASS   Continue to calculations
1006          *  1     FAIL  FAIL   Use defaults
1007          *  1     FAIL  PASS   Continue to calculations
1008          *  1     PASS  FAIL   Use defaults
1009          *  1     PASS  PASS   Continue to calculations
1010          */
1011         /* err = DATA where FAIL = 1 */
1012         if (!info->cfg.pos_low || info->cfg.pos_high <= info->cfg.pos_low ||
1013                 !info->cfg.limit_low ||
1014                 info->cfg.limit_high <= info->cfg.limit_low)
1015                 err = 1;
1016         else
1017                 err = 0;
1018         /* Exit with -EIO */
1019         if (!use_defaults && ret && err) {
1020                 dev_err(&info->i2c_client->dev, "%s ERR\n", __func__);
1021                 return -EIO;
1022         }
1023
1024         /* Use defaults */
1025         if (err) {
1026                 info->cfg.pos_low = SH532U_POS_LOW_DEFAULT;
1027                 info->cfg.pos_high = SH532U_POS_HIGH_DEFAULT;
1028                 info->cfg.limit_low = SH532U_POS_LOW_DEFAULT;
1029                 info->cfg.limit_high = SH532U_POS_HIGH_DEFAULT;
1030                 dev_err(&info->i2c_client->dev, "%s ERR: ERPOM data is void!  "
1031                         "Focuser will use defaults that will cause "
1032                         "reduced functionality!\n", __func__);
1033         }
1034         if (info->cfg.pos_low < info->cfg.limit_low)
1035                 info->cfg.pos_low = info->cfg.limit_low;
1036         if (info->cfg.pos_high > info->cfg.limit_high)
1037                 info->cfg.pos_high = info->cfg.limit_high;
1038         dev_dbg(&info->i2c_client->dev, "%s pos_low=%d\n",
1039                 __func__, (int)info->cfg.pos_low);
1040         dev_dbg(&info->i2c_client->dev, "%s pos_high=%d\n",
1041                 __func__, (int)info->cfg.pos_high);
1042         dev_dbg(&info->i2c_client->dev, "%s limit_low=%d\n",
1043                 __func__, (int)info->cfg.limit_low);
1044         dev_dbg(&info->i2c_client->dev, "%s limit_high=%d\n",
1045                 __func__, (int)info->cfg.limit_high);
1046
1047         sh532u_calibration_caps(info);
1048         info->init_cal_flag = 1;
1049         dev_dbg(&info->i2c_client->dev, "%s complete\n", __func__);
1050         return 0;
1051 }
1052
1053         /* Write 1 byte data to the HVCA Drive IC by data type */
1054 static int sh532u_hvca_wr1(struct sh532u_info *info,
1055                            u8 ep_type, u8 ep_data1, u8 ep_addr)
1056 {
1057         u8 us_data;
1058         int err = 0;
1059
1060         switch (ep_type & 0xF0) {
1061         case DIRECT_MODE:
1062                 us_data = ep_data1;
1063                 break;
1064
1065         case INDIRECT_EEPROM:
1066                 err = sh532u_i2c_rd8(info,
1067                                      info->i2c_addr_rom,
1068                                      ep_data1,
1069                                      &us_data);
1070                 break;
1071
1072         case INDIRECT_HVCA:
1073                 err = sh532u_i2c_rd8(info, 0, ep_data1, &us_data);
1074                 break;
1075
1076         case MASK_AND:
1077                 err = sh532u_i2c_rd8(info, 0, ep_addr, &us_data);
1078                 us_data &= ep_data1;
1079                 break;
1080
1081         case MASK_OR:
1082                 err = sh532u_i2c_rd8(info, 0, ep_addr, &us_data);
1083                 us_data |= ep_data1;
1084                 break;
1085
1086         default:
1087                 err = -EINVAL;
1088         }
1089         if (!err)
1090                 err = sh532u_i2c_wr8(info, ep_addr, us_data);
1091         return err;
1092 }
1093
1094         /* Write 2 byte data to the HVCA Drive IC by data type */
1095 static int sh532u_hvca_wr2(struct sh532u_info *info, u8 ep_type,
1096                                 u8 ep_data1, u8 ep_data2, u8 ep_addr)
1097 {
1098         u8 uc_data1;
1099         u8 uc_data2;
1100         u16 us_data;
1101         int err = 0;
1102
1103         switch (ep_type & 0xF0) {
1104         case DIRECT_MODE:
1105                 us_data = (((u16)ep_data1 << 8) & 0xFF00) |
1106                         ((u16)ep_data2 & 0x00FF);
1107                 break;
1108
1109         case INDIRECT_EEPROM:
1110                 err = sh532u_i2c_rd8(info,
1111                                      info->i2c_addr_rom,
1112                                      ep_data1,
1113                                      &uc_data1);
1114                 err |= sh532u_i2c_rd8(info,
1115                                       info->i2c_addr_rom,
1116                                       ep_data2,
1117                                       &uc_data2);
1118                 us_data = (((u16)uc_data1 << 8) & 0xFF00) |
1119                                 ((u16)uc_data2 & 0x00FF);
1120                 break;
1121
1122         case INDIRECT_HVCA:
1123                 err = sh532u_i2c_rd8(info, 0, ep_data1, &uc_data1);
1124                 err |= sh532u_i2c_rd8(info, 0, ep_data2, &uc_data2);
1125                 us_data = (((u16)uc_data1 << 8) & 0xFF00) |
1126                                 ((u16)uc_data2 & 0x00FF);
1127                 break;
1128
1129         case MASK_AND:
1130                 err = sh532u_i2c_rd16(info, ep_addr, &us_data);
1131                 us_data &= ((((u16)ep_data1 << 8) & 0xFF00) |
1132                             ((u16)ep_data2 & 0x00FF));
1133                 break;
1134
1135         case MASK_OR:
1136                 err = sh532u_i2c_rd16(info, ep_addr, &us_data);
1137                 us_data |= ((((u16)ep_data1 << 8) & 0xFF00) |
1138                             ((u16)ep_data2 & 0x00FF));
1139                 break;
1140
1141         default:
1142                 err = -EINVAL;
1143         }
1144         if (!err)
1145                 err = sh532u_i2c_wr16(info, ep_addr, us_data);
1146         return err;
1147 }
1148
1149 static int sh532u_dev_init(struct sh532u_info *info)
1150 {
1151         int eeprom_reg;
1152         unsigned eeprom_data = 0;
1153         u8 ep_addr;
1154         u8 ep_type;
1155         u8 ep_data1;
1156         u8 ep_data2;
1157         int err;
1158         int ret = 0;
1159
1160         err = sh532u_i2c_rd8(info, 0, SWTCH_211, &ep_data1);
1161         if (err)
1162                 return err; /* exit if unable to communicate with device */
1163
1164         ep_data2 = ep_data1;
1165         err |= sh532u_i2c_rd8(info, 0, ANA1_211, &ep_data1);
1166         ep_data2 |= ep_data1;
1167         if (!err && ep_data2)
1168                 return 0; /* Already initialized */
1169
1170         info->sts = NVC_FOCUS_STS_INITIALIZING;
1171         for (eeprom_reg = 0x30; eeprom_reg <= 0x013C; eeprom_reg += 4) {
1172                 if (eeprom_reg > 0xFF) {
1173                         /* use hardcoded data instead */
1174                         eeprom_data = sh532u_a2buf[(eeprom_reg & 0xFF) / 4];
1175                 } else {
1176                         err = (sh532u_i2c_rd32(info,
1177                                             info->i2c_addr_rom,
1178                                             eeprom_reg & 0xFF,
1179                                             &eeprom_data));
1180                         if (err) {
1181                                 ret |= err;
1182                                 continue;
1183                         }
1184                 }
1185
1186                 /* HVCA Address to write eeprom Data1,Data2 by the Data type */
1187                 ep_addr = (u8)(eeprom_data & 0x000000ff);
1188                 ep_type = (u8)((eeprom_data & 0x0000ff00) >> 8);
1189                 ep_data1 = (u8)((eeprom_data & 0x00ff0000) >> 16);
1190                 ep_data2 = (u8)((eeprom_data & 0xff000000) >> 24);
1191                 if (ep_addr == 0xFF)
1192                         break;
1193
1194                 if (ep_addr == 0xDD) {
1195                         mdelay((unsigned int)((ep_data1 << 8) | ep_data2));
1196                 } else {
1197                         if ((ep_type & 0x0F) == DATA_1BYTE) {
1198                                 err = sh532u_hvca_wr1(info,
1199                                                       ep_type,
1200                                                       ep_data1,
1201                                                       ep_addr);
1202                         } else {
1203                                 err = sh532u_hvca_wr2(info,
1204                                                       ep_type,
1205                                                       ep_data1,
1206                                                       ep_data2,
1207                                                       ep_addr);
1208                         }
1209                 }
1210                 ret |= err;
1211         }
1212
1213         err = ret;
1214         if (err)
1215                 dev_err(&info->i2c_client->dev, "%s programming err=%d\n",
1216                         __func__, err);
1217         err |= sh532u_calibration(info, false);
1218         info->sts = NVC_FOCUS_STS_LENS_SETTLED;
1219         return err;
1220 }
1221
1222 static int sh532u_pos_abs_wr(struct sh532u_info *info, s16 tar_pos)
1223 {
1224         s16 cur_pos;
1225         s16 move_step;
1226         u16 move_distance;
1227         int err;
1228
1229         sh532u_pm_dev_wr(info, NVC_PWR_ON);
1230         err = sh532u_dev_init(info);
1231         if (err)
1232                 return err;
1233
1234         /* Read Current Position */
1235         err = sh532u_abs_pos_rd(info, &cur_pos);
1236         if (err)
1237                 return err;
1238
1239         dev_dbg(&info->i2c_client->dev, "%s cur_pos=%d tar_pos=%d\n",
1240                 __func__, (int)cur_pos, (int)tar_pos);
1241         info->sts = NVC_FOCUS_STS_WAIT_FOR_MOVE_END;
1242         /* Check move distance to Target Position */
1243         move_distance = abs((int)cur_pos - (int)tar_pos);
1244         /* if move distance is shorter than MS1Z12(=Step width) */
1245         if (move_distance <= STMV_SIZE) {
1246                 err = sh532u_i2c_wr8(info, MSSET_211,
1247                                      (INI_MSSET_211 | 0x01));
1248                 err |= sh532u_i2c_wr16(info, MS1Z22_211H, tar_pos);
1249         } else {
1250                 if (cur_pos < tar_pos)
1251                         move_step = STMV_SIZE;
1252                 else
1253                         move_step = -STMV_SIZE;
1254                 /* Set StepMove Target Positon */
1255                 err = sh532u_i2c_wr16(info, MS1Z12_211H, move_step);
1256                 err |= sh532u_i2c_wr16(info, STMVENDH_211, tar_pos);
1257                 /* Start StepMove */
1258                 err |= sh532u_i2c_wr8(info, STMVEN_211,
1259                                       (STMCHTG_ON |
1260                                        STMSV_ON |
1261                                        STMLFF_OFF |
1262                                        STMVEN_ON));
1263         }
1264         dev_dbg(&info->i2c_client->dev, "%s: position %d\n", __func__, tar_pos);
1265         return err;
1266 }
1267
1268 static int sh532u_move_wait(struct sh532u_info *info)
1269 {
1270         u16 us_smv_fin;
1271         u8 moveTime;
1272         u8 ucParMod;
1273         u8 tmp;
1274         int err;
1275
1276         moveTime = 0;
1277         do {
1278                 mdelay(1);
1279                 err = sh532u_i2c_rd8(info, 0, STMVEN_211, &ucParMod);
1280                 err |= sh532u_i2c_rd16(info, RZ_211H, &us_smv_fin);
1281                 if (err)
1282                         return err;
1283
1284                 /* StepMove Error Handling, Unexpected Position */
1285                 if ((us_smv_fin == 0x7FFF) || (us_smv_fin == 0x8001)) {
1286                         /* Stop StepMove Operation */
1287                         err = sh532u_i2c_wr8(info, STMVEN_211,
1288                                              ucParMod & 0xFE);
1289                         if (err)
1290                                 return err;
1291                 }
1292
1293                 moveTime++;
1294                 /* Wait StepMove operation end */
1295         } while ((ucParMod & STMVEN_ON) && (moveTime < 50));
1296
1297         moveTime = 0;
1298         if ((ucParMod & 0x08) == STMCHTG_ON) {
1299                 mdelay(5);
1300                 do {
1301                         mdelay(1);
1302                         moveTime++;
1303                         err = sh532u_i2c_rd8(info, 0, MSSET_211, &tmp);
1304                         if (err)
1305                                 return err;
1306
1307                 } while ((tmp & CHTGST_ON) && (moveTime < 15));
1308         }
1309         return err;
1310 }
1311
1312 static int sh532u_move_pulse(struct sh532u_info *info, s16 position)
1313 {
1314         int err;
1315
1316         err = sh532u_pos_abs_wr(info, position);
1317         err |= sh532u_move_wait(info);
1318         return err;
1319 }
1320
1321 static int sh532u_hvca_pos_init(struct sh532u_info *info)
1322 {
1323         s16 limit_bottom;
1324         s16 limit_top;
1325         int err;
1326
1327         limit_bottom = (((int)info->cfg.limit_low * 5) >> 3) & 0xFFC0;
1328         if (limit_bottom < info->cfg.limit_low)
1329                 limit_bottom = info->cfg.limit_low;
1330         limit_top = (((int)info->cfg.limit_high * 5) >> 3) & 0xFFC0;
1331         if (limit_top > info->cfg.limit_high)
1332                 limit_top = info->cfg.limit_high;
1333         err = sh532u_move_pulse(info, limit_bottom);
1334         err |= sh532u_move_pulse(info, limit_top);
1335         err |= sh532u_move_pulse(info, info->cfg.pos_high);
1336         return err;
1337 }
1338
1339 static int sh532u_pos_rel_wr(struct sh532u_info *info, s32 position)
1340 {
1341         s16 abs_pos;
1342
1343         if (info->cap.position_translate) {
1344                 if (position > info->cap.actuator_range) {
1345                         dev_err(&info->i2c_client->dev, "%s invalid position %d\n",
1346                                 __func__, position);
1347                         return -EINVAL;
1348                 }
1349         }
1350         abs_pos = sh532u_rel2abs(info, position);
1351
1352         info->pos_rel = position;
1353         info->pos_abs = abs_pos;
1354         info->pos_time_wr = jiffies;
1355         return sh532u_pos_abs_wr(info, abs_pos);
1356 }
1357
1358 static void sh532u_get_focuser_capabilities(struct sh532u_info *info)
1359 {
1360         memset(&info->config, 0, sizeof(info->config));
1361
1362         info->config.focal_length = info->nvc.focal_length;
1363         info->config.fnumber = info->nvc.fnumber;
1364         info->config.max_aperture = info->nvc.fnumber;
1365         info->config.range_ends_reversed = (SH532U_POS_SIGN_CHANGER == -1)
1366                                                                 ? 1 : 0;
1367
1368         info->config.settle_time = info->cap.settle_time;
1369
1370         /*
1371          * We do not use pos_working_low and pos_working_high
1372          * in the kernel driver.
1373          */
1374         info->config.pos_working_low = AF_POS_INVALID_VALUE;
1375         info->config.pos_working_high = AF_POS_INVALID_VALUE;
1376
1377         info->config.pos_actual_low = info->cfg.limit_high *
1378                                                         SH532U_POS_SIGN_CHANGER;
1379         info->config.pos_actual_high = info->cfg.limit_low *
1380                                                         SH532U_POS_SIGN_CHANGER;
1381         info->config.slew_rate = info->cap.slew_rate;
1382         info->config.circle_of_confusion = -1;
1383
1384         /*
1385          * These need to be passed up once we have the EEPROM/OTP read
1386          * routines in teh kernel. These need to be passed up much earlier on.
1387          * Till we have these routines, we pass them up as part of the get call.
1388          */
1389         info->config.num_focuser_sets = 1;
1390         info->config.focuser_set[0].posture = 'S';
1391         info->config.focuser_set[0].macro = info->cap.focus_macro;
1392         info->config.focuser_set[0].hyper = info->cap.focus_hyper;
1393         info->config.focuser_set[0].inf = info->cap.focus_infinity;
1394         info->config.focuser_set[0].hysteresis = 0;
1395         info->config.focuser_set[0].settle_time = info->cap.settle_time;
1396         info->config.focuser_set[0].num_dist_pairs = 0;
1397
1398         dev_dbg(&info->i2c_client->dev, "%s: pos_actual_low %d pos_actual_high %d "
1399                 " settle_time %d\n", __func__, info->config.pos_actual_low,
1400                 info->config.pos_actual_high, info->cap.settle_time);
1401
1402 }
1403
1404
1405 static int sh532u_set_focuser_capabilities(struct sh532u_info *info,
1406                                         struct nvc_param *params)
1407 {
1408         if (copy_from_user(&info->config, (const void __user *)params->p_value,
1409                 sizeof(struct nv_focuser_config))) {
1410                 dev_err(&info->i2c_client->dev, "%s Error: copy_from_user bytes %d\n",
1411                                 __func__, sizeof(struct nv_focuser_config));
1412                 return -EFAULT;
1413         }
1414
1415         /* info.config.focuser_set[0].posture, macro, hyper, infinity and
1416          * hysterisis can remain there only. We need only settle_time &
1417          * slew_rate for use here.
1418          */
1419         info->cap.settle_time = info->config.focuser_set[0].settle_time;
1420         info->config.slew_rate = info->config.slew_rate;
1421
1422         dev_dbg(&info->i2c_client->dev, "%s: copy_from_user bytes %d\n",
1423                         __func__,  sizeof(struct nv_focuser_config));
1424         return 0;
1425 }
1426
1427
1428 static int sh532u_param_rd(struct sh532u_info *info, unsigned long arg)
1429 {
1430         struct nvc_param params;
1431         const void *data_ptr;
1432         u32 data_size = 0;
1433         s32 position;
1434         int err;
1435
1436         if (copy_from_user(&params,
1437                         (const void __user *)arg,
1438                         sizeof(struct nvc_param))) {
1439                 dev_err(&info->i2c_client->dev, "%s %d copy_from_user err\n",
1440                         __func__, __LINE__);
1441                 return -EFAULT;
1442         }
1443
1444         if (info->s_mode == NVC_SYNC_SLAVE)
1445                 info = info->s_info;
1446         switch (params.param) {
1447         case NVC_PARAM_LOCUS:
1448                 sh532u_pm_dev_wr(info, NVC_PWR_COMM);
1449                 err = sh532u_rel_pos_rd(info, &position);
1450                 if (err && !(info->pdata->cfg & NVC_CFG_NOERR))
1451                         return -EINVAL;
1452
1453                 data_ptr = &position;
1454                 data_size = sizeof(position);
1455                 sh532u_pm_dev_wr(info, NVC_PWR_STDBY);
1456                 dev_dbg(&info->i2c_client->dev, "%s LOCUS: %d\n",
1457                         __func__, position);
1458                 break;
1459
1460         case NVC_PARAM_FOCAL_LEN:
1461                 data_ptr = &info->nvc.focal_length;
1462                 data_size = sizeof(info->nvc.focal_length);
1463                 dev_dbg(&info->i2c_client->dev, "%s FOCAL_LEN: %x\n",
1464                         __func__, info->nvc.focal_length);
1465                 break;
1466
1467         case NVC_PARAM_MAX_APERTURE:
1468                 data_ptr = &info->nvc.max_aperature;
1469                 data_size = sizeof(info->nvc.max_aperature);
1470                 dev_dbg(&info->i2c_client->dev, "%s MAX_APERTURE: %x\n",
1471                         __func__, info->nvc.max_aperature);
1472                 break;
1473
1474         case NVC_PARAM_FNUMBER:
1475                 data_ptr = &info->nvc.fnumber;
1476                 data_size = sizeof(info->nvc.fnumber);
1477                 dev_dbg(&info->i2c_client->dev, "%s FNUMBER: %u\n",
1478                         __func__, info->nvc.fnumber);
1479                 break;
1480
1481         case NVC_PARAM_CAPS:
1482                 sh532u_pm_dev_wr(info, NVC_PWR_COMM);
1483                 err = sh532u_calibration(info, true);
1484                 sh532u_pm_dev_wr(info, NVC_PWR_STDBY);
1485                 if (err)
1486                         return -EIO;
1487                 dev_dbg(&info->i2c_client->dev, "%s: NVC_PARAM_CAPS: params.param %d "
1488                                 "params.sizeofvalue %d\n",
1489                                 __func__, params.param, params.sizeofvalue);
1490
1491                 sh532u_get_focuser_capabilities(info);
1492
1493                 data_ptr = &info->config;
1494                 data_size = sizeof(info->config);
1495                 break;
1496
1497         case NVC_PARAM_STS:
1498                 data_ptr = &info->sts;
1499                 data_size = sizeof(info->sts);
1500                 dev_dbg(&info->i2c_client->dev, "%s STS: %d\n",
1501                         __func__, info->sts);
1502                 break;
1503
1504         case NVC_PARAM_STEREO:
1505                 data_ptr = &info->s_mode;
1506                 data_size = sizeof(info->s_mode);
1507                 dev_dbg(&info->i2c_client->dev, "%s STEREO: %d\n",
1508                         __func__, info->s_mode);
1509                 break;
1510
1511         default:
1512                 dev_dbg(&info->i2c_client->dev,
1513                         "%s unsupported parameter: %d\n",
1514                         __func__, params.param);
1515                 return -EINVAL;
1516         }
1517
1518         if (params.sizeofvalue < data_size) {
1519                 dev_err(&info->i2c_client->dev,
1520                         "%s data size mismatch %d != %d Param: %d\n",
1521                         __func__, params.sizeofvalue, data_size, params.param);
1522                 return -EINVAL;
1523         }
1524
1525         if (copy_to_user((void __user *)params.p_value,
1526                          data_ptr,
1527                          data_size)) {
1528                 dev_err(&info->i2c_client->dev,
1529                         "%s copy_to_user err line %d\n", __func__, __LINE__);
1530                 return -EFAULT;
1531         }
1532
1533         return 0;
1534 }
1535
1536 static int sh532u_param_wr_s(struct sh532u_info *info,
1537                              struct nvc_param *params,
1538                              u32 u32val)
1539 {
1540         u8 u8val;
1541         int err;
1542
1543         u8val = (u8)u32val;
1544         switch (params->param) {
1545         case NVC_PARAM_LOCUS:
1546                 dev_dbg(&info->i2c_client->dev, "%s LOCUS: %d\n",
1547                         __func__, (s32) u32val);
1548                 err = sh532u_pos_rel_wr(info, (s32) u32val);
1549                 return err;
1550
1551         case NVC_PARAM_RESET:
1552                 err = sh532u_reset(info, u32val);
1553                 dev_dbg(&info->i2c_client->dev, "%s RESET: %d\n",
1554                         __func__, err);
1555                 return err;
1556
1557         case NVC_PARAM_SELF_TEST:
1558                 err = sh532u_hvca_pos_init(info);
1559                 dev_dbg(&info->i2c_client->dev, "%s SELF_TEST: %d\n",
1560                         __func__, err);
1561                 return err;
1562
1563         case NVC_PARAM_CAPS:
1564                 dev_dbg(&info->i2c_client->dev, "%s CAPS. Error. sh532u_param_wr "
1565                                 "should be called instead\n", __func__);
1566                 return -EFAULT;
1567
1568         default:
1569                 dev_dbg(&info->i2c_client->dev,
1570                         "%s unsupported parameter: %d\n",
1571                         __func__, params->param);
1572                 return -EINVAL;
1573         }
1574 }
1575
1576 static int sh532u_param_wr(struct sh532u_info *info, unsigned long arg)
1577 {
1578         struct nvc_param params;
1579         u8 u8val;
1580         u32 u32val;
1581         int err = 0;
1582
1583         if (copy_from_user(&params, (const void __user *)arg,
1584                            sizeof(struct nvc_param))) {
1585                 dev_err(&info->i2c_client->dev,
1586                         "%s copy_from_user err line %d\n", __func__, __LINE__);
1587                 return -EFAULT;
1588         }
1589
1590         if (copy_from_user(&u32val, (const void __user *)params.p_value,
1591                            sizeof(u32val))) {
1592                 dev_err(&info->i2c_client->dev, "%s %d copy_from_user err\n",
1593                         __func__, __LINE__);
1594                 return -EFAULT;
1595         }
1596
1597         u8val = (u8)u32val;
1598         /* parameters independent of sync mode */
1599         switch (params.param) {
1600         case NVC_PARAM_STEREO:
1601                 dev_dbg(&info->i2c_client->dev, "%s STEREO: %d\n",
1602                         __func__, u8val);
1603                 if (u8val == info->s_mode)
1604                         return 0;
1605
1606                 switch (u8val) {
1607                 case NVC_SYNC_OFF:
1608                         info->s_mode = u8val;
1609                         sh532u_gpio_wr(info, SH532U_GPIO_I2CMUX, 0);
1610                         if (info->s_info != NULL) {
1611                                 info->s_info->s_mode = u8val;
1612                                 sh532u_pm_wr(info->s_info, NVC_PWR_OFF);
1613                         }
1614                         break;
1615
1616                 case NVC_SYNC_MASTER:
1617                         info->s_mode = u8val;
1618                         sh532u_gpio_wr(info, SH532U_GPIO_I2CMUX, 0);
1619                         if (info->s_info != NULL)
1620                                 info->s_info->s_mode = u8val;
1621                         break;
1622
1623                 case NVC_SYNC_SLAVE:
1624                         if (info->s_info != NULL) {
1625                                 /* default slave lens position */
1626                                 err = sh532u_pos_rel_wr(info->s_info,
1627                                              info->s_info->cap.focus_infinity);
1628                                 if (!err) {
1629                                         info->s_mode = u8val;
1630                                         info->s_info->s_mode = u8val;
1631                                         sh532u_gpio_wr(info,
1632                                                        SH532U_GPIO_I2CMUX, 0);
1633                                 } else {
1634                                         if (info->s_mode != NVC_SYNC_STEREO)
1635                                                 sh532u_pm_wr(info->s_info,
1636                                                              NVC_PWR_OFF);
1637                                         err = -EIO;
1638                                 }
1639                         } else {
1640                                 err = -EINVAL;
1641                         }
1642                         break;
1643
1644                 case NVC_SYNC_STEREO:
1645                         if (info->s_info != NULL) {
1646                                 /* sync power */
1647                                 info->s_info->pwr_api = info->pwr_api;
1648                                 /* move slave lens to master position */
1649                                 err = sh532u_pos_rel_wr(info->s_info,
1650                                                          info->pos_rel);
1651                                 if (!err) {
1652                                         info->s_mode = u8val;
1653                                         info->s_info->s_mode = u8val;
1654                                         sh532u_gpio_wr(info,
1655                                                        SH532U_GPIO_I2CMUX, 1);
1656                                 } else {
1657                                         if (info->s_mode != NVC_SYNC_SLAVE)
1658                                                 sh532u_pm_wr(info->s_info,
1659                                                              NVC_PWR_OFF);
1660                                         err = -EIO;
1661                                 }
1662                         } else {
1663                                 err = -EINVAL;
1664                         }
1665                         break;
1666
1667                 default:
1668                         err = -EINVAL;
1669                 }
1670                 if (info->pdata->cfg & NVC_CFG_NOERR)
1671                         return 0;
1672
1673                 return err;
1674
1675         case NVC_PARAM_CAPS:
1676                 if (sh532u_set_focuser_capabilities(info, &params)) {
1677                         dev_err(&info->i2c_client->dev, "%s: Error: copy_from_user bytes %d\n",
1678                                         __func__, params.sizeofvalue);
1679                         return -EFAULT;
1680                 }
1681                 return 0;
1682
1683         default:
1684         /* parameters dependent on sync mode */
1685                 switch (info->s_mode) {
1686                 case NVC_SYNC_OFF:
1687                 case NVC_SYNC_MASTER:
1688                         return sh532u_param_wr_s(info, &params, u32val);
1689
1690                 case NVC_SYNC_SLAVE:
1691                         return sh532u_param_wr_s(info->s_info, &params,
1692                                                  u32val);
1693
1694                 case NVC_SYNC_STEREO:
1695                         err = sh532u_param_wr_s(info, &params, u32val);
1696                         if (!(info->pdata->cfg & NVC_CFG_SYNC_I2C_MUX))
1697                                 err |= sh532u_param_wr_s(info->s_info,
1698                                                          &params,
1699                                                          u32val);
1700                         return err;
1701
1702                 default:
1703                         dev_err(&info->i2c_client->dev, "%s %d internal err\n",
1704                                 __func__, __LINE__);
1705                         return -EINVAL;
1706                 }
1707         }
1708 }
1709
1710 static long sh532u_ioctl(struct file *file,
1711                          unsigned int cmd,
1712                          unsigned long arg)
1713 {
1714         struct sh532u_info *info = file->private_data;
1715         int pwr;
1716         int err;
1717
1718         switch (cmd) {
1719         case NVC_IOCTL_PARAM_WR:
1720                 err = sh532u_param_wr(info, arg);
1721                 return err;
1722
1723         case NVC_IOCTL_PARAM_RD:
1724                 err = sh532u_param_rd(info, arg);
1725                 return err;
1726
1727         case NVC_IOCTL_PWR_WR:
1728                 /* This is a Guaranteed Level of Service (GLOS) call */
1729                 pwr = (int)arg * 2;
1730                 dev_dbg(&info->i2c_client->dev, "%s PWR_WR: %d\n",
1731                         __func__, pwr);
1732                 err = sh532u_pm_api_wr(info, pwr);
1733                 return err;
1734
1735         case NVC_IOCTL_PWR_RD:
1736                 if (info->s_mode == NVC_SYNC_SLAVE)
1737                         pwr = info->s_info->pwr_api / 2;
1738                 else
1739                         pwr = info->pwr_api / 2;
1740                 dev_dbg(&info->i2c_client->dev, "%s PWR_RD: %d\n",
1741                         __func__, pwr);
1742                 if (copy_to_user((void __user *)arg, (const void *)&pwr,
1743                                  sizeof(pwr))) {
1744                         dev_err(&info->i2c_client->dev,
1745                                         "%s copy_to_user err line %d\n",
1746                                         __func__, __LINE__);
1747                         return -EFAULT;
1748                 }
1749
1750                 return 0;
1751
1752         default:
1753                 dev_dbg(&info->i2c_client->dev, "%s unsupported ioctl: %x\n",
1754                         __func__, cmd);
1755         }
1756
1757         return -EINVAL;
1758 }
1759
1760 static void sh532u_sdata_init(struct sh532u_info *info)
1761 {
1762         /* set defaults */
1763         memcpy(&info->cfg, &sh532u_default_info, sizeof(info->cfg));
1764         memcpy(&info->nvc, &sh532u_default_nvc, sizeof(info->nvc));
1765         memcpy(&info->cap, &sh532u_default_cap, sizeof(info->cap));
1766         if (info->pdata->i2c_addr_rom)
1767                 info->i2c_addr_rom = info->pdata->i2c_addr_rom;
1768         else
1769                 info->i2c_addr_rom = sh532u_default_pdata.i2c_addr_rom;
1770         /* set overrides if any */
1771         if (info->pdata->nvc) {
1772                 if (info->pdata->nvc->fnumber)
1773                         info->nvc.fnumber = info->pdata->nvc->fnumber;
1774                 if (info->pdata->nvc->focal_length)
1775                         info->nvc.focal_length =
1776                                         info->pdata->nvc->focal_length;
1777                 if (info->pdata->nvc->max_aperature)
1778                         info->nvc.max_aperature =
1779                                         info->pdata->nvc->max_aperature;
1780         }
1781         if (info->pdata->cap) {
1782                 if (info->pdata->cap->actuator_range)
1783                         info->cap.actuator_range =
1784                                         info->pdata->cap->actuator_range;
1785                 if (info->pdata->cap->settle_time)
1786                         info->cap.settle_time = info->pdata->cap->settle_time;
1787                 if (info->pdata->cap->focus_macro)
1788                         info->cap.focus_macro = info->pdata->cap->focus_macro;
1789                 if (info->pdata->cap->focus_hyper)
1790                         info->cap.focus_hyper = info->pdata->cap->focus_hyper;
1791                 if (info->pdata->cap->focus_infinity)
1792                         info->cap.focus_infinity =
1793                                         info->pdata->cap->focus_infinity;
1794         }
1795 }
1796
1797 static int sh532u_sync_en(unsigned num, unsigned sync)
1798 {
1799         struct sh532u_info *master = NULL;
1800         struct sh532u_info *slave = NULL;
1801         struct sh532u_info *pos = NULL;
1802
1803         rcu_read_lock();
1804         list_for_each_entry_rcu(pos, &sh532u_info_list, list) {
1805                 if (pos->pdata->num == num) {
1806                         master = pos;
1807                         break;
1808                 }
1809         }
1810         pos = NULL;
1811         list_for_each_entry_rcu(pos, &sh532u_info_list, list) {
1812                 if (pos->pdata->num == sync) {
1813                         slave = pos;
1814                         break;
1815                 }
1816         }
1817         rcu_read_unlock();
1818         if (master != NULL)
1819                 master->s_info = NULL;
1820         if (slave != NULL)
1821                 slave->s_info = NULL;
1822         if (!sync)
1823                 return 0; /* no err if sync disabled */
1824
1825         if (num == sync)
1826                 return -EINVAL; /* err if sync instance is itself */
1827
1828         if ((master != NULL) && (slave != NULL)) {
1829                 master->s_info = slave;
1830                 slave->s_info = master;
1831         }
1832         return 0;
1833 }
1834
1835 static int sh532u_sync_dis(struct sh532u_info *info)
1836 {
1837         if (info->s_info != NULL) {
1838                 info->s_info->s_mode = 0;
1839                 info->s_info->s_info = NULL;
1840                 info->s_mode = 0;
1841                 info->s_info = NULL;
1842                 return 0;
1843         }
1844
1845         return -EINVAL;
1846 }
1847
1848 static int sh532u_open(struct inode *inode, struct file *file)
1849 {
1850         struct sh532u_info *info = NULL;
1851         struct sh532u_info *pos = NULL;
1852         int err;
1853
1854         rcu_read_lock();
1855         list_for_each_entry_rcu(pos, &sh532u_info_list, list) {
1856                 if (pos->miscdev.minor == iminor(inode)) {
1857                         info = pos;
1858                         break;
1859                 }
1860         }
1861         rcu_read_unlock();
1862         if (!info)
1863                 return -ENODEV;
1864
1865         err = sh532u_sync_en(info->pdata->num, info->pdata->sync);
1866         if (err == -EINVAL)
1867                 dev_err(&info->i2c_client->dev,
1868                         "%s err: invalid num (%u) and sync (%u) instance\n",
1869                         __func__, info->pdata->num, info->pdata->sync);
1870         if (atomic_xchg(&info->in_use, 1))
1871                 return -EBUSY;
1872
1873         if (info->s_info != NULL) {
1874                 if (atomic_xchg(&info->s_info->in_use, 1))
1875                         return -EBUSY;
1876         }
1877
1878         file->private_data = info;
1879         dev_dbg(&info->i2c_client->dev, "%s\n", __func__);
1880         sh532u_pos_rel_wr(info, info->cap.focus_infinity);
1881         sh532u_pm_wr_s(info, NVC_PWR_OFF);
1882         return 0;
1883 }
1884
1885 static int sh532u_release(struct inode *inode, struct file *file)
1886 {
1887         struct sh532u_info *info = file->private_data;
1888
1889         dev_dbg(&info->i2c_client->dev, "%s\n", __func__);
1890         sh532u_pm_wr_s(info, NVC_PWR_OFF);
1891         file->private_data = NULL;
1892         WARN_ON(!atomic_xchg(&info->in_use, 0));
1893         if (info->s_info != NULL)
1894                 WARN_ON(!atomic_xchg(&info->s_info->in_use, 0));
1895         sh532u_sync_dis(info);
1896         return 0;
1897 }
1898
1899 static const struct file_operations sh532u_fileops = {
1900         .owner = THIS_MODULE,
1901         .open = sh532u_open,
1902         .unlocked_ioctl = sh532u_ioctl,
1903         .release = sh532u_release,
1904 };
1905
1906 static void sh532u_del(struct sh532u_info *info)
1907 {
1908         sh532u_pm_exit(info);
1909         if ((info->s_mode == NVC_SYNC_SLAVE) ||
1910                                              (info->s_mode == NVC_SYNC_STEREO))
1911                 sh532u_pm_exit(info->s_info);
1912         sh532u_sync_dis(info);
1913         spin_lock(&sh532u_spinlock);
1914         list_del_rcu(&info->list);
1915         spin_unlock(&sh532u_spinlock);
1916         synchronize_rcu();
1917 }
1918
1919 static int sh532u_remove(struct i2c_client *client)
1920 {
1921         struct sh532u_info *info = i2c_get_clientdata(client);
1922
1923         dev_dbg(&info->i2c_client->dev, "%s\n", __func__);
1924         misc_deregister(&info->miscdev);
1925         sh532u_del(info);
1926         return 0;
1927 }
1928
1929 static int sh532u_probe(
1930         struct i2c_client *client,
1931         const struct i2c_device_id *id)
1932 {
1933         struct sh532u_info *info;
1934         char dname[16];
1935         int err;
1936
1937         info = devm_kzalloc(&client->dev, sizeof(*info), GFP_KERNEL);
1938         if (info == NULL) {
1939                 dev_err(&client->dev, "%s: kzalloc error\n", __func__);
1940                 return -ENOMEM;
1941         }
1942
1943         info->i2c_client = client;
1944         if (client->dev.platform_data) {
1945                 info->pdata = client->dev.platform_data;
1946         } else {
1947                 info->pdata = &sh532u_default_pdata;
1948                 dev_dbg(&client->dev,
1949                         "%s No platform data.  Using defaults.\n", __func__);
1950         }
1951         i2c_set_clientdata(client, info);
1952         INIT_LIST_HEAD(&info->list);
1953         spin_lock(&sh532u_spinlock);
1954         list_add_rcu(&info->list, &sh532u_info_list);
1955         spin_unlock(&sh532u_spinlock);
1956         sh532u_pm_init(info);
1957         sh532u_sdata_init(info);
1958         if (info->pdata->cfg & (NVC_CFG_NODEV | NVC_CFG_BOOT_INIT)) {
1959                 err = sh532u_dev_id(info);
1960                 if (err < 0) {
1961                         if (info->pdata->cfg & NVC_CFG_NODEV) {
1962                                 sh532u_del(info);
1963                                 return -ENODEV;
1964                         } else {
1965                                 dev_err(&client->dev, "%s dev %x not found\n",
1966                                         __func__, SH532U_ID);
1967                         }
1968                 } else {
1969                         dev_dbg(&client->dev, "%s device found\n", __func__);
1970                         sh532u_pm_dev_wr(info, NVC_PWR_ON);
1971                         sh532u_calibration(info, false);
1972                         if (info->pdata->cfg & NVC_CFG_BOOT_INIT)
1973                                 /* initial move causes full initialization */
1974                                 sh532u_pos_rel_wr(info,
1975                                                   info->cap.focus_infinity);
1976                 }
1977                 sh532u_pm_dev_wr(info, NVC_PWR_OFF);
1978         }
1979
1980         if (info->pdata->dev_name != 0)
1981                 strcpy(dname, info->pdata->dev_name);
1982         else
1983                 strcpy(dname, "sh532u");
1984         if (info->pdata->num)
1985                 snprintf(dname, sizeof(dname), "%s.%u",
1986                          dname, info->pdata->num);
1987         info->miscdev.name = dname;
1988         info->miscdev.fops = &sh532u_fileops;
1989         info->miscdev.minor = MISC_DYNAMIC_MINOR;
1990         if (misc_register(&info->miscdev)) {
1991                 dev_err(&client->dev, "%s unable to register misc device %s\n",
1992                         __func__, dname);
1993                 sh532u_del(info);
1994                 return -ENODEV;
1995         }
1996
1997         return 0;
1998 }
1999
2000 static const struct i2c_device_id sh532u_id[] = {
2001         { "sh532u", 0 },
2002         { },
2003 };
2004
2005 MODULE_DEVICE_TABLE(i2c, sh532u_id);
2006
2007 static struct i2c_driver sh532u_i2c_driver = {
2008         .driver = {
2009                 .name = "sh532u",
2010                 .owner = THIS_MODULE,
2011         },
2012         .id_table = sh532u_id,
2013         .probe = sh532u_probe,
2014         .remove = sh532u_remove,
2015 };
2016
2017 static int __init sh532u_init(void)
2018 {
2019         return i2c_add_driver(&sh532u_i2c_driver);
2020 }
2021
2022 static void __exit sh532u_exit(void)
2023 {
2024         i2c_del_driver(&sh532u_i2c_driver);
2025 }
2026
2027 module_init(sh532u_init);
2028 module_exit(sh532u_exit);
2029 MODULE_LICENSE("GPL");