Revert "media: camera config changes"
[linux-2.6.git] / drivers / media / video / tegra / ssl3250a.c
1 /*
2  * ssl3250a.c - ssl3250a flash/torch kernel 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 ssl3250a_platform_data structure.
25  * Standard among NVC kernel drivers in this structure is:
26  * .cfg = Use the NVC_CFG_ defines that are in nvc_torch.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/ssl3250a.1
32  * .sync = If there is a need to synchronize two devices, then this value is
33  *         the number of the device instance this device is allowed to sync to.
34  *         This is typically used for stereo applications.
35  * .dev_name = The MISC driver name the device registers as.  If not used,
36  *             then the part number of the device is used for the driver name.
37  *             If using the NVC user driver then use the name found in this
38  *             driver under _default_pdata.
39  *
40  * The following is specific to NVC kernel flash/torch drivers:
41  * .pinstate = a pointer to the nvc_torch_pin_state structure.  This
42  *             structure gives the details of which VI GPIO to use to trigger
43  *             the flash.  The mask tells which pin and the values is the
44  *             level.  For example, if VI GPIO pin 6 is used, then
45  *             .mask = 0x0040
46  *             .values = 0x0040
47  *             If VI GPIO pin 0 is used, then
48  *             .mask = 0x0001
49  *             .values = 0x0001
50  *             This is typically just one pin but there is some legacy
51  *             here that insinuates more than one pin can be used.
52  *             When the flash level is set, then the driver will return the
53  *             value in values.  When the flash level is off, the driver will
54  *             return 0 for the values to deassert the signal.
55  *             If a VI GPIO is not used, then the mask and values must be set
56  *             to 0.  The flash may then be triggered via I2C instead.
57  *             However, a VI GPIO is strongly encouraged since it allows
58  *             tighter timing with the picture taken as well as reduced power
59  *             by asserting the trigger signal for only when needed.
60  * .max_amp_torch = Is the maximum torch value allowed.  The value is 0 to
61  *                  _MAX_TORCH_LEVEL.  This is to allow a limit to the amount
62  *                  of amps used.  If left blank then _MAX_TORCH_LEVEL will be
63  *                  used.
64  * .max_amp_flash = Is the maximum flash value allowed.  The value is 0 to
65  *                  _MAX_FLASH_LEVEL.  This is to allow a limit to the amount
66  *                  of amps used.  If left blank then _MAX_FLASH_LEVEL will be
67  *                  used.
68  *
69  * The following is specific to only this NVC kernel flash/torch driver:
70  * .gpio_act = Is the GPIO needed to control the ACT signal.  If tied high,
71  *             then this can be left blank.
72  *
73  * Power Requirements
74  * The board power file must contain the following labels for the power
75  * regulator(s) of this device:
76  * "vdd_i2c" = the power regulator for the I2C power.
77  * Note that this device is typically connected directly to the battery rail
78  * and does not need a source power regulator (vdd).
79  *
80  * The above values should be all that is needed to use the device with this
81  * driver.  Modifications of this driver should not be needed.
82  */
83
84
85 #include <linux/fs.h>
86 #include <linux/i2c.h>
87 #include <linux/miscdevice.h>
88 #include <linux/slab.h>
89 #include <linux/delay.h>
90 #include <linux/uaccess.h>
91 #include <linux/list.h>
92 #include <linux/regulator/consumer.h>
93 #include <linux/gpio.h>
94 #include <media/nvc.h>
95 #include <media/ssl3250a.h>
96
97 #define SSL3250A_REG_AMP        0x00
98 #define SSL3250A_REG_TMR        0x01
99 #define SSL3250A_REG_STRB       0x02
100 #define SSL3250A_REG_STS        0x03
101 #define ssl3250a_flash_cap_size (sizeof(ssl3250a_flash_cap.numberoflevels) \
102                                 + (sizeof(ssl3250a_flash_cap.levels[0]) \
103                                 * (SSL3250A_MAX_FLASH_LEVEL + 1)))
104 #define ssl3250a_torch_cap_size (sizeof(ssl3250a_torch_cap.numberoflevels) \
105                                 + (sizeof(ssl3250a_torch_cap.guidenum[0]) \
106                                 * (SSL3250A_MAX_TORCH_LEVEL + 1)))
107
108
109 static struct nvc_torch_flash_capabilities ssl3250a_flash_cap = {
110         SSL3250A_MAX_FLASH_LEVEL + 1,
111         {
112                 { 0,   0xFFFFFFFF, 0 },
113                 { 215, 820,        20 },
114                 { 230, 820,        20 },
115                 { 245, 820,        20 },
116                 { 260, 820,        20 },
117                 { 275, 820,        20 },
118                 { 290, 820,        20 },
119                 { 305, 820,        20 },
120                 { 320, 820,        20 },
121                 { 335, 820,        20 },
122                 { 350, 820,        20 },
123                 { 365, 820,        20 },
124                 { 380, 820,        20 },
125                 { 395, 820,        20 },
126                 { 410, 820,        20 },
127                 { 425, 820,        20 },
128                 { 440, 820,        20 },
129                 { 455, 820,        20 },
130                 { 470, 820,        20 },
131                 { 485, 820,        20 },
132                 { 500, 820,        20 }
133         }
134 };
135
136 static struct nvc_torch_torch_capabilities ssl3250a_torch_cap = {
137         SSL3250A_MAX_TORCH_LEVEL + 1,
138         {
139                 0,
140                 50,
141                 65,
142                 80,
143                 95,
144                 110,
145                 125,
146                 140,
147                 155,
148                 170,
149                 185,
150                 200
151         }
152 };
153
154 struct ssl3250a_info {
155         atomic_t in_use;
156         struct i2c_client *i2c_client;
157         struct ssl3250a_platform_data *pdata;
158         struct miscdevice miscdev;
159         struct list_head list;
160         int pwr_api;
161         int pwr_dev;
162         struct nvc_regulator vreg_i2c;
163         u8 s_mode;
164         struct ssl3250a_info *s_info;
165 };
166
167 static struct nvc_torch_pin_state ssl3250a_default_pinstate = {
168         .mask           = 0x0000,
169         .values         = 0x0000,
170 };
171
172 static struct ssl3250a_platform_data ssl3250a_default_pdata = {
173         .cfg            = 0,
174         .num            = 0,
175         .sync           = 0,
176         .dev_name       = "torch",
177         .pinstate       = &ssl3250a_default_pinstate,
178         .max_amp_torch  = SSL3250A_MAX_TORCH_LEVEL,
179         .max_amp_flash  = SSL3250A_MAX_FLASH_LEVEL,
180 };
181
182 static LIST_HEAD(ssl3250a_info_list);
183 static DEFINE_SPINLOCK(ssl3250a_spinlock);
184
185
186 static int ssl3250a_i2c_rd(struct ssl3250a_info *info, u8 reg, u8 *val)
187 {
188         struct i2c_msg msg[2];
189         u8 buf[2];
190
191         buf[0] = reg;
192         msg[0].addr = info->i2c_client->addr;
193         msg[0].flags = 0;
194         msg[0].len = 1;
195         msg[0].buf = &buf[0];
196         msg[1].addr = info->i2c_client->addr;
197         msg[1].flags = I2C_M_RD;
198         msg[1].len = 1;
199         msg[1].buf = &buf[1];
200         *val = 0;
201         if (i2c_transfer(info->i2c_client->adapter, msg, 2) != 2)
202                 return -EIO;
203
204         *val = buf[1];
205         return 0;
206 }
207
208 static int ssl3250a_i2c_wr(struct ssl3250a_info *info, u8 reg, u8 val)
209 {
210         struct i2c_msg msg;
211         u8 buf[2];
212
213         buf[0] = reg;
214         buf[1] = val;
215         msg.addr = info->i2c_client->addr;
216         msg.flags = 0;
217         msg.len = 2;
218         msg.buf = &buf[0];
219         if (i2c_transfer(info->i2c_client->adapter, &msg, 1) != 1)
220                 return -EIO;
221
222         return 0;
223 }
224
225 static void ssl3250a_gpio_act(struct ssl3250a_info *info, int val)
226 {
227         int prev_val;
228
229         if (info->pdata->gpio_act) {
230                 prev_val = gpio_get_value(info->pdata->gpio_act);
231                 if (val != prev_val) {
232                         gpio_set_value(info->pdata->gpio_act, val);
233                         if (val)
234                                 mdelay(1); /* delay for device startup */
235                 }
236         }
237 }
238
239 static void ssl3250a_pm_regulator_put(struct nvc_regulator *sreg)
240 {
241         regulator_put(sreg->vreg);
242         sreg->vreg = NULL;
243 }
244
245 static int ssl3250a_pm_regulator_get(struct ssl3250a_info *info,
246                                      struct nvc_regulator *sreg,
247                                      char vreg_name[])
248 {
249         int err = 0;
250
251         sreg->vreg_flag = 0;
252         sreg->vreg = regulator_get(&info->i2c_client->dev, vreg_name);
253         if (WARN_ON(IS_ERR(sreg->vreg))) {
254                 dev_err(&info->i2c_client->dev,
255                         "%s err for regulator: %s err: %d\n",
256                         __func__, vreg_name, (int)sreg->vreg);
257                 err = PTR_ERR(sreg->vreg);
258                 sreg->vreg = NULL;
259         } else {
260                 sreg->vreg_name = vreg_name;
261                 dev_dbg(&info->i2c_client->dev,
262                                 "%s vreg_name: %s\n",
263                                 __func__, sreg->vreg_name);
264         }
265         return err;
266 }
267
268 static int ssl3250a_pm_regulator_en(struct ssl3250a_info *info,
269                                     struct nvc_regulator *sreg)
270 {
271         int err = 0;
272
273         if (!sreg->vreg_flag && (sreg->vreg != NULL)) {
274                 err = regulator_enable(sreg->vreg);
275                 if (!err) {
276                         dev_dbg(&info->i2c_client->dev,
277                                         "%s vreg_name: %s\n",
278                                         __func__, sreg->vreg_name);
279                         sreg->vreg_flag = 1;
280                         err = 1; /* flag regulator state change */
281                 } else {
282                         dev_err(&info->i2c_client->dev,
283                                         "%s err, regulator: %s\n",
284                                         __func__, sreg->vreg_name);
285                 }
286         }
287         return err;
288 }
289
290 static int ssl3250a_pm_regulator_dis(struct ssl3250a_info *info,
291                                      struct nvc_regulator *sreg)
292 {
293         int err = 0;
294
295         if (sreg->vreg_flag && (sreg->vreg != NULL)) {
296                 err = regulator_disable(sreg->vreg);
297                 if (!err)
298                         dev_dbg(&info->i2c_client->dev,
299                                         "%s vreg_name: %s\n",
300                                         __func__, sreg->vreg_name);
301                 else
302                         dev_err(&info->i2c_client->dev,
303                                         "%s err, regulator: %s\n",
304                                         __func__, sreg->vreg_name);
305         }
306         sreg->vreg_flag = 0;
307         return err;
308 }
309
310 static int ssl3250a_pm_wr(struct ssl3250a_info *info, int pwr)
311 {
312         int err = 0;
313
314         if (pwr == info->pwr_dev)
315                 return 0;
316
317         switch (pwr) {
318         case NVC_PWR_OFF:
319                 if ((info->pdata->cfg & NVC_CFG_OFF2STDBY) ||
320                              (info->pdata->cfg & NVC_CFG_BOOT_INIT)) {
321                         pwr = NVC_PWR_STDBY;
322                 } else {
323                         ssl3250a_gpio_act(info, 0);
324                         err = ssl3250a_pm_regulator_dis(info, &info->vreg_i2c);
325                         break;
326                 }
327         case NVC_PWR_STDBY_OFF:
328                 if ((info->pdata->cfg & NVC_CFG_OFF2STDBY) ||
329                              (info->pdata->cfg & NVC_CFG_BOOT_INIT)) {
330                         pwr = NVC_PWR_STDBY;
331                 } else {
332                         ssl3250a_gpio_act(info, 0);
333                         err = ssl3250a_pm_regulator_en(info, &info->vreg_i2c);
334                         break;
335                 }
336         case NVC_PWR_STDBY:
337                 err = ssl3250a_pm_regulator_en(info, &info->vreg_i2c);
338                 ssl3250a_gpio_act(info, 1);
339                 err |= ssl3250a_i2c_wr(info, SSL3250A_REG_AMP, 0x00);
340                 break;
341
342         case NVC_PWR_COMM:
343         case NVC_PWR_ON:
344                 err = ssl3250a_pm_regulator_en(info, &info->vreg_i2c);
345                 ssl3250a_gpio_act(info, 1);
346                 break;
347
348         default:
349                 err = -EINVAL;
350                 break;
351         }
352
353         if (err < 0) {
354                 dev_err(&info->i2c_client->dev, "%s error\n", __func__);
355                 pwr = NVC_PWR_ERR;
356         }
357         info->pwr_dev = pwr;
358         if (err > 0)
359                 return 0;
360
361         return err;
362 }
363
364 static int ssl3250a_pm_wr_s(struct ssl3250a_info *info, int pwr)
365 {
366         int err1 = 0;
367         int err2 = 0;
368
369         if ((info->s_mode == NVC_SYNC_OFF) ||
370                         (info->s_mode == NVC_SYNC_MASTER) ||
371                         (info->s_mode == NVC_SYNC_STEREO))
372                 err1 = ssl3250a_pm_wr(info, pwr);
373         if ((info->s_mode == NVC_SYNC_SLAVE) ||
374                         (info->s_mode == NVC_SYNC_STEREO))
375                 err2 = ssl3250a_pm_wr(info->s_info, pwr);
376         return err1 | err2;
377 }
378
379 static int ssl3250a_pm_api_wr(struct ssl3250a_info *info, int pwr)
380 {
381         int err = 0;
382
383         if (!pwr || (pwr > NVC_PWR_ON))
384                 return 0;
385
386         if (pwr > info->pwr_dev)
387                 err = ssl3250a_pm_wr_s(info, pwr);
388         if (!err)
389                 info->pwr_api = pwr;
390         else
391                 info->pwr_api = NVC_PWR_ERR;
392         if (info->pdata->cfg & NVC_CFG_NOERR)
393                 return 0;
394
395         return err;
396 }
397
398 static int ssl3250a_pm_dev_wr(struct ssl3250a_info *info, int pwr)
399 {
400         if (pwr < info->pwr_api)
401                 pwr = info->pwr_api;
402         return ssl3250a_pm_wr(info, pwr);
403 }
404
405 static void ssl3250a_pm_exit(struct ssl3250a_info *info)
406 {
407         ssl3250a_pm_wr_s(info, NVC_PWR_OFF);
408         ssl3250a_pm_regulator_put(&info->vreg_i2c);
409 }
410
411 static void ssl3250a_pm_init(struct ssl3250a_info *info)
412 {
413         ssl3250a_pm_regulator_get(info, &info->vreg_i2c, "vdd_i2c");
414 }
415
416 static int ssl3250a_dev_id(struct ssl3250a_info *info)
417 {
418         u8 addr;
419         u8 reg;
420         int err;
421
422         ssl3250a_pm_dev_wr(info, NVC_PWR_COMM);
423         /* There isn't a device ID so we just check that all the registers
424          * equal their startup defaults, which in this case, is 0.
425          */
426         for (addr = 0; addr < SSL3250A_REG_STS; addr++) {
427                 err = ssl3250a_i2c_rd(info, addr, &reg);
428                 if (err) {
429                         break;
430                 } else {
431                         if (reg) {
432                                 err = -ENODEV;
433                                 break;
434                         }
435                 }
436         }
437         ssl3250a_pm_dev_wr(info, NVC_PWR_OFF);
438         return err;
439 }
440
441 static int ssl3250a_param_rd(struct ssl3250a_info *info, long arg)
442 {
443         struct nvc_param params;
444         struct nvc_torch_pin_state pinstate;
445         const void *data_ptr;
446         u32 data_size = 0;
447         int err;
448         u8 reg;
449
450         if (copy_from_user(&params,
451                         (const void __user *)arg,
452                         sizeof(struct nvc_param))) {
453                 dev_err(&info->i2c_client->dev, "%s %d copy_from_user err\n",
454                                 __func__, __LINE__);
455                 return -EINVAL;
456         }
457
458         if (info->s_mode == NVC_SYNC_SLAVE)
459                 info = info->s_info;
460         switch (params.param) {
461         case NVC_PARAM_FLASH_CAPS:
462                 dev_dbg(&info->i2c_client->dev, "%s FLASH_CAPS\n", __func__);
463                 data_ptr = &ssl3250a_flash_cap;
464                 data_size = ssl3250a_flash_cap_size;
465                 break;
466
467         case NVC_PARAM_FLASH_LEVEL:
468                 ssl3250a_pm_dev_wr(info, NVC_PWR_COMM);
469                 err = ssl3250a_i2c_rd(info, SSL3250A_REG_AMP, &reg);
470                 ssl3250a_pm_dev_wr(info, NVC_PWR_OFF);
471                 if (err < 0)
472                         return err;
473
474                 reg >>= 3; /*7:3=flash amps*/
475                 reg &= 0x1F; /*4:0=flash amps*/
476                 if (reg < 12) /*flash starts at 12*/
477                         reg = 0; /*<12=torch or off*/
478                 else
479                         reg -= 11; /*create flash index*/
480                 dev_dbg(&info->i2c_client->dev, "%s FLASH_LEVEL: %u\n",
481                             __func__,
482                             (unsigned)ssl3250a_flash_cap.levels[reg].guidenum);
483                 data_ptr = &ssl3250a_flash_cap.levels[reg].guidenum;
484                 data_size = sizeof(ssl3250a_flash_cap.levels[reg].guidenum);
485                 break;
486
487         case NVC_PARAM_TORCH_CAPS:
488                 dev_dbg(&info->i2c_client->dev, "%s TORCH_CAPS\n", __func__);
489                 data_ptr = &ssl3250a_torch_cap;
490                 data_size = ssl3250a_torch_cap_size;
491                 break;
492
493         case NVC_PARAM_TORCH_LEVEL:
494                 ssl3250a_pm_dev_wr(info, NVC_PWR_COMM);
495                 err = ssl3250a_i2c_rd(info, SSL3250A_REG_AMP, &reg);
496                 ssl3250a_pm_dev_wr(info, NVC_PWR_OFF);
497                 if (err < 0)
498                         return err;
499
500                 reg >>= 3; /*7:3=torch amps*/
501                 reg &= 0x1F; /*4:0=torch amps*/
502                 if (reg > 11) /*flash starts at 12*/
503                         reg = 0; /*>11=flash mode (torch off)*/
504                 dev_dbg(&info->i2c_client->dev, "%s TORCH_LEVEL: %u\n",
505                                 __func__,
506                                 (unsigned)ssl3250a_torch_cap.guidenum[reg]);
507                 data_ptr = &ssl3250a_torch_cap.guidenum[reg];
508                 data_size = sizeof(ssl3250a_torch_cap.guidenum[reg]);
509                 break;
510
511         case NVC_PARAM_FLASH_PIN_STATE:
512                 pinstate.mask = info->pdata->pinstate->mask;
513                 ssl3250a_pm_dev_wr(info, NVC_PWR_COMM);
514                 err = ssl3250a_i2c_rd(info, SSL3250A_REG_AMP, &reg);
515                 ssl3250a_pm_dev_wr(info, NVC_PWR_OFF);
516                 if (err < 0)
517                         return err;
518
519                 reg >>= 3; /*7:3=flash amps*/
520                 reg &= 0x1F; /*4:0=flash amps*/
521                 if (reg < 12) /*flash starts at 12*/
522                         pinstate.values = 0; /*deassert strobe*/
523                 else
524                         /*assert strobe*/
525                         pinstate.values = info->pdata->pinstate->values;
526                 dev_dbg(&info->i2c_client->dev, "%s FLASH_PIN_STATE: %x&%x\n",
527                                 __func__, pinstate.mask, pinstate.values);
528                 data_ptr = &pinstate;
529                 data_size = sizeof(struct nvc_torch_pin_state);
530                 break;
531
532         case NVC_PARAM_STEREO:
533                 dev_dbg(&info->i2c_client->dev, "%s STEREO: %d\n",
534                                 __func__, info->s_mode);
535                 data_ptr = &info->s_mode;
536                 data_size = sizeof(info->s_mode);
537                 break;
538
539         default:
540                 dev_err(&info->i2c_client->dev,
541                                 "%s unsupported parameter: %d\n",
542                                 __func__, params.param);
543                 return -EINVAL;
544         }
545
546         if (params.sizeofvalue < data_size) {
547                 dev_err(&info->i2c_client->dev,
548                                 "%s data size mismatch %d != %d\n",
549                                 __func__, params.sizeofvalue, data_size);
550                 return -EINVAL;
551         }
552
553         if (copy_to_user((void __user *)params.p_value,
554                          data_ptr,
555                          data_size)) {
556                 dev_err(&info->i2c_client->dev,
557                                 "%s copy_to_user err line %d\n",
558                                 __func__, __LINE__);
559                 return -EFAULT;
560         }
561
562         return 0;
563 }
564
565 static int ssl3250a_param_wr_s(struct ssl3250a_info *info,
566                                struct nvc_param *params,
567                                u8 val)
568 {
569         int err;
570
571         switch (params->param) {
572         case NVC_PARAM_FLASH_LEVEL:
573                 dev_dbg(&info->i2c_client->dev, "%s FLASH_LEVEL: %d\n",
574                                 __func__, val);
575                 ssl3250a_pm_dev_wr(info, NVC_PWR_ON);
576                 if (val > ssl3250a_default_pdata.max_amp_flash)
577                         val = ssl3250a_default_pdata.max_amp_flash;
578                 /*Amp limit values are in the board-sensors file.*/
579                 if (info->pdata->max_amp_flash &&
580                                 (val > info->pdata->max_amp_flash))
581                         val = info->pdata->max_amp_flash;
582                 if (val) {
583                         val += 11; /*flash starts at 12*/
584                         val <<= 3; /*7:3=flash/torch amps*/
585                 }
586                 err = ssl3250a_i2c_wr(info, SSL3250A_REG_AMP, val);
587                 if (!val) /*turn pwr off if no flash && no pwr_api*/
588                         ssl3250a_pm_dev_wr(info, NVC_PWR_OFF);
589                 return err;
590
591         case NVC_PARAM_TORCH_LEVEL:
592                 dev_dbg(&info->i2c_client->dev, "%s TORCH_LEVEL: %d\n",
593                                 __func__, val);
594                 ssl3250a_pm_dev_wr(info, NVC_PWR_ON);
595                 if (val > ssl3250a_default_pdata.max_amp_torch)
596                         val = ssl3250a_default_pdata.max_amp_torch;
597                 /*Amp limit values are in the board-sensors file.*/
598                 if (info->pdata->max_amp_torch &&
599                                 (val > info->pdata->max_amp_torch))
600                         val = info->pdata->max_amp_torch;
601                 if (val)
602                         val <<= 3; /*7:3=flash/torch amps*/
603                 err = ssl3250a_i2c_wr(info, SSL3250A_REG_AMP, val);
604                 if (!val) /*turn pwr off if no torch && no pwr_api*/
605                         ssl3250a_pm_dev_wr(info, NVC_PWR_OFF);
606                 return err;
607
608         case NVC_PARAM_FLASH_PIN_STATE:
609                 dev_dbg(&info->i2c_client->dev, "%s FLASH_PIN_STATE: %d\n",
610                                 __func__, val);
611                 if (val)
612                         val = 0x01; /*0:0=soft trigger*/
613                 return ssl3250a_i2c_wr(info, SSL3250A_REG_STRB, val);
614
615         default:
616                 dev_err(&info->i2c_client->dev,
617                                 "%s unsupported parameter: %d\n",
618                                 __func__, params->param);
619                 return -EINVAL;
620         }
621 }
622
623 static int ssl3250a_param_wr(struct ssl3250a_info *info, long arg)
624 {
625         struct nvc_param params;
626         u8 val;
627         int err = 0;
628
629         if (copy_from_user(&params,
630                                 (const void __user *)arg,
631                                 sizeof(struct nvc_param))) {
632                 dev_err(&info->i2c_client->dev, "%s %d copy_from_user err\n",
633                                 __func__, __LINE__);
634                 return -EINVAL;
635         }
636
637         if (copy_from_user(&val, (const void __user *)params.p_value,
638                            sizeof(val))) {
639                 dev_err(&info->i2c_client->dev, "%s %d copy_from_user err\n",
640                                 __func__, __LINE__);
641                 return -EINVAL;
642         }
643
644         /* parameters independent of sync mode */
645         switch (params.param) {
646         case NVC_PARAM_STEREO:
647                 dev_dbg(&info->i2c_client->dev, "%s STEREO: %d\n",
648                                 __func__, (int)val);
649                 if (val == info->s_mode)
650                         return 0;
651
652                 switch (val) {
653                 case NVC_SYNC_OFF:
654                         info->s_mode = val;
655                         if (info->s_info != NULL) {
656                                 info->s_info->s_mode = val;
657                                 ssl3250a_pm_wr(info->s_info, NVC_PWR_OFF);
658                         }
659                         break;
660
661                 case NVC_SYNC_MASTER:
662                         info->s_mode = val;
663                         if (info->s_info != NULL)
664                                 info->s_info->s_mode = val;
665                         break;
666
667                 case NVC_SYNC_SLAVE:
668                 case NVC_SYNC_STEREO:
669                         if (info->s_info != NULL) {
670                                 /* sync power */
671                                 info->s_info->pwr_api = info->pwr_api;
672                                 err = ssl3250a_pm_wr(info->s_info,
673                                                      info->pwr_dev);
674                                 if (!err) {
675                                         info->s_mode = val;
676                                         info->s_info->s_mode = val;
677                                 } else {
678                                         ssl3250a_pm_wr(info->s_info,
679                                                        NVC_PWR_OFF);
680                                         err = -EIO;
681                                 }
682                         } else {
683                                 err = -EINVAL;
684                         }
685                         break;
686
687                 default:
688                         err = -EINVAL;
689                 }
690                 if (info->pdata->cfg & NVC_CFG_NOERR)
691                         return 0;
692
693                 return err;
694
695         default:
696         /* parameters dependent on sync mode */
697                 switch (info->s_mode) {
698                 case NVC_SYNC_OFF:
699                 case NVC_SYNC_MASTER:
700                         return ssl3250a_param_wr_s(info, &params, val);
701
702                 case NVC_SYNC_SLAVE:
703                         return ssl3250a_param_wr_s(info->s_info,
704                                                  &params,
705                                                  val);
706
707                 case NVC_SYNC_STEREO:
708                         err = ssl3250a_param_wr_s(info, &params, val);
709                         if (!(info->pdata->cfg & NVC_CFG_SYNC_I2C_MUX))
710                                 err |= ssl3250a_param_wr_s(info->s_info,
711                                                          &params,
712                                                          val);
713                         return err;
714
715                 default:
716                         dev_err(&info->i2c_client->dev, "%s %d internal err\n",
717                                         __func__, __LINE__);
718                         return -EINVAL;
719                 }
720         }
721 }
722
723 static long ssl3250a_ioctl(struct file *file,
724                            unsigned int cmd,
725                            unsigned long arg)
726 {
727         struct ssl3250a_info *info = file->private_data;
728         int pwr;
729
730         switch (cmd) {
731         case NVC_IOCTL_PARAM_WR:
732                 return ssl3250a_param_wr(info, arg);
733
734         case NVC_IOCTL_PARAM_RD:
735                 return ssl3250a_param_rd(info, arg);
736
737         case NVC_IOCTL_PWR_WR:
738                 /* This is a Guaranteed Level of Service (GLOS) call */
739                 pwr = (int)arg * 2;
740                 dev_dbg(&info->i2c_client->dev, "%s PWR_WR: %d\n",
741                                 __func__, pwr);
742                 return ssl3250a_pm_api_wr(info, pwr);
743
744         case NVC_IOCTL_PWR_RD:
745                 if (info->s_mode == NVC_SYNC_SLAVE)
746                         pwr = info->s_info->pwr_api / 2;
747                 else
748                         pwr = info->pwr_api / 2;
749                 dev_dbg(&info->i2c_client->dev, "%s PWR_RD: %d\n",
750                                 __func__, pwr);
751                 if (copy_to_user((void __user *)arg, (const void *)&pwr,
752                                  sizeof(pwr))) {
753                         dev_err(&info->i2c_client->dev,
754                                         "%s copy_to_user err line %d\n",
755                                         __func__, __LINE__);
756                         return -EFAULT;
757                 }
758
759                 return 0;
760
761         default:
762                 dev_err(&info->i2c_client->dev, "%s unsupported ioctl: %x\n",
763                                 __func__, cmd);
764                 return -EINVAL;
765         }
766 }
767
768 static int ssl3250a_sync_en(int dev1, int dev2)
769 {
770         struct ssl3250a_info *sync1 = NULL;
771         struct ssl3250a_info *sync2 = NULL;
772         struct ssl3250a_info *pos = NULL;
773
774         rcu_read_lock();
775         list_for_each_entry_rcu(pos, &ssl3250a_info_list, list) {
776                 if (pos->pdata->num == dev1) {
777                         sync1 = pos;
778                         break;
779                 }
780         }
781         pos = NULL;
782         list_for_each_entry_rcu(pos, &ssl3250a_info_list, list) {
783                 if (pos->pdata->num == dev2) {
784                         sync2 = pos;
785                         break;
786                 }
787         }
788         rcu_read_unlock();
789         if (sync1 != NULL)
790                 sync1->s_info = NULL;
791         if (sync2 != NULL)
792                 sync2->s_info = NULL;
793         if (!dev1 && !dev2)
794                 return 0; /* no err if default instance 0's used */
795
796         if (dev1 == dev2)
797                 return -EINVAL; /* err if sync instance is itself */
798
799         if ((sync1 != NULL) && (sync2 != NULL)) {
800                 sync1->s_info = sync2;
801                 sync2->s_info = sync1;
802         }
803
804         return 0;
805 }
806
807 static int ssl3250a_sync_dis(struct ssl3250a_info *info)
808 {
809         if (info->s_info != NULL) {
810                 info->s_info->s_mode = 0;
811                 info->s_info->s_info = NULL;
812                 info->s_mode = 0;
813                 info->s_info = NULL;
814                 return 0;
815         }
816
817         return -EINVAL;
818 }
819
820 static int ssl3250a_open(struct inode *inode, struct file *file)
821 {
822         struct ssl3250a_info *info = NULL;
823         struct ssl3250a_info *pos = NULL;
824         int err;
825
826         rcu_read_lock();
827         list_for_each_entry_rcu(pos, &ssl3250a_info_list, list) {
828                 if (pos->miscdev.minor == iminor(inode)) {
829                         info = pos;
830                         break;
831                 }
832         }
833         rcu_read_unlock();
834         if (!info)
835                 return -ENODEV;
836
837         err = ssl3250a_sync_en(info->pdata->num, info->pdata->sync);
838         if (err == -EINVAL)
839                 dev_err(&info->i2c_client->dev,
840                          "%s err: invalid num (%u) and sync (%u) instance\n",
841                          __func__, info->pdata->num, info->pdata->sync);
842         if (atomic_xchg(&info->in_use, 1))
843                 return -EBUSY;
844
845         if (info->s_info != NULL) {
846                 if (atomic_xchg(&info->s_info->in_use, 1))
847                         return -EBUSY;
848         }
849
850         file->private_data = info;
851         dev_dbg(&info->i2c_client->dev, "%s\n", __func__);
852         return 0;
853 }
854
855 static int ssl3250a_release(struct inode *inode, struct file *file)
856 {
857         struct ssl3250a_info *info = file->private_data;
858
859         dev_dbg(&info->i2c_client->dev, "%s\n", __func__);
860         ssl3250a_pm_wr_s(info, NVC_PWR_OFF);
861         file->private_data = NULL;
862         WARN_ON(!atomic_xchg(&info->in_use, 0));
863         if (info->s_info != NULL)
864                 WARN_ON(!atomic_xchg(&info->s_info->in_use, 0));
865         ssl3250a_sync_dis(info);
866         return 0;
867 }
868
869 static const struct file_operations ssl3250a_fileops = {
870         .owner = THIS_MODULE,
871         .open = ssl3250a_open,
872         .unlocked_ioctl = ssl3250a_ioctl,
873         .release = ssl3250a_release,
874 };
875
876 static void ssl3250a_del(struct ssl3250a_info *info)
877 {
878         ssl3250a_pm_exit(info);
879         ssl3250a_sync_dis(info);
880         spin_lock(&ssl3250a_spinlock);
881         list_del_rcu(&info->list);
882         spin_unlock(&ssl3250a_spinlock);
883         synchronize_rcu();
884 }
885
886 static int ssl3250a_remove(struct i2c_client *client)
887 {
888         struct ssl3250a_info *info = i2c_get_clientdata(client);
889
890         dev_dbg(&info->i2c_client->dev, "%s\n", __func__);
891         misc_deregister(&info->miscdev);
892         ssl3250a_del(info);
893         return 0;
894 }
895
896 static int ssl3250a_probe(
897         struct i2c_client *client,
898         const struct i2c_device_id *id)
899 {
900         struct ssl3250a_info *info;
901         char dname[16];
902         int err;
903
904         dev_dbg(&client->dev, "%s\n", __func__);
905         info = devm_kzalloc(&client->dev, sizeof(*info), GFP_KERNEL);
906         if (info == NULL) {
907                 dev_err(&client->dev, "%s: kzalloc error\n", __func__);
908                 return -ENOMEM;
909         }
910
911         info->i2c_client = client;
912         if (client->dev.platform_data) {
913                 info->pdata = client->dev.platform_data;
914         } else {
915                 info->pdata = &ssl3250a_default_pdata;
916                 dev_dbg(&client->dev,
917                                 "%s No platform data.  Using defaults.\n",
918                                 __func__);
919         }
920         i2c_set_clientdata(client, info);
921         INIT_LIST_HEAD(&info->list);
922         spin_lock(&ssl3250a_spinlock);
923         list_add_rcu(&info->list, &ssl3250a_info_list);
924         spin_unlock(&ssl3250a_spinlock);
925         ssl3250a_pm_init(info);
926         err = ssl3250a_dev_id(info);
927         if (err < 0) {
928                 dev_err(&client->dev, "%s device not found\n", __func__);
929                 if (info->pdata->cfg & NVC_CFG_NODEV) {
930                         ssl3250a_del(info);
931                         return -ENODEV;
932                 }
933         } else {
934                 dev_dbg(&client->dev, "%s device found\n", __func__);
935         }
936
937         if (info->pdata->dev_name != 0)
938                 strcpy(dname, info->pdata->dev_name);
939         else
940                 strcpy(dname, "ssl3250a");
941         if (info->pdata->num)
942                 snprintf(dname, sizeof(dname), "%s.%u",
943                          dname, info->pdata->num);
944         info->miscdev.name = dname;
945         info->miscdev.fops = &ssl3250a_fileops;
946         info->miscdev.minor = MISC_DYNAMIC_MINOR;
947         if (misc_register(&info->miscdev)) {
948                 dev_err(&client->dev, "%s unable to register misc device %s\n",
949                                 __func__, dname);
950                 ssl3250a_del(info);
951                 return -ENODEV;
952         }
953
954         return 0;
955 }
956
957 static const struct i2c_device_id ssl3250a_id[] = {
958         { "ssl3250a", 0 },
959         { },
960 };
961
962 MODULE_DEVICE_TABLE(i2c, ssl3250a_id);
963
964 static struct i2c_driver ssl3250a_i2c_driver = {
965         .driver = {
966                 .name = "ssl3250a",
967                 .owner = THIS_MODULE,
968         },
969         .id_table = ssl3250a_id,
970         .probe = ssl3250a_probe,
971         .remove = ssl3250a_remove,
972 };
973
974 static int __init ssl3250a_init(void)
975 {
976         return i2c_add_driver(&ssl3250a_i2c_driver);
977 }
978
979 static void __exit ssl3250a_exit(void)
980 {
981         i2c_del_driver(&ssl3250a_i2c_driver);
982 }
983
984 module_init(ssl3250a_init);
985 module_exit(ssl3250a_exit);
986 MODULE_LICENSE("GPL");