46303ebe1dea2e9354b8dbe4a313013cdda7f6da
[linux-3.10.git] / drivers / media / platform / tegra / cam_dev / virtual.c
1 /*
2  * virtual.c - Virtual kernel driver
3  *
4  * Copyright (c) 2013-2016, NVIDIA CORPORATION. All rights reserved.
5
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms and conditions of the GNU General Public License,
8  * version 2, as published by the Free Software Foundation.
9
10  * This program is distributed in the hope it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #define CAMERA_DEVICE_INTERNAL
20
21 #include <linux/kernel.h>
22 #include <linux/slab.h>
23 #include <linux/err.h>
24 #include <linux/uaccess.h>
25 #include <linux/delay.h>
26 #include <linux/gpio.h>
27 #include <linux/module.h>
28 #include <linux/of_device.h>
29 #include <linux/clk.h>
30
31 #include <media/nvc.h>
32 #include <media/camera.h>
33 #include "camera_common.h"
34
35 struct chip_config {
36         int clk_num;
37         int gpio_num;
38 };
39
40 static int virtual_update(
41         struct camera_device *cdev, struct cam_update *upd, int num)
42 {
43         int err = 0;
44         int idx;
45
46         dev_dbg(cdev->dev, "%s %d\n", __func__, num);
47         mutex_lock(&cdev->mutex);
48         for (idx = 0; idx < num; idx++) {
49                 switch (upd[idx].type) {
50                 case UPDATE_EDP:
51                 {
52                         dev_dbg(cdev->dev, "%s UPDATE_EDP is deprecated\n",
53                                 __func__);
54                         break;
55                 }
56                 case UPDATE_CLOCK:
57                 {
58                         struct clk *ck;
59                         u8 buf[CAMERA_MAX_NAME_LENGTH];
60
61                         if (!cdev->num_clk) {
62                                 dev_err(cdev->dev, "NO clock needed.\n");
63                                 err = -ENODEV;
64                                 break;
65                         }
66                         if (upd[idx].index >= cdev->num_clk) {
67                                 dev_err(cdev->dev,
68                                         "clock index %d out of range.\n",
69                                         upd[idx].index);
70                                 err = -ENODEV;
71                                 break;
72                         }
73
74                         memset(buf, 0, sizeof(buf));
75                         if (copy_from_user(buf,
76                                 ((const void __user *)
77                                 (unsigned long)upd[idx].arg),
78                                 sizeof(buf) - 1 < upd[idx].size ?
79                                 sizeof(buf) - 1 : upd[idx].size)) {
80                                 dev_err(cdev->dev,
81                                         "%s copy_from_user err line %d\n",
82                                         __func__, __LINE__);
83                                 err = -EFAULT;
84                                 break;
85                         }
86
87                         dev_dbg(cdev->dev, "%s UPDATE_CLOCK %d of %d, %s\n",
88                                 __func__, upd[idx].index, cdev->num_clk, buf);
89                         ck = devm_clk_get(cdev->dev, buf);
90                         if (IS_ERR(ck)) {
91                                 dev_err(cdev->dev, "%s: get clock %s FAILED.\n",
92                                         __func__, buf);
93                                 return PTR_ERR(ck);
94                         }
95                         cdev->clks[upd[idx].index] = ck;
96                         dev_dbg(cdev->dev, "UPDATE_CLOCK: %d %s\n",
97                                 upd[idx].index, buf);
98                         break;
99                 }
100                 case UPDATE_PINMUX:
101                 {
102                         break;
103                 }
104                 case UPDATE_GPIO:
105                 {
106                         dev_dbg(cdev->dev, "%s UPDATE_GPIO is deprecated\n",
107                                 __func__);
108                         break;
109                 }
110                 default:
111                         dev_err(cdev->dev,
112                                 "unsupported upd type %d\n", upd[idx].type);
113                         break;
114                 }
115
116                 if (err)
117                         break;
118         }
119         mutex_unlock(&cdev->mutex);
120         return err;
121 }
122
123 static int virtual_power_on(struct camera_device *cdev)
124 {
125         struct camera_reg *pwr_seq = cdev->seq_power_on;
126         int err = 0;
127
128         dev_dbg(cdev->dev, "%s %x %p\n",
129                 __func__, cdev->is_power_on, pwr_seq);
130         if (cdev->is_power_on || !pwr_seq)
131                 return 0;
132
133         mutex_lock(&cdev->mutex);
134         err = camera_dev_wr_table(cdev, pwr_seq, NULL);
135         if (!err)
136                 cdev->is_power_on = 1;
137         mutex_unlock(&cdev->mutex);
138         return err;
139 }
140
141 static int virtual_power_off(struct camera_device *cdev)
142 {
143         struct camera_reg *pwr_seq = cdev->seq_power_off;
144         int err = 0;
145
146         dev_dbg(cdev->dev, "%s %x %p\n",
147                 __func__, cdev->is_power_on, pwr_seq);
148         if (!cdev->is_power_on || !pwr_seq)
149                 return 0;
150
151         mutex_lock(&cdev->mutex);
152         err = camera_dev_wr_table(cdev, pwr_seq, NULL);
153         if (!err)
154                 cdev->is_power_on = 0;
155         mutex_unlock(&cdev->mutex);
156
157         return err;
158 }
159
160 static int virtual_shutdown(struct camera_device *cdev)
161 {
162         int err = 0;
163
164         dev_dbg(cdev->dev, "%s %x\n", __func__, cdev->is_power_on);
165         if (!cdev->is_power_on)
166                 return 0;
167
168         if (!err)
169                 err = virtual_power_off(cdev);
170
171         return err;
172 }
173
174 static int virtual_instance_destroy(struct camera_device *cdev)
175 {
176         void *buf;
177         u32 idx;
178
179         dev_dbg(cdev->dev, "%s\n", __func__);
180
181         if (!cdev->dev)
182                 return 0;
183
184         buf = dev_get_drvdata(cdev->dev);
185         dev_set_drvdata(cdev->dev, NULL);
186
187         for (idx = 0; idx < cdev->num_reg; idx++)
188                 if (likely(cdev->regs[idx].vreg))
189                         regulator_put(cdev->regs[idx].vreg);
190
191         cdev->num_gpio = 0;
192         cdev->num_reg = 0;
193         kfree(buf);
194         return 0;
195 }
196
197 static int virtual_instance_create(struct camera_device *cdev, void *pdata)
198 {
199         struct chip_config *c_info = cdev->chip->private;
200         struct camera_reg *pwr_seq = NULL;
201         const char *reg_names[8], *clks;
202         void *buf;
203         u32 idx, pwron_size, pwroff_size, reg_num, clk_num;
204
205         dev_dbg(cdev->dev, "%s\n", __func__);
206         memset(reg_names, 0 , sizeof(reg_names));
207         if (cdev->of_node) {
208                 pwr_seq = of_camera_get_pwrseq(
209                         cdev->dev, cdev->of_node, &pwron_size, &pwroff_size);
210                 reg_num = of_camera_get_regulators(cdev->dev, cdev->of_node,
211                         reg_names, sizeof(reg_names) / sizeof(reg_names[0]));
212                 clks = of_camera_get_clocks(cdev->dev, cdev->of_node, &clk_num);
213         } else {
214                 pwron_size = 0;
215                 pwroff_size = 0;
216                 reg_num = 0;
217                 clks = NULL;
218                 clk_num = 0;
219         }
220
221         buf = kzalloc(c_info->gpio_num * sizeof(*cdev->gpios) +
222                 reg_num * sizeof(*cdev->regs) +
223                 c_info->clk_num * sizeof(*cdev->clks) +
224                 (pwron_size + pwroff_size) * sizeof(*pwr_seq),
225                 GFP_KERNEL);
226         if (buf == NULL) {
227                 dev_err(cdev->dev, "%s memory low!\n", __func__);
228                 of_camera_put_pwrseq(cdev->dev, pwr_seq);
229                 return -ENOMEM;
230         }
231
232         cdev->gpios = buf;
233         cdev->regs = (void *)(cdev->gpios + c_info->gpio_num);
234         cdev->clks = (void *)(cdev->regs + reg_num);
235         cdev->num_gpio = c_info->gpio_num;
236         cdev->num_reg = reg_num;
237         cdev->num_clk = c_info->clk_num;
238         cdev->mclk_enable_idx = CAMDEV_INVALID;
239         cdev->mclk_disable_idx = CAMDEV_INVALID;
240         if (pdata) {
241                 cdev->pinmux_num =
242                         ((struct camera_platform_data *)pdata)->pinmux_num;
243                 cdev->pinmux_tbl =
244                         ((struct camera_platform_data *)pdata)->pinmux;
245         }
246
247         for (idx = 0; idx < cdev->num_gpio; idx++)
248                 cdev->gpios[idx].valid = false;
249
250         for (idx = 0; idx < cdev->num_reg; idx++) {
251                 cdev->regs[idx].vreg_name = (void *)reg_names[idx];
252                 camera_regulator_get(cdev->dev, &cdev->regs[idx],
253                         (char *)cdev->regs[idx].vreg_name);
254         }
255
256         for (idx = 0; idx < cdev->num_clk && idx < clk_num; idx++) {
257                 if (clks == NULL)
258                         break;
259                 cdev->clks[idx] = devm_clk_get(cdev->dev, clks);
260                 if (IS_ERR(cdev->clks[idx])) {
261                         dev_err(cdev->dev, "%s: get clock %s FAILED.\n",
262                                         __func__, clks);
263                         of_camera_put_pwrseq(cdev->dev, pwr_seq);
264                         kfree(buf);
265                         cdev->gpios = buf = NULL;
266                         return PTR_ERR(cdev->clks[idx]);
267                 }
268                 dev_dbg(cdev->dev, "%s - clock: %s\n", __func__, clks);
269                 clks += strlen(clks) + 1;
270         }
271
272         dev_set_drvdata(cdev->dev, cdev->gpios);
273
274         if (pwr_seq) {
275                 cdev->seq_power_on = (void *)(cdev->clks + c_info->clk_num);
276                 cdev->seq_power_off = cdev->seq_power_on + pwron_size;
277                 memcpy(cdev->seq_power_on, pwr_seq,
278                         (pwron_size + pwroff_size) * sizeof(*pwr_seq));
279
280                 dev_dbg(cdev->dev, "%s power_on: %p %u\n",
281                         __func__, cdev->seq_power_on, pwron_size);
282                 for (idx = 0; idx < pwron_size; idx++) {
283                         dev_dbg(cdev->dev, "    %08x - %08x\n",
284                                 cdev->seq_power_on[idx].addr,
285                                 cdev->seq_power_on[idx].val);
286                 }
287                 dev_dbg(cdev->dev, "%s power_off: %p %u\n",
288                         __func__, cdev->seq_power_off, pwroff_size);
289                 for (idx = 0; idx < pwroff_size; idx++) {
290                         dev_dbg(cdev->dev, "    %08x - %08x\n",
291                                 cdev->seq_power_off[idx].addr,
292                                 cdev->seq_power_off[idx].val);
293                 }
294         }
295         of_camera_put_pwrseq(cdev->dev, pwr_seq);
296         return 0;
297 }
298
299 static struct regmap_config regmap_cfg_default = {
300         .reg_bits = 16,
301         .val_bits = 8,
302         .cache_type = REGCACHE_NONE,
303 };
304
305 static int virtual_device_sanity_check(
306         struct device *dev, struct virtual_device *pvd, int *len)
307 {
308         u32 num;
309         u8 *nptr;
310         int n;
311
312         dev_dbg(dev, "%s: %s, bus type %d, addr bits %d, val bits %d\n",
313                 __func__, pvd->name, pvd->bus_type,
314                 pvd->regmap_cfg.addr_bits, pvd->regmap_cfg.val_bits);
315         dev_dbg(dev, "gpios %d, regs %d, clks %d\n",
316                 pvd->gpio_num, pvd->reg_num, pvd->clk_num);
317         if (pvd->name[0] == '\0') {
318                 dev_err(dev, "%s need a device name!\n", __func__);
319                 return -ENODEV;
320         }
321
322         if (pvd->bus_type != CAMERA_DEVICE_TYPE_I2C) {
323                 dev_err(dev, "%s unsupported device type %d!\n",
324                 __func__, pvd->bus_type);
325                 return -ENODEV;
326         }
327
328         if (pvd->regmap_cfg.addr_bits != 0 &&
329                 pvd->regmap_cfg.addr_bits != 8 &&
330                 pvd->regmap_cfg.addr_bits != 16) {
331                 dev_err(dev, "%s unsupported address bits %d!\n",
332                 __func__, pvd->regmap_cfg.addr_bits);
333                 return -ENODEV;
334         }
335
336         if (pvd->regmap_cfg.val_bits != 8 &&
337                 pvd->regmap_cfg.val_bits != 16) {
338                 dev_err(dev, "%s unsupported data bits %d!\n",
339                 __func__, pvd->regmap_cfg.val_bits);
340                 return -ENODEV;
341         }
342
343         if (pvd->regmap_cfg.cache_type != REGCACHE_NONE &&
344                 pvd->regmap_cfg.cache_type != REGCACHE_RBTREE &&
345                 pvd->regmap_cfg.cache_type != REGCACHE_COMPRESSED) {
346                 dev_err(dev, "%s unsupported cache type %d!\n",
347                 __func__, pvd->regmap_cfg.cache_type);
348                 return -ENODEV;
349         }
350
351         if (pvd->gpio_num >= ARCH_NR_GPIOS) {
352                 dev_err(dev, "%s too many gpios %d!\n",
353                 __func__, pvd->gpio_num);
354                 return -ENODEV;
355         }
356
357         if (pvd->gpio_num >= 6) {
358                 dev_notice(dev, "%s WHAT?! Are you sure you need %d gpios?\n",
359                         __func__, pvd->gpio_num);
360         }
361
362         if (pvd->clk_num >= 5) {
363                 dev_notice(dev, "%s WHAT?! Are you sure you need %d clocks?\n",
364                         __func__, pvd->clk_num);
365         }
366
367         *len = 0;
368         num = pvd->reg_num;
369         nptr = &pvd->reg_names[0];
370         while (num) {
371                 n = strlen(nptr);
372                 if (!n) {
373                         dev_err(dev, "%s NULL reg name @ %d\n",
374                                 __func__, pvd->reg_num - num);
375                         return -ENODEV;
376                 }
377                 *len += n + 1;
378                 nptr += CAMERA_MAX_NAME_LENGTH;
379                 num--;
380         }
381         dev_dbg(dev, "regulator name size: %d\n", *len);
382
383         if (pvd->pwr_on_size > VIRTUAL_DEV_MAX_POWER_SIZE) {
384                 dev_err(dev, "%s power on function size too big %d!\n",
385                 __func__, pvd->pwr_on_size);
386                 return -ENODEV;
387         }
388
389         if (pvd->pwr_off_size > VIRTUAL_DEV_MAX_POWER_SIZE) {
390                 dev_err(dev, "%s power off function size too big %d!\n",
391                 __func__, pvd->pwr_off_size);
392                 return -ENODEV;
393         }
394
395         return 0;
396 }
397
398 static int virtual_chip_config(
399         struct device *dev,
400         struct virtual_device *pvd,
401         struct chip_config *c_info)
402 {
403         dev_dbg(dev, "%s regulators:\n", __func__);
404         c_info->clk_num = pvd->clk_num;
405         c_info->gpio_num = pvd->gpio_num;
406
407         return 0;
408 }
409
410 void *virtual_chip_add(struct device *dev, struct virtual_device *pvd)
411 {
412         struct camera_chip *v_chip;
413         struct chip_config *c_info;
414         struct regmap_config *p_regmap;
415         int buf_len;
416         int err = 0;
417
418         dev_info(dev, "%s\n", __func__);
419
420         if (pvd == NULL) {
421                 dev_err(dev, "%s EMPTY virtual device.\n", __func__);
422                 return ERR_PTR(-EFAULT);
423         }
424
425         err = virtual_device_sanity_check(dev, pvd, &buf_len);
426         if (err)
427                 return ERR_PTR(err);
428
429         buf_len += sizeof(char *) * pvd->reg_num +
430                 sizeof(struct camera_reg) * pvd->pwr_on_size +
431                 sizeof(struct camera_reg) * pvd->pwr_off_size;
432         v_chip = kzalloc(
433                 sizeof(*v_chip) + sizeof(*c_info) + buf_len, GFP_KERNEL);
434         if (!v_chip) {
435                 dev_err(dev, "%s unable to allocate memory!\n", __func__);
436                 return ERR_PTR(-ENOMEM);
437         }
438
439         c_info = (void *)v_chip + sizeof(*v_chip);
440         err = virtual_chip_config(dev, pvd, c_info);
441         if (err) {
442                 kfree(v_chip);
443                 return ERR_PTR(err);
444         }
445
446         strncpy((u8 *)v_chip->name, (u8 const *)pvd->name,
447                 sizeof(v_chip->name));
448
449         p_regmap = (struct regmap_config *)&v_chip->regmap_cfg;
450         memcpy(p_regmap, &regmap_cfg_default, sizeof(*p_regmap));
451         v_chip->type = pvd->bus_type;
452         if (pvd->regmap_cfg.addr_bits)
453                 p_regmap->reg_bits = pvd->regmap_cfg.addr_bits;
454         if (pvd->regmap_cfg.val_bits)
455                 p_regmap->val_bits = pvd->regmap_cfg.val_bits;
456         p_regmap->cache_type = pvd->regmap_cfg.cache_type;
457
458         INIT_LIST_HEAD(&v_chip->list);
459         v_chip->private = c_info;
460         v_chip->init = virtual_instance_create;
461         v_chip->release = virtual_instance_destroy,
462         v_chip->power_on = virtual_power_on,
463         v_chip->power_off = virtual_power_off,
464         v_chip->shutdown = virtual_shutdown,
465         v_chip->update = virtual_update,
466
467         err = camera_chip_add(v_chip);
468         if (err) {
469                 kfree(v_chip);
470                 if (err != -EEXIST)
471                         return ERR_PTR(err);
472                 return NULL;
473         }
474
475         return v_chip;
476 }
477
478 static int __init virtual_init(void)
479 {
480         pr_info("%s\n", __func__);
481         return 0;
482 }
483 device_initcall(virtual_init);
484
485 static void __exit virtual_exit(void)
486 {
487 }
488 module_exit(virtual_exit);
489
490 MODULE_DESCRIPTION("virtual sensor device");
491 MODULE_AUTHOR("Charlie Huang <chahuang@nvidia.com>");
492 MODULE_LICENSE("GPL v2");