diff options
Diffstat (limited to 'drivers/media/video/v4l2-common.c')
-rw-r--r-- | drivers/media/video/v4l2-common.c | 181 |
1 files changed, 180 insertions, 1 deletions
diff --git a/drivers/media/video/v4l2-common.c b/drivers/media/video/v4l2-common.c index f96475626da7..b91d66a767d7 100644 --- a/drivers/media/video/v4l2-common.c +++ b/drivers/media/video/v4l2-common.c | |||
@@ -802,6 +802,17 @@ struct v4l2_subdev *v4l2_i2c_new_subdev(struct v4l2_device *v4l2_dev, | |||
802 | /* Decrease the module use count to match the first try_module_get. */ | 802 | /* Decrease the module use count to match the first try_module_get. */ |
803 | module_put(client->driver->driver.owner); | 803 | module_put(client->driver->driver.owner); |
804 | 804 | ||
805 | if (sd) { | ||
806 | /* We return errors from v4l2_subdev_call only if we have the | ||
807 | callback as the .s_config is not mandatory */ | ||
808 | int err = v4l2_subdev_call(sd, core, s_config, 0, NULL); | ||
809 | |||
810 | if (err && err != -ENOIOCTLCMD) { | ||
811 | v4l2_device_unregister_subdev(sd); | ||
812 | sd = NULL; | ||
813 | } | ||
814 | } | ||
815 | |||
805 | error: | 816 | error: |
806 | /* If we have a client but no subdev, then something went wrong and | 817 | /* If we have a client but no subdev, then something went wrong and |
807 | we must unregister the client. */ | 818 | we must unregister the client. */ |
@@ -852,6 +863,17 @@ struct v4l2_subdev *v4l2_i2c_new_probed_subdev(struct v4l2_device *v4l2_dev, | |||
852 | /* Decrease the module use count to match the first try_module_get. */ | 863 | /* Decrease the module use count to match the first try_module_get. */ |
853 | module_put(client->driver->driver.owner); | 864 | module_put(client->driver->driver.owner); |
854 | 865 | ||
866 | if (sd) { | ||
867 | /* We return errors from v4l2_subdev_call only if we have the | ||
868 | callback as the .s_config is not mandatory */ | ||
869 | int err = v4l2_subdev_call(sd, core, s_config, 0, NULL); | ||
870 | |||
871 | if (err && err != -ENOIOCTLCMD) { | ||
872 | v4l2_device_unregister_subdev(sd); | ||
873 | sd = NULL; | ||
874 | } | ||
875 | } | ||
876 | |||
855 | error: | 877 | error: |
856 | /* If we have a client but no subdev, then something went wrong and | 878 | /* If we have a client but no subdev, then something went wrong and |
857 | we must unregister the client. */ | 879 | we must unregister the client. */ |
@@ -872,6 +894,89 @@ struct v4l2_subdev *v4l2_i2c_new_probed_subdev_addr(struct v4l2_device *v4l2_dev | |||
872 | } | 894 | } |
873 | EXPORT_SYMBOL_GPL(v4l2_i2c_new_probed_subdev_addr); | 895 | EXPORT_SYMBOL_GPL(v4l2_i2c_new_probed_subdev_addr); |
874 | 896 | ||
897 | /* Load an i2c sub-device. */ | ||
898 | struct v4l2_subdev *v4l2_i2c_new_subdev_board(struct v4l2_device *v4l2_dev, | ||
899 | struct i2c_adapter *adapter, const char *module_name, | ||
900 | struct i2c_board_info *info, const unsigned short *probe_addrs) | ||
901 | { | ||
902 | struct v4l2_subdev *sd = NULL; | ||
903 | struct i2c_client *client; | ||
904 | |||
905 | BUG_ON(!v4l2_dev); | ||
906 | |||
907 | if (module_name) | ||
908 | request_module(module_name); | ||
909 | |||
910 | /* Create the i2c client */ | ||
911 | if (info->addr == 0 && probe_addrs) | ||
912 | client = i2c_new_probed_device(adapter, info, probe_addrs); | ||
913 | else | ||
914 | client = i2c_new_device(adapter, info); | ||
915 | |||
916 | /* Note: by loading the module first we are certain that c->driver | ||
917 | will be set if the driver was found. If the module was not loaded | ||
918 | first, then the i2c core tries to delay-load the module for us, | ||
919 | and then c->driver is still NULL until the module is finally | ||
920 | loaded. This delay-load mechanism doesn't work if other drivers | ||
921 | want to use the i2c device, so explicitly loading the module | ||
922 | is the best alternative. */ | ||
923 | if (client == NULL || client->driver == NULL) | ||
924 | goto error; | ||
925 | |||
926 | /* Lock the module so we can safely get the v4l2_subdev pointer */ | ||
927 | if (!try_module_get(client->driver->driver.owner)) | ||
928 | goto error; | ||
929 | sd = i2c_get_clientdata(client); | ||
930 | |||
931 | /* Register with the v4l2_device which increases the module's | ||
932 | use count as well. */ | ||
933 | if (v4l2_device_register_subdev(v4l2_dev, sd)) | ||
934 | sd = NULL; | ||
935 | /* Decrease the module use count to match the first try_module_get. */ | ||
936 | module_put(client->driver->driver.owner); | ||
937 | |||
938 | if (sd) { | ||
939 | /* We return errors from v4l2_subdev_call only if we have the | ||
940 | callback as the .s_config is not mandatory */ | ||
941 | int err = v4l2_subdev_call(sd, core, s_config, | ||
942 | info->irq, info->platform_data); | ||
943 | |||
944 | if (err && err != -ENOIOCTLCMD) { | ||
945 | v4l2_device_unregister_subdev(sd); | ||
946 | sd = NULL; | ||
947 | } | ||
948 | } | ||
949 | |||
950 | error: | ||
951 | /* If we have a client but no subdev, then something went wrong and | ||
952 | we must unregister the client. */ | ||
953 | if (client && sd == NULL) | ||
954 | i2c_unregister_device(client); | ||
955 | return sd; | ||
956 | } | ||
957 | EXPORT_SYMBOL_GPL(v4l2_i2c_new_subdev_board); | ||
958 | |||
959 | struct v4l2_subdev *v4l2_i2c_new_subdev_cfg(struct v4l2_device *v4l2_dev, | ||
960 | struct i2c_adapter *adapter, | ||
961 | const char *module_name, const char *client_type, | ||
962 | int irq, void *platform_data, | ||
963 | u8 addr, const unsigned short *probe_addrs) | ||
964 | { | ||
965 | struct i2c_board_info info; | ||
966 | |||
967 | /* Setup the i2c board info with the device type and | ||
968 | the device address. */ | ||
969 | memset(&info, 0, sizeof(info)); | ||
970 | strlcpy(info.type, client_type, sizeof(info.type)); | ||
971 | info.addr = addr; | ||
972 | info.irq = irq; | ||
973 | info.platform_data = platform_data; | ||
974 | |||
975 | return v4l2_i2c_new_subdev_board(v4l2_dev, adapter, module_name, | ||
976 | &info, probe_addrs); | ||
977 | } | ||
978 | EXPORT_SYMBOL_GPL(v4l2_i2c_new_subdev_cfg); | ||
979 | |||
875 | /* Return i2c client address of v4l2_subdev. */ | 980 | /* Return i2c client address of v4l2_subdev. */ |
876 | unsigned short v4l2_i2c_subdev_addr(struct v4l2_subdev *sd) | 981 | unsigned short v4l2_i2c_subdev_addr(struct v4l2_subdev *sd) |
877 | { | 982 | { |
@@ -916,4 +1021,78 @@ const unsigned short *v4l2_i2c_tuner_addrs(enum v4l2_i2c_tuner_type type) | |||
916 | } | 1021 | } |
917 | EXPORT_SYMBOL_GPL(v4l2_i2c_tuner_addrs); | 1022 | EXPORT_SYMBOL_GPL(v4l2_i2c_tuner_addrs); |
918 | 1023 | ||
919 | #endif | 1024 | #endif /* defined(CONFIG_I2C) */ |
1025 | |||
1026 | /* Clamp x to be between min and max, aligned to a multiple of 2^align. min | ||
1027 | * and max don't have to be aligned, but there must be at least one valid | ||
1028 | * value. E.g., min=17,max=31,align=4 is not allowed as there are no multiples | ||
1029 | * of 16 between 17 and 31. */ | ||
1030 | static unsigned int clamp_align(unsigned int x, unsigned int min, | ||
1031 | unsigned int max, unsigned int align) | ||
1032 | { | ||
1033 | /* Bits that must be zero to be aligned */ | ||
1034 | unsigned int mask = ~((1 << align) - 1); | ||
1035 | |||
1036 | /* Round to nearest aligned value */ | ||
1037 | if (align) | ||
1038 | x = (x + (1 << (align - 1))) & mask; | ||
1039 | |||
1040 | /* Clamp to aligned value of min and max */ | ||
1041 | if (x < min) | ||
1042 | x = (min + ~mask) & mask; | ||
1043 | else if (x > max) | ||
1044 | x = max & mask; | ||
1045 | |||
1046 | return x; | ||
1047 | } | ||
1048 | |||
1049 | /* Bound an image to have a width between wmin and wmax, and height between | ||
1050 | * hmin and hmax, inclusive. Additionally, the width will be a multiple of | ||
1051 | * 2^walign, the height will be a multiple of 2^halign, and the overall size | ||
1052 | * (width*height) will be a multiple of 2^salign. The image may be shrunk | ||
1053 | * or enlarged to fit the alignment constraints. | ||
1054 | * | ||
1055 | * The width or height maximum must not be smaller than the corresponding | ||
1056 | * minimum. The alignments must not be so high there are no possible image | ||
1057 | * sizes within the allowed bounds. wmin and hmin must be at least 1 | ||
1058 | * (don't use 0). If you don't care about a certain alignment, specify 0, | ||
1059 | * as 2^0 is 1 and one byte alignment is equivalent to no alignment. If | ||
1060 | * you only want to adjust downward, specify a maximum that's the same as | ||
1061 | * the initial value. | ||
1062 | */ | ||
1063 | void v4l_bound_align_image(u32 *w, unsigned int wmin, unsigned int wmax, | ||
1064 | unsigned int walign, | ||
1065 | u32 *h, unsigned int hmin, unsigned int hmax, | ||
1066 | unsigned int halign, unsigned int salign) | ||
1067 | { | ||
1068 | *w = clamp_align(*w, wmin, wmax, walign); | ||
1069 | *h = clamp_align(*h, hmin, hmax, halign); | ||
1070 | |||
1071 | /* Usually we don't need to align the size and are done now. */ | ||
1072 | if (!salign) | ||
1073 | return; | ||
1074 | |||
1075 | /* How much alignment do we have? */ | ||
1076 | walign = __ffs(*w); | ||
1077 | halign = __ffs(*h); | ||
1078 | /* Enough to satisfy the image alignment? */ | ||
1079 | if (walign + halign < salign) { | ||
1080 | /* Max walign where there is still a valid width */ | ||
1081 | unsigned int wmaxa = __fls(wmax ^ (wmin - 1)); | ||
1082 | /* Max halign where there is still a valid height */ | ||
1083 | unsigned int hmaxa = __fls(hmax ^ (hmin - 1)); | ||
1084 | |||
1085 | /* up the smaller alignment until we have enough */ | ||
1086 | do { | ||
1087 | if (halign >= hmaxa || | ||
1088 | (walign <= halign && walign < wmaxa)) { | ||
1089 | *w = clamp_align(*w, wmin, wmax, walign + 1); | ||
1090 | walign = __ffs(*w); | ||
1091 | } else { | ||
1092 | *h = clamp_align(*h, hmin, hmax, halign + 1); | ||
1093 | halign = __ffs(*h); | ||
1094 | } | ||
1095 | } while (halign + walign < salign); | ||
1096 | } | ||
1097 | } | ||
1098 | EXPORT_SYMBOL_GPL(v4l_bound_align_image); | ||