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}
503 AR0330_MODE_2304X1536,
504 AR0330_MODE_1280X720,
505 AR0330_MODE_1280X960,
508 static struct ar0330_reg *mode_table[] = {
509 [AR0330_MODE_2304X1536] = mode_2304x1536,
510 [AR0330_MODE_1280X720] = mode_1280x720,
511 [AR0330_MODE_1280X960] = mode_1280x960,
515 msleep_range(unsigned int delay_base)
517 usleep_range(delay_base*1000, delay_base*1000+500);
521 ar0330_get_frame_length_regs(struct ar0330_reg *regs, u32 frame_length)
523 regs->addr = AR0330_FRAME_LENGTH_ADDR;
524 regs->val = frame_length & 0xffff;
528 ar0330_get_coarse_time_regs(struct ar0330_reg *regs, u32 coarse_time)
530 regs->addr = AR0330_COARSE_TIME_ADDR;
531 regs->val = coarse_time & 0xffff;
535 ar0330_get_gain_reg(struct ar0330_reg *regs, u16 gain)
537 regs->addr = AR0330_GAIN_ADDR;
542 ar0330_read_reg(struct ar0330_info *info, u16 addr, u16 *val)
544 return regmap_read(info->regmap, addr, (unsigned int *) val);
548 ar0330_write_reg(struct ar0330_info *info, u16 addr, u16 val)
552 err = regmap_write(info->regmap, addr, val);
555 pr_err("%s:i2c write failed, %x = %x\n",
556 __func__, addr, val);
562 ar0330_write_table(struct ar0330_info *info,
563 const struct ar0330_reg table[],
564 const struct ar0330_reg override_list[],
565 int num_override_regs)
568 const struct ar0330_reg *next;
572 for (next = table; next->addr != AR0330_TABLE_END; next++) {
573 if (next->addr == AR0330_TABLE_WAIT_MS) {
574 msleep_range(next->val);
580 /* When an override list is passed in, replace the reg */
581 /* value to write if the reg is in the list */
583 for (i = 0; i < num_override_regs; i++) {
584 if (next->addr == override_list[i].addr) {
585 val = override_list[i].val;
591 err = ar0330_write_reg(info, next->addr, val);
593 pr_err("%s:ar0330_write_table:%d", __func__, err);
600 static int ar0330_get_flash_cap(struct ar0330_info *info)
602 struct ar0330_flash_control *fctl;
604 dev_dbg(&info->i2c_client->dev, "%s: %p\n", __func__, info->pdata);
606 fctl = &info->pdata->flash_cap;
607 dev_dbg(&info->i2c_client->dev,
608 "edg: %x, st: %x, rpt: %x, dl: %x\n",
620 static inline int ar0330_set_flash_control(
621 struct ar0330_info *info, struct ar0330_flash_control *fc)
623 dev_dbg(&info->i2c_client->dev, "%s\n", __func__);
628 ar0330_set_mode(struct ar0330_info *info, struct ar0330_mode *mode)
632 struct ar0330_reg reg_list[8];
634 pr_info("%s: xres %u yres %u framelength %u coarsetime %u gain %u\n",
635 __func__, mode->xres, mode->yres, mode->frame_length,
636 mode->coarse_time, mode->gain);
638 if (mode->xres == 2304 && mode->yres == 1520) {
639 sensor_mode = AR0330_MODE_2304X1536;
640 } else if (mode->xres == 1280 && mode->yres == 720) {
641 sensor_mode = AR0330_MODE_1280X720;
642 } else if (mode->xres == 1280 && mode->yres == 960) {
643 sensor_mode = AR0330_MODE_1280X960;
645 pr_err("%s: invalid resolution supplied to set mode %d %d\n",
646 __func__, mode->xres, mode->yres);
650 /* get a list of override regs for the asking frame length, */
651 /* coarse integration time, and gain. */
652 ar0330_get_frame_length_regs(reg_list, mode->frame_length);
653 ar0330_get_coarse_time_regs(reg_list + 1, mode->coarse_time);
654 ar0330_get_gain_reg(reg_list + 2, mode->gain);
656 err = ar0330_write_table(info,
657 mode_table[sensor_mode],
661 info->mode = sensor_mode;
662 pr_info("[AR0330]: stream on.\n");
667 ar0330_get_status(struct ar0330_info *info, u8 *dev_status)
674 ar0330_set_frame_length(struct ar0330_info *info, u32 frame_length,
677 struct ar0330_reg reg_list[2];
681 ar0330_get_frame_length_regs(reg_list, frame_length);
683 for (i = 0; i < 1; i++) {
684 ret = ar0330_write_reg(info, reg_list[i].addr,
694 ar0330_set_coarse_time(struct ar0330_info *info, u32 coarse_time,
699 struct ar0330_reg reg_list[2];
702 ar0330_get_coarse_time_regs(reg_list, coarse_time);
704 for (i = 0; i < 1; i++) {
705 ret = ar0330_write_reg(info, reg_list[i].addr,
715 ar0330_set_gain(struct ar0330_info *info, u16 gain, bool group_hold)
718 struct ar0330_reg reg_list;
720 ar0330_get_gain_reg(®_list, gain);
722 ret = ar0330_write_reg(info, reg_list.addr, reg_list.val);
730 ar0330_set_group_hold(struct ar0330_info *info, struct ar0330_ae *ae)
733 bool group_hold_enabled = false;
737 if (ae->coarse_time_enable)
739 if (ae->frame_length_enable)
742 group_hold_enabled = true;
745 ar0330_set_gain(info, ae->gain, false);
746 if (ae->coarse_time_enable)
747 ar0330_set_coarse_time(info, ae->coarse_time, false);
748 if (ae->frame_length_enable)
749 ar0330_set_frame_length(info, ae->frame_length, false);
754 static int ar0330_get_sensor_id(struct ar0330_info *info)
758 pr_info("%s\n", __func__);
759 if (info->sensor_data.fuse_id_size)
762 /* Note 1: If the sensor does not have power at this point
763 Need to supply the power, e.g. by calling power on function */
765 /*ret |= ar0330_write_reg(info, 0x3B02, 0x00);
766 ret |= ar0330_write_reg(info, 0x3B00, 0x01);
767 for (i = 0; i < 9; i++) {
768 ret |= ar0330_read_reg(info, 0x3B24 + i, &bak);
769 info->sensor_data.fuse_id[i] = bak;
773 info->sensor_data.fuse_id_size = i;*/
775 /* Note 2: Need to clean up any action carried out in Note 1 */
780 static void ar0330_mclk_disable(struct ar0330_info *info)
782 dev_dbg(&info->i2c_client->dev, "%s: disable MCLK\n", __func__);
783 clk_disable_unprepare(info->mclk);
786 static int ar0330_mclk_enable(struct ar0330_info *info)
789 unsigned long mclk_init_rate = 24000000;
791 dev_dbg(&info->i2c_client->dev, "%s: enable MCLK with %lu Hz\n",
792 __func__, mclk_init_rate);
794 err = clk_set_rate(info->mclk, mclk_init_rate);
796 err = clk_prepare_enable(info->mclk);
801 ar0330_ioctl(struct file *file,
802 unsigned int cmd, unsigned long arg)
805 struct ar0330_info *info = file->private_data;
808 case AR0330_IOCTL_SET_POWER:
811 if (arg && info->pdata->power_on) {
812 err = ar0330_mclk_enable(info);
814 err = info->pdata->power_on(&info->power);
816 ar0330_mclk_disable(info);
818 if (!arg && info->pdata->power_off) {
819 info->pdata->power_off(&info->power);
820 ar0330_mclk_disable(info);
823 case AR0330_IOCTL_SET_MODE:
825 struct ar0330_mode mode;
826 if (copy_from_user(&mode, (const void __user *)arg,
827 sizeof(struct ar0330_mode))) {
828 pr_err("%s:Failed to get mode from user.\n", __func__);
831 return ar0330_set_mode(info, &mode);
833 case AR0330_IOCTL_SET_FRAME_LENGTH:
834 return ar0330_set_frame_length(info, (u32)arg, true);
835 case AR0330_IOCTL_SET_COARSE_TIME:
836 return ar0330_set_coarse_time(info, (u32)arg, true);
837 case AR0330_IOCTL_SET_GAIN:
838 return ar0330_set_gain(info, (u16)arg, true);
839 case AR0330_IOCTL_GET_STATUS:
843 err = ar0330_get_status(info, &status);
846 if (copy_to_user((void __user *)arg, &status, 1)) {
847 pr_err("%s:Failed to copy status to user\n", __func__);
852 case AR0330_IOCTL_GET_SENSORDATA:
854 err = ar0330_get_sensor_id(info);
857 pr_err("%s:Failed to get fuse id info.\n", __func__);
860 if (copy_to_user((void __user *)arg, &info->sensor_data,
861 sizeof(struct ar0330_sensordata))) {
862 pr_info("%s:Failed to copy fuse id to user space\n",
868 case AR0330_IOCTL_SET_GROUP_HOLD:
871 if (copy_from_user(&ae, (const void __user *)arg,
872 sizeof(struct ar0330_ae))) {
873 pr_info("%s:fail group hold\n", __func__);
876 return ar0330_set_group_hold(info, &ae);
878 case AR0330_IOCTL_SET_FLASH_MODE:
880 struct ar0330_flash_control values;
882 dev_dbg(&info->i2c_client->dev,
883 "AR0330_IOCTL_SET_FLASH_MODE\n");
884 if (copy_from_user(&values,
885 (const void __user *)arg,
886 sizeof(struct ar0330_flash_control))) {
890 err = ar0330_set_flash_control(info, &values);
893 case AR0330_IOCTL_GET_FLASH_CAP:
894 err = ar0330_get_flash_cap(info);
897 pr_err("%s:unknown cmd.\n", __func__);
904 static int ar0330_power_on(struct ar0330_power_rail *pw)
907 struct ar0330_info *info = container_of(pw, struct ar0330_info, power);
909 if (unlikely(WARN_ON(!pw || !pw->iovdd || !pw->avdd || !pw->dvdd)))
912 gpio_set_value(info->pdata->cam2_gpio, 0);
913 usleep_range(10, 20);
915 err = regulator_enable(pw->avdd);
917 goto ar0330_avdd_fail;
919 err = regulator_enable(pw->dvdd);
921 goto ar0330_dvdd_fail;
923 err = regulator_enable(pw->iovdd);
925 goto ar0330_iovdd_fail;
928 gpio_set_value(info->pdata->cam2_gpio, 1);
930 usleep_range(300, 310);
936 regulator_disable(pw->dvdd);
939 regulator_disable(pw->avdd);
942 pr_err("%s failed.\n", __func__);
946 static int ar0330_power_off(struct ar0330_power_rail *pw)
948 struct ar0330_info *info = container_of(pw, struct ar0330_info, power);
950 if (unlikely(WARN_ON(!pw || !pw->iovdd || !pw->avdd || !pw->dvdd)))
954 gpio_set_value(info->pdata->cam2_gpio, 0);
957 regulator_disable(pw->iovdd);
958 regulator_disable(pw->dvdd);
959 regulator_disable(pw->avdd);
965 ar0330_open(struct inode *inode, struct file *file)
967 struct miscdevice *miscdev = file->private_data;
968 struct ar0330_info *info;
970 info = container_of(miscdev, struct ar0330_info, miscdev_info);
971 /* check if the device is in use */
972 if (atomic_xchg(&info->in_use, 1)) {
973 pr_info("%s:BUSY!\n", __func__);
977 file->private_data = info;
983 ar0330_release(struct inode *inode, struct file *file)
985 struct ar0330_info *info = file->private_data;
987 file->private_data = NULL;
989 /* warn if device is already released */
990 WARN_ON(!atomic_xchg(&info->in_use, 0));
994 static int ar0330_power_put(struct ar0330_power_rail *pw)
999 if (likely(pw->avdd))
1000 regulator_put(pw->avdd);
1002 if (likely(pw->iovdd))
1003 regulator_put(pw->iovdd);
1005 if (likely(pw->dvdd))
1006 regulator_put(pw->dvdd);
1015 static int ar0330_regulator_get(struct ar0330_info *info,
1016 struct regulator **vreg, char vreg_name[])
1018 struct regulator *reg = NULL;
1021 reg = regulator_get(&info->i2c_client->dev, vreg_name);
1022 if (unlikely(IS_ERR(reg))) {
1023 dev_err(&info->i2c_client->dev, "%s %s ERR: %d\n",
1024 __func__, vreg_name, (int)reg);
1028 dev_dbg(&info->i2c_client->dev, "%s: %s\n",
1029 __func__, vreg_name);
1035 static int ar0330_power_get(struct ar0330_info *info)
1037 struct ar0330_power_rail *pw = &info->power;
1040 err |= ar0330_regulator_get(info, &pw->avdd, "vana"); /* ananlog 2.7v */
1041 err |= ar0330_regulator_get(info, &pw->dvdd, "vdig"); /* digital 1.2v */
1042 err |= ar0330_regulator_get(info, &pw->iovdd, "vif"); /* IO 1.8v */
1047 static const struct file_operations ar0330_fileops = {
1048 .owner = THIS_MODULE,
1049 .open = ar0330_open,
1050 .unlocked_ioctl = ar0330_ioctl,
1051 .release = ar0330_release,
1054 static struct miscdevice ar0330_device = {
1055 .minor = MISC_DYNAMIC_MINOR,
1057 .fops = &ar0330_fileops,
1060 static struct of_device_id ar0330_of_match[] = {
1061 { .compatible = "nvidia,ar0330", },
1065 MODULE_DEVICE_TABLE(of, ar0330_of_match);
1067 static struct ar0330_platform_data *ar0330_parse_dt(struct i2c_client *client)
1069 struct device_node *np = client->dev.of_node;
1070 struct ar0330_platform_data *board_info_pdata;
1071 const struct of_device_id *match;
1073 match = of_match_device(ar0330_of_match, &client->dev);
1075 dev_err(&client->dev, "Failed to find matching dt id\n");
1079 board_info_pdata = devm_kzalloc(&client->dev, sizeof(*board_info_pdata),
1081 if (!board_info_pdata) {
1082 dev_err(&client->dev, "Failed to allocate pdata\n");
1086 board_info_pdata->cam2_gpio = of_get_named_gpio(np, "cam1-gpios", 0);
1087 board_info_pdata->ext_reg = of_property_read_bool(np, "nvidia,ext_reg");
1089 board_info_pdata->power_on = ar0330_power_on;
1090 board_info_pdata->power_off = ar0330_power_off;
1092 return board_info_pdata;
1096 ar0330_probe(struct i2c_client *client,
1097 const struct i2c_device_id *id)
1099 struct ar0330_info *info;
1101 const char *mclk_name;
1103 pr_err("[AR0330]: probing sensor.\n");
1105 info = devm_kzalloc(&client->dev,
1106 sizeof(struct ar0330_info), GFP_KERNEL);
1108 pr_err("%s:Unable to allocate memory!\n", __func__);
1112 info->regmap = devm_regmap_init_i2c(client, &sensor_regmap_config);
1113 if (IS_ERR(info->regmap)) {
1114 dev_err(&client->dev,
1115 "regmap init failed: %ld\n", PTR_ERR(info->regmap));
1119 if (client->dev.of_node)
1120 info->pdata = ar0330_parse_dt(client);
1122 info->pdata = client->dev.platform_data;
1125 pr_err("[AR0330]:%s:Unable to get platform data\n", __func__);
1129 info->i2c_client = client;
1130 atomic_set(&info->in_use, 0);
1133 mclk_name = info->pdata->mclk_name ?
1134 info->pdata->mclk_name : "default_mclk";
1135 info->mclk = devm_clk_get(&client->dev, mclk_name);
1136 if (IS_ERR(info->mclk)) {
1137 dev_err(&client->dev, "%s: unable to get clock %s\n",
1138 __func__, mclk_name);
1139 return PTR_ERR(info->mclk);
1142 if (info->pdata->dev_name != NULL)
1143 strncpy(info->devname, info->pdata->dev_name,
1144 sizeof(info->devname) - 1);
1146 strncpy(info->devname, "ar0330", sizeof(info->devname) - 1);
1148 ar0330_power_get(info);
1150 memcpy(&info->miscdev_info,
1152 sizeof(struct miscdevice));
1154 info->miscdev_info.name = info->devname;
1156 err = misc_register(&info->miscdev_info);
1158 pr_err("%s:Unable to register misc device!\n", __func__);
1159 goto ar0330_probe_fail;
1162 i2c_set_clientdata(client, info);
1164 mutex_init(&info->ar0330_camera_lock);
1165 pr_err("[AR0330]: end of probing sensor.\n");
1169 ar0330_power_put(&info->power);
1175 ar0330_remove(struct i2c_client *client)
1177 struct ar0330_info *info;
1178 info = i2c_get_clientdata(client);
1179 misc_deregister(&ar0330_device);
1180 mutex_destroy(&info->ar0330_camera_lock);
1182 ar0330_power_put(&info->power);
1187 static const struct i2c_device_id ar0330_id[] = {
1193 MODULE_DEVICE_TABLE(i2c, ar0330_id);
1195 static struct i2c_driver ar0330_i2c_driver = {
1198 .owner = THIS_MODULE,
1200 .probe = ar0330_probe,
1201 .remove = ar0330_remove,
1202 .id_table = ar0330_id,
1205 static int __init ar0330_init(void)
1207 pr_info("[AR0330] sensor driver loading\n");
1208 return i2c_add_driver(&ar0330_i2c_driver);
1211 static void __exit ar0330_exit(void)
1213 i2c_del_driver(&ar0330_i2c_driver);
1216 module_init(ar0330_init);
1217 module_exit(ar0330_exit);