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