2 * ar0330.c - ar0330 sensor driver
4 * Copyright (c) 2014 - 2015, NVIDIA CORPORATION, All Rights Reserved.
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.
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
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/>.
19 #include <linux/delay.h>
21 #include <linux/i2c.h>
22 #include <linux/clk.h>
23 #include <linux/miscdevice.h>
24 #include <linux/slab.h>
25 #include <linux/uaccess.h>
26 #include <linux/regulator/consumer.h>
27 #include <linux/regmap.h>
28 #include <media/ar0330.h>
29 #include <linux/gpio.h>
30 #include <linux/module.h>
32 #include <linux/kernel.h>
33 #include <linux/seq_file.h>
35 #include <linux/of_device.h>
36 #include <linux/of_gpio.h>
38 #include "nvc_utilities.h"
46 struct miscdevice miscdev_info;
48 struct ar0330_power_rail power;
49 struct ar0330_sensordata sensor_data;
50 struct i2c_client *i2c_client;
51 struct ar0330_platform_data *pdata;
53 struct regmap *regmap;
54 struct mutex ar0330_camera_lock;
59 static const struct regmap_config sensor_regmap_config = {
62 .cache_type = REGCACHE_RBTREE,
65 #define AR0330_TABLE_WAIT_MS 0
66 #define AR0330_TABLE_END 1
67 #define AR0330_MAX_RETRIES 3
68 #define AR0330_WAIT_MS 100
70 #define MAX_BUFFER_SIZE 32
71 #define AR0330_FRAME_LENGTH_ADDR 0x300A
72 #define AR0330_COARSE_TIME_ADDR 0x3012
73 #define AR0330_GAIN_ADDR 0x3060
75 static struct ar0330_reg mode_2304x1536[] = {
78 {AR0330_TABLE_WAIT_MS, AR0330_WAIT_MS},
121 {AR0330_TABLE_END, 0x00}
124 static struct ar0330_reg mode_1280x720[] = {
126 {AR0330_TABLE_WAIT_MS, AR0330_WAIT_MS},
129 {AR0330_TABLE_WAIT_MS, AR0330_WAIT_MS},
185 {AR0330_TABLE_WAIT_MS, AR0330_WAIT_MS},
445 {AR0330_TABLE_WAIT_MS, AR0330_WAIT_MS},
450 {AR0330_TABLE_END, 0x00}
453 static struct ar0330_reg mode_1280x960[] = {
456 {AR0330_TABLE_WAIT_MS, AR0330_WAIT_MS},
499 {AR0330_TABLE_END, 0x00}
502 static struct ar0330_reg mode_2048x1296[] = {
505 {AR0330_TABLE_WAIT_MS, AR0330_WAIT_MS},
548 {AR0330_TABLE_END, 0x00}
552 AR0330_MODE_2304X1536,
553 AR0330_MODE_1280X720,
554 AR0330_MODE_1280X960,
555 AR0330_MODE_2048X1296,
558 static struct ar0330_reg *mode_table[] = {
559 [AR0330_MODE_2304X1536] = mode_2304x1536,
560 [AR0330_MODE_1280X720] = mode_1280x720,
561 [AR0330_MODE_1280X960] = mode_1280x960,
562 [AR0330_MODE_2048X1296] = mode_2048x1296,
566 msleep_range(unsigned int delay_base)
568 usleep_range(delay_base*1000, delay_base*1000+500);
572 ar0330_get_frame_length_regs(struct ar0330_reg *regs, u32 frame_length)
574 regs->addr = AR0330_FRAME_LENGTH_ADDR;
575 regs->val = frame_length & 0xffff;
579 ar0330_get_coarse_time_regs(struct ar0330_reg *regs, u32 coarse_time)
581 regs->addr = AR0330_COARSE_TIME_ADDR;
582 regs->val = coarse_time & 0xffff;
586 ar0330_get_gain_reg(struct ar0330_reg *regs, u16 gain)
588 regs->addr = AR0330_GAIN_ADDR;
593 ar0330_read_reg(struct ar0330_info *info, u16 addr, u16 *val)
595 return regmap_read(info->regmap, addr, (unsigned int *) val);
599 ar0330_write_reg(struct ar0330_info *info, u16 addr, u16 val)
603 err = regmap_write(info->regmap, addr, val);
606 pr_err("%s:i2c write failed, %x = %x\n",
607 __func__, addr, val);
613 ar0330_write_table(struct ar0330_info *info,
614 const struct ar0330_reg table[],
615 const struct ar0330_reg override_list[],
616 int num_override_regs)
619 const struct ar0330_reg *next;
623 for (next = table; next->addr != AR0330_TABLE_END; next++) {
624 if (next->addr == AR0330_TABLE_WAIT_MS) {
625 msleep_range(next->val);
631 /* When an override list is passed in, replace the reg */
632 /* value to write if the reg is in the list */
634 for (i = 0; i < num_override_regs; i++) {
635 if (next->addr == override_list[i].addr) {
636 val = override_list[i].val;
642 err = ar0330_write_reg(info, next->addr, val);
644 pr_err("%s:ar0330_write_table:%d", __func__, err);
651 static int ar0330_get_flash_cap(struct ar0330_info *info)
653 struct ar0330_flash_control *fctl;
655 dev_dbg(&info->i2c_client->dev, "%s: %p\n", __func__, info->pdata);
657 fctl = &info->pdata->flash_cap;
658 dev_dbg(&info->i2c_client->dev,
659 "edg: %x, st: %x, rpt: %x, dl: %x\n",
671 static inline int ar0330_set_flash_control(
672 struct ar0330_info *info, struct ar0330_flash_control *fc)
674 dev_dbg(&info->i2c_client->dev, "%s\n", __func__);
679 ar0330_set_mode(struct ar0330_info *info, struct ar0330_mode *mode)
683 struct ar0330_reg reg_list[8];
685 pr_info("%s: xres %u yres %u framelength %u coarsetime %u gain %u\n",
686 __func__, mode->xres, mode->yres, mode->frame_length,
687 mode->coarse_time, mode->gain);
689 if (mode->xres == 2304 && mode->yres == 1520) {
690 sensor_mode = AR0330_MODE_2304X1536;
691 } else if (mode->xres == 1280 && mode->yres == 720) {
692 sensor_mode = AR0330_MODE_1280X720;
693 } else if (mode->xres == 1280 && mode->yres == 960) {
694 sensor_mode = AR0330_MODE_1280X960;
695 } else if (mode->xres == 2048 && mode->yres == 1296) {
696 sensor_mode = AR0330_MODE_2048X1296;
698 pr_err("%s: invalid resolution supplied to set mode %d %d\n",
699 __func__, mode->xres, mode->yres);
703 /* get a list of override regs for the asking frame length, */
704 /* coarse integration time, and gain. */
705 ar0330_get_frame_length_regs(reg_list, mode->frame_length);
706 ar0330_get_coarse_time_regs(reg_list + 1, mode->coarse_time);
707 ar0330_get_gain_reg(reg_list + 2, mode->gain);
709 err = ar0330_write_table(info,
710 mode_table[sensor_mode],
714 info->mode = sensor_mode;
715 pr_info("[AR0330]: stream on.\n");
720 ar0330_get_status(struct ar0330_info *info, u8 *dev_status)
727 ar0330_set_frame_length(struct ar0330_info *info, u32 frame_length,
730 struct ar0330_reg reg_list[2];
734 ar0330_get_frame_length_regs(reg_list, frame_length);
736 for (i = 0; i < 1; i++) {
737 ret = ar0330_write_reg(info, reg_list[i].addr,
747 ar0330_set_coarse_time(struct ar0330_info *info, u32 coarse_time,
752 struct ar0330_reg reg_list[2];
755 ar0330_get_coarse_time_regs(reg_list, coarse_time);
757 for (i = 0; i < 1; i++) {
758 ret = ar0330_write_reg(info, reg_list[i].addr,
768 ar0330_set_gain(struct ar0330_info *info, u16 gain, bool group_hold)
771 struct ar0330_reg reg_list;
773 ar0330_get_gain_reg(®_list, gain);
775 ret = ar0330_write_reg(info, reg_list.addr, reg_list.val);
783 ar0330_set_group_hold(struct ar0330_info *info, struct ar0330_ae *ae)
786 bool group_hold_enabled = false;
790 if (ae->coarse_time_enable)
792 if (ae->frame_length_enable)
795 group_hold_enabled = true;
798 ar0330_set_gain(info, ae->gain, false);
799 if (ae->coarse_time_enable)
800 ar0330_set_coarse_time(info, ae->coarse_time, false);
801 if (ae->frame_length_enable)
802 ar0330_set_frame_length(info, ae->frame_length, false);
807 static int ar0330_get_sensor_id(struct ar0330_info *info)
811 pr_info("%s\n", __func__);
812 if (info->sensor_data.fuse_id_size)
815 /* Note 1: If the sensor does not have power at this point
816 Need to supply the power, e.g. by calling power on function */
818 /*ret |= ar0330_write_reg(info, 0x3B02, 0x00);
819 ret |= ar0330_write_reg(info, 0x3B00, 0x01);
820 for (i = 0; i < 9; i++) {
821 ret |= ar0330_read_reg(info, 0x3B24 + i, &bak);
822 info->sensor_data.fuse_id[i] = bak;
826 info->sensor_data.fuse_id_size = i;*/
828 /* Note 2: Need to clean up any action carried out in Note 1 */
833 static void ar0330_mclk_disable(struct ar0330_info *info)
835 dev_dbg(&info->i2c_client->dev, "%s: disable MCLK\n", __func__);
836 clk_disable_unprepare(info->mclk);
839 static int ar0330_mclk_enable(struct ar0330_info *info)
842 unsigned long mclk_init_rate = 24000000;
844 dev_dbg(&info->i2c_client->dev, "%s: enable MCLK with %lu Hz\n",
845 __func__, mclk_init_rate);
847 err = clk_set_rate(info->mclk, mclk_init_rate);
849 err = clk_prepare_enable(info->mclk);
854 ar0330_ioctl(struct file *file,
855 unsigned int cmd, unsigned long arg)
858 struct ar0330_info *info = file->private_data;
861 case AR0330_IOCTL_SET_POWER:
864 if (arg && info->pdata->power_on) {
865 err = ar0330_mclk_enable(info);
867 err = info->pdata->power_on(&info->power);
869 ar0330_mclk_disable(info);
871 if (!arg && info->pdata->power_off) {
872 info->pdata->power_off(&info->power);
873 ar0330_mclk_disable(info);
876 case AR0330_IOCTL_SET_MODE:
878 struct ar0330_mode mode;
879 if (copy_from_user(&mode, (const void __user *)arg,
880 sizeof(struct ar0330_mode))) {
881 pr_err("%s:Failed to get mode from user.\n", __func__);
884 return ar0330_set_mode(info, &mode);
886 case AR0330_IOCTL_SET_FRAME_LENGTH:
887 return ar0330_set_frame_length(info, (u32)arg, true);
888 case AR0330_IOCTL_SET_COARSE_TIME:
889 return ar0330_set_coarse_time(info, (u32)arg, true);
890 case AR0330_IOCTL_SET_GAIN:
891 return ar0330_set_gain(info, (u16)arg, true);
892 case AR0330_IOCTL_GET_STATUS:
896 err = ar0330_get_status(info, &status);
899 if (copy_to_user((void __user *)arg, &status, 1)) {
900 pr_err("%s:Failed to copy status to user\n", __func__);
905 case AR0330_IOCTL_GET_SENSORDATA:
907 err = ar0330_get_sensor_id(info);
910 pr_err("%s:Failed to get fuse id info.\n", __func__);
913 if (copy_to_user((void __user *)arg, &info->sensor_data,
914 sizeof(struct ar0330_sensordata))) {
915 pr_info("%s:Failed to copy fuse id to user space\n",
921 case AR0330_IOCTL_SET_GROUP_HOLD:
924 if (copy_from_user(&ae, (const void __user *)arg,
925 sizeof(struct ar0330_ae))) {
926 pr_info("%s:fail group hold\n", __func__);
929 return ar0330_set_group_hold(info, &ae);
931 case AR0330_IOCTL_SET_FLASH_MODE:
933 struct ar0330_flash_control values;
935 dev_dbg(&info->i2c_client->dev,
936 "AR0330_IOCTL_SET_FLASH_MODE\n");
937 if (copy_from_user(&values,
938 (const void __user *)arg,
939 sizeof(struct ar0330_flash_control))) {
943 err = ar0330_set_flash_control(info, &values);
946 case AR0330_IOCTL_GET_FLASH_CAP:
947 err = ar0330_get_flash_cap(info);
950 pr_err("%s:unknown cmd.\n", __func__);
957 static int ar0330_power_on(struct ar0330_power_rail *pw)
960 struct ar0330_info *info = container_of(pw, struct ar0330_info, power);
962 if (unlikely(WARN_ON(!pw || !pw->iovdd || !pw->avdd || !pw->dvdd)))
965 gpio_set_value(info->pdata->cam2_gpio, 0);
966 usleep_range(10, 20);
968 err = regulator_enable(pw->avdd);
970 goto ar0330_avdd_fail;
972 err = regulator_enable(pw->dvdd);
974 goto ar0330_dvdd_fail;
976 err = regulator_enable(pw->iovdd);
978 goto ar0330_iovdd_fail;
981 gpio_set_value(info->pdata->cam2_gpio, 1);
983 usleep_range(300, 310);
989 regulator_disable(pw->dvdd);
992 regulator_disable(pw->avdd);
995 pr_err("%s failed.\n", __func__);
999 static int ar0330_power_off(struct ar0330_power_rail *pw)
1001 struct ar0330_info *info = container_of(pw, struct ar0330_info, power);
1003 if (unlikely(WARN_ON(!pw || !pw->iovdd || !pw->avdd || !pw->dvdd)))
1007 gpio_set_value(info->pdata->cam2_gpio, 0);
1010 regulator_disable(pw->iovdd);
1011 regulator_disable(pw->dvdd);
1012 regulator_disable(pw->avdd);
1018 ar0330_open(struct inode *inode, struct file *file)
1020 struct miscdevice *miscdev = file->private_data;
1021 struct ar0330_info *info;
1023 info = container_of(miscdev, struct ar0330_info, miscdev_info);
1024 /* check if the device is in use */
1025 if (atomic_xchg(&info->in_use, 1)) {
1026 pr_info("%s:BUSY!\n", __func__);
1030 file->private_data = info;
1036 ar0330_release(struct inode *inode, struct file *file)
1038 struct ar0330_info *info = file->private_data;
1040 file->private_data = NULL;
1042 /* warn if device is already released */
1043 WARN_ON(!atomic_xchg(&info->in_use, 0));
1047 static int ar0330_power_put(struct ar0330_power_rail *pw)
1052 if (likely(pw->avdd))
1053 regulator_put(pw->avdd);
1055 if (likely(pw->iovdd))
1056 regulator_put(pw->iovdd);
1058 if (likely(pw->dvdd))
1059 regulator_put(pw->dvdd);
1068 static int ar0330_regulator_get(struct ar0330_info *info,
1069 struct regulator **vreg, char vreg_name[])
1071 struct regulator *reg = NULL;
1074 reg = regulator_get(&info->i2c_client->dev, vreg_name);
1075 if (unlikely(IS_ERR(reg))) {
1076 dev_err(&info->i2c_client->dev, "%s %s ERR: %d\n",
1077 __func__, vreg_name, (int)reg);
1081 dev_dbg(&info->i2c_client->dev, "%s: %s\n",
1082 __func__, vreg_name);
1088 static int ar0330_power_get(struct ar0330_info *info)
1090 struct ar0330_power_rail *pw = &info->power;
1093 err |= ar0330_regulator_get(info, &pw->avdd, "vana"); /* ananlog 2.7v */
1094 err |= ar0330_regulator_get(info, &pw->dvdd, "vdig"); /* digital 1.2v */
1095 err |= ar0330_regulator_get(info, &pw->iovdd, "vif"); /* IO 1.8v */
1100 static const struct file_operations ar0330_fileops = {
1101 .owner = THIS_MODULE,
1102 .open = ar0330_open,
1103 .unlocked_ioctl = ar0330_ioctl,
1104 .release = ar0330_release,
1107 static struct miscdevice ar0330_device = {
1108 .minor = MISC_DYNAMIC_MINOR,
1110 .fops = &ar0330_fileops,
1113 static struct of_device_id ar0330_of_match[] = {
1114 { .compatible = "nvidia,ar0330", },
1118 MODULE_DEVICE_TABLE(of, ar0330_of_match);
1120 static struct ar0330_platform_data *ar0330_parse_dt(struct i2c_client *client)
1122 struct device_node *np = client->dev.of_node;
1123 struct ar0330_platform_data *board_info_pdata;
1124 const struct of_device_id *match;
1126 match = of_match_device(ar0330_of_match, &client->dev);
1128 dev_err(&client->dev, "Failed to find matching dt id\n");
1132 board_info_pdata = devm_kzalloc(&client->dev, sizeof(*board_info_pdata),
1134 if (!board_info_pdata) {
1135 dev_err(&client->dev, "Failed to allocate pdata\n");
1139 board_info_pdata->cam2_gpio = of_get_named_gpio(np, "cam1-gpios", 0);
1140 board_info_pdata->ext_reg = of_property_read_bool(np, "nvidia,ext_reg");
1142 board_info_pdata->power_on = ar0330_power_on;
1143 board_info_pdata->power_off = ar0330_power_off;
1145 return board_info_pdata;
1149 ar0330_probe(struct i2c_client *client,
1150 const struct i2c_device_id *id)
1152 struct ar0330_info *info;
1154 const char *mclk_name;
1156 pr_err("[AR0330]: probing sensor.\n");
1158 info = devm_kzalloc(&client->dev,
1159 sizeof(struct ar0330_info), GFP_KERNEL);
1161 pr_err("%s:Unable to allocate memory!\n", __func__);
1165 info->regmap = devm_regmap_init_i2c(client, &sensor_regmap_config);
1166 if (IS_ERR(info->regmap)) {
1167 dev_err(&client->dev,
1168 "regmap init failed: %ld\n", PTR_ERR(info->regmap));
1172 if (client->dev.of_node)
1173 info->pdata = ar0330_parse_dt(client);
1175 info->pdata = client->dev.platform_data;
1178 pr_err("[AR0330]:%s:Unable to get platform data\n", __func__);
1182 info->i2c_client = client;
1183 atomic_set(&info->in_use, 0);
1186 mclk_name = info->pdata->mclk_name ?
1187 info->pdata->mclk_name : "default_mclk";
1188 info->mclk = devm_clk_get(&client->dev, mclk_name);
1189 if (IS_ERR(info->mclk)) {
1190 dev_err(&client->dev, "%s: unable to get clock %s\n",
1191 __func__, mclk_name);
1192 return PTR_ERR(info->mclk);
1195 if (info->pdata->dev_name != NULL)
1196 strncpy(info->devname, info->pdata->dev_name,
1197 sizeof(info->devname) - 1);
1199 strncpy(info->devname, "ar0330", sizeof(info->devname) - 1);
1201 ar0330_power_get(info);
1203 memcpy(&info->miscdev_info,
1205 sizeof(struct miscdevice));
1207 info->miscdev_info.name = info->devname;
1209 err = misc_register(&info->miscdev_info);
1211 pr_err("%s:Unable to register misc device!\n", __func__);
1212 goto ar0330_probe_fail;
1215 i2c_set_clientdata(client, info);
1217 mutex_init(&info->ar0330_camera_lock);
1218 pr_err("[AR0330]: end of probing sensor.\n");
1222 ar0330_power_put(&info->power);
1228 ar0330_remove(struct i2c_client *client)
1230 struct ar0330_info *info;
1231 info = i2c_get_clientdata(client);
1232 misc_deregister(&ar0330_device);
1233 mutex_destroy(&info->ar0330_camera_lock);
1235 ar0330_power_put(&info->power);
1240 static const struct i2c_device_id ar0330_id[] = {
1246 MODULE_DEVICE_TABLE(i2c, ar0330_id);
1248 static struct i2c_driver ar0330_i2c_driver = {
1251 .owner = THIS_MODULE,
1253 .probe = ar0330_probe,
1254 .remove = ar0330_remove,
1255 .id_table = ar0330_id,
1258 static int __init ar0330_init(void)
1260 pr_info("[AR0330] sensor driver loading\n");
1261 return i2c_add_driver(&ar0330_i2c_driver);
1264 static void __exit ar0330_exit(void)
1266 i2c_del_driver(&ar0330_i2c_driver);
1269 module_init(ar0330_init);
1270 module_exit(ar0330_exit);