Merge branch 'locking-core-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[cascardo/linux.git] / drivers / media / i2c / smiapp / smiapp-quirk.c
1 /*
2  * drivers/media/i2c/smiapp/smiapp-quirk.c
3  *
4  * Generic driver for SMIA/SMIA++ compliant camera modules
5  *
6  * Copyright (C) 2011--2012 Nokia Corporation
7  * Contact: Sakari Ailus <sakari.ailus@iki.fi>
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * version 2 as published by the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21  * 02110-1301 USA
22  *
23  */
24
25 #include <linux/delay.h>
26
27 #include "smiapp.h"
28
29 static int smiapp_write_8(struct smiapp_sensor *sensor, u16 reg, u8 val)
30 {
31         return smiapp_write(sensor, SMIAPP_REG_MK_U8(reg), val);
32 }
33
34 static int smiapp_write_8s(struct smiapp_sensor *sensor,
35                            struct smiapp_reg_8 *regs, int len)
36 {
37         struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
38         int rval;
39
40         for (; len > 0; len--, regs++) {
41                 rval = smiapp_write_8(sensor, regs->reg, regs->val);
42                 if (rval < 0) {
43                         dev_err(&client->dev,
44                                 "error %d writing reg 0x%4.4x, val 0x%2.2x",
45                                 rval, regs->reg, regs->val);
46                         return rval;
47                 }
48         }
49
50         return 0;
51 }
52
53 void smiapp_replace_limit(struct smiapp_sensor *sensor,
54                           u32 limit, u32 val)
55 {
56         struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
57
58         dev_dbg(&client->dev, "quirk: 0x%8.8x \"%s\" = %d, 0x%x\n",
59                 smiapp_reg_limits[limit].addr,
60                 smiapp_reg_limits[limit].what, val, val);
61         sensor->limits[limit] = val;
62 }
63
64 static int jt8ew9_limits(struct smiapp_sensor *sensor)
65 {
66         if (sensor->minfo.revision_number_major < 0x03)
67                 sensor->frame_skip = 1;
68
69         /* Below 24 gain doesn't have effect at all, */
70         /* but ~59 is needed for full dynamic range */
71         smiapp_replace_limit(sensor, SMIAPP_LIMIT_ANALOGUE_GAIN_CODE_MIN, 59);
72         smiapp_replace_limit(
73                 sensor, SMIAPP_LIMIT_ANALOGUE_GAIN_CODE_MAX, 6000);
74
75         return 0;
76 }
77
78 static int jt8ew9_post_poweron(struct smiapp_sensor *sensor)
79 {
80         struct smiapp_reg_8 regs[] = {
81                 { 0x30a3, 0xd8 }, /* Output port control : LVDS ports only */
82                 { 0x30ae, 0x00 }, /* 0x0307 pll_multiplier maximum value on PLL input 9.6MHz ( 19.2MHz is divided on pre_pll_div) */
83                 { 0x30af, 0xd0 }, /* 0x0307 pll_multiplier maximum value on PLL input 9.6MHz ( 19.2MHz is divided on pre_pll_div) */
84                 { 0x322d, 0x04 }, /* Adjusting Processing Image Size to Scaler Toshiba Recommendation Setting */
85                 { 0x3255, 0x0f }, /* Horizontal Noise Reduction Control Toshiba Recommendation Setting */
86                 { 0x3256, 0x15 }, /* Horizontal Noise Reduction Control Toshiba Recommendation Setting */
87                 { 0x3258, 0x70 }, /* Analog Gain Control Toshiba Recommendation Setting */
88                 { 0x3259, 0x70 }, /* Analog Gain Control Toshiba Recommendation Setting */
89                 { 0x325f, 0x7c }, /* Analog Gain Control Toshiba Recommendation Setting */
90                 { 0x3302, 0x06 }, /* Pixel Reference Voltage Control Toshiba Recommendation Setting */
91                 { 0x3304, 0x00 }, /* Pixel Reference Voltage Control Toshiba Recommendation Setting */
92                 { 0x3307, 0x22 }, /* Pixel Reference Voltage Control Toshiba Recommendation Setting */
93                 { 0x3308, 0x8d }, /* Pixel Reference Voltage Control Toshiba Recommendation Setting */
94                 { 0x331e, 0x0f }, /* Black Hole Sun Correction Control Toshiba Recommendation Setting */
95                 { 0x3320, 0x30 }, /* Black Hole Sun Correction Control Toshiba Recommendation Setting */
96                 { 0x3321, 0x11 }, /* Black Hole Sun Correction Control Toshiba Recommendation Setting */
97                 { 0x3322, 0x98 }, /* Black Hole Sun Correction Control Toshiba Recommendation Setting */
98                 { 0x3323, 0x64 }, /* Black Hole Sun Correction Control Toshiba Recommendation Setting */
99                 { 0x3325, 0x83 }, /* Read Out Timing Control Toshiba Recommendation Setting */
100                 { 0x3330, 0x18 }, /* Read Out Timing Control Toshiba Recommendation Setting */
101                 { 0x333c, 0x01 }, /* Read Out Timing Control Toshiba Recommendation Setting */
102                 { 0x3345, 0x2f }, /* Black Hole Sun Correction Control Toshiba Recommendation Setting */
103                 { 0x33de, 0x38 }, /* Horizontal Noise Reduction Control Toshiba Recommendation Setting */
104                 /* Taken from v03. No idea what the rest are. */
105                 { 0x32e0, 0x05 },
106                 { 0x32e1, 0x05 },
107                 { 0x32e2, 0x04 },
108                 { 0x32e5, 0x04 },
109                 { 0x32e6, 0x04 },
110
111         };
112
113         return smiapp_write_8s(sensor, regs, ARRAY_SIZE(regs));
114 }
115
116 const struct smiapp_quirk smiapp_jt8ew9_quirk = {
117         .limits = jt8ew9_limits,
118         .post_poweron = jt8ew9_post_poweron,
119 };
120
121 static int imx125es_post_poweron(struct smiapp_sensor *sensor)
122 {
123         /* Taken from v02. No idea what the other two are. */
124         struct smiapp_reg_8 regs[] = {
125                 /*
126                  * 0x3302: clk during frame blanking:
127                  * 0x00 - HS mode, 0x01 - LP11
128                  */
129                 { 0x3302, 0x01 },
130                 { 0x302d, 0x00 },
131                 { 0x3b08, 0x8c },
132         };
133
134         return smiapp_write_8s(sensor, regs, ARRAY_SIZE(regs));
135 }
136
137 const struct smiapp_quirk smiapp_imx125es_quirk = {
138         .post_poweron = imx125es_post_poweron,
139 };
140
141 static int jt8ev1_limits(struct smiapp_sensor *sensor)
142 {
143         smiapp_replace_limit(sensor, SMIAPP_LIMIT_X_ADDR_MAX, 4271);
144         smiapp_replace_limit(sensor,
145                              SMIAPP_LIMIT_MIN_LINE_BLANKING_PCK_BIN, 184);
146
147         return 0;
148 }
149
150 static int jt8ev1_post_poweron(struct smiapp_sensor *sensor)
151 {
152         struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
153         int rval;
154
155         struct smiapp_reg_8 regs[] = {
156                 { 0x3031, 0xcd }, /* For digital binning (EQ_MONI) */
157                 { 0x30a3, 0xd0 }, /* FLASH STROBE enable */
158                 { 0x3237, 0x00 }, /* For control of pulse timing for ADC */
159                 { 0x3238, 0x43 },
160                 { 0x3301, 0x06 }, /* For analog bias for sensor */
161                 { 0x3302, 0x06 },
162                 { 0x3304, 0x00 },
163                 { 0x3305, 0x88 },
164                 { 0x332a, 0x14 },
165                 { 0x332c, 0x6b },
166                 { 0x3336, 0x01 },
167                 { 0x333f, 0x1f },
168                 { 0x3355, 0x00 },
169                 { 0x3356, 0x20 },
170                 { 0x33bf, 0x20 }, /* Adjust the FBC speed */
171                 { 0x33c9, 0x20 },
172                 { 0x33ce, 0x30 }, /* Adjust the parameter for logic function */
173                 { 0x33cf, 0xec }, /* For Black sun */
174                 { 0x3328, 0x80 }, /* Ugh. No idea what's this. */
175         };
176
177         struct smiapp_reg_8 regs_96[] = {
178                 { 0x30ae, 0x00 }, /* For control of ADC clock */
179                 { 0x30af, 0xd0 },
180                 { 0x30b0, 0x01 },
181         };
182
183         rval = smiapp_write_8s(sensor, regs, ARRAY_SIZE(regs));
184         if (rval < 0)
185                 return rval;
186
187         switch (sensor->platform_data->ext_clk) {
188         case 9600000:
189                 return smiapp_write_8s(sensor, regs_96,
190                                        ARRAY_SIZE(regs_96));
191         default:
192                 dev_warn(&client->dev, "no MSRs for %d Hz ext_clk\n",
193                          sensor->platform_data->ext_clk);
194                 return 0;
195         }
196 }
197
198 static int jt8ev1_pre_streamon(struct smiapp_sensor *sensor)
199 {
200         return smiapp_write_8(sensor, 0x3328, 0x00);
201 }
202
203 static int jt8ev1_post_streamoff(struct smiapp_sensor *sensor)
204 {
205         int rval;
206
207         /* Workaround: allows fast standby to work properly */
208         rval = smiapp_write_8(sensor, 0x3205, 0x04);
209         if (rval < 0)
210                 return rval;
211
212         /* Wait for 1 ms + one line => 2 ms is likely enough */
213         usleep_range(2000, 2000);
214
215         /* Restore it */
216         rval = smiapp_write_8(sensor, 0x3205, 0x00);
217         if (rval < 0)
218                 return rval;
219
220         return smiapp_write_8(sensor, 0x3328, 0x80);
221 }
222
223 static unsigned long jt8ev1_pll_flags(struct smiapp_sensor *sensor)
224 {
225         return SMIAPP_PLL_FLAG_OP_PIX_CLOCK_PER_LANE;
226 }
227
228 const struct smiapp_quirk smiapp_jt8ev1_quirk = {
229         .limits = jt8ev1_limits,
230         .post_poweron = jt8ev1_post_poweron,
231         .pre_streamon = jt8ev1_pre_streamon,
232         .post_streamoff = jt8ev1_post_streamoff,
233         .pll_flags = jt8ev1_pll_flags,
234 };
235
236 static int tcm8500md_limits(struct smiapp_sensor *sensor)
237 {
238         smiapp_replace_limit(sensor, SMIAPP_LIMIT_MIN_PLL_IP_FREQ_HZ, 2700000);
239
240         return 0;
241 }
242
243 const struct smiapp_quirk smiapp_tcm8500md_quirk = {
244         .limits = tcm8500md_limits,
245 };