Merge branch 'for-3.5-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/tj...
[firefly-linux-kernel-4.4.55.git] / drivers / media / video / smiapp / smiapp-quirk.c
1 /*
2  * drivers/media/video/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@maxwell.research.nokia.com>
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, (SMIA_REG_8BIT << 16) | 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 int smiapp_replace_limit_at(struct smiapp_sensor *sensor,
65                             u32 reg, u32 val)
66 {
67         struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
68         int i;
69
70         for (i = 0; smiapp_reg_limits[i].addr; i++) {
71                 if ((smiapp_reg_limits[i].addr & 0xffff) != reg)
72                         continue;
73
74                 smiapp_replace_limit(sensor, i, val);
75
76                 return 0;
77         }
78
79         dev_dbg(&client->dev, "quirk: bad register 0x%4.4x\n", reg);
80
81         return -EINVAL;
82 }
83
84 bool smiapp_quirk_reg(struct smiapp_sensor *sensor,
85                       u32 reg, u32 *val)
86 {
87         struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
88         const struct smia_reg *sreg;
89
90         if (!sensor->minfo.quirk)
91                 return false;
92
93         sreg = sensor->minfo.quirk->regs;
94
95         if (!sreg)
96                 return false;
97
98         while (sreg->type) {
99                 u16 type = reg >> 16;
100                 u16 reg16 = reg;
101
102                 if (sreg->type != type || sreg->reg != reg16) {
103                         sreg++;
104                         continue;
105                 }
106
107                 switch ((u8)type) {
108                 case SMIA_REG_8BIT:
109                         dev_dbg(&client->dev, "quirk: 0x%8.8x: 0x%2.2x\n",
110                                 reg, sreg->val);
111                         break;
112                 case SMIA_REG_16BIT:
113                         dev_dbg(&client->dev, "quirk: 0x%8.8x: 0x%4.4x\n",
114                                 reg, sreg->val);
115                         break;
116                 case SMIA_REG_32BIT:
117                         dev_dbg(&client->dev, "quirk: 0x%8.8x: 0x%8.8x\n",
118                                 reg, sreg->val);
119                         break;
120                 }
121
122                 *val = sreg->val;
123
124                 return true;
125         }
126
127         return false;
128 }
129
130 static int jt8ew9_limits(struct smiapp_sensor *sensor)
131 {
132         if (sensor->minfo.revision_number_major < 0x03)
133                 sensor->frame_skip = 1;
134
135         /* Below 24 gain doesn't have effect at all, */
136         /* but ~59 is needed for full dynamic range */
137         smiapp_replace_limit(sensor, SMIAPP_LIMIT_ANALOGUE_GAIN_CODE_MIN, 59);
138         smiapp_replace_limit(
139                 sensor, SMIAPP_LIMIT_ANALOGUE_GAIN_CODE_MAX, 6000);
140
141         return 0;
142 }
143
144 static int jt8ew9_post_poweron(struct smiapp_sensor *sensor)
145 {
146         struct smiapp_reg_8 regs[] = {
147                 { 0x30a3, 0xd8 }, /* Output port control : LVDS ports only */
148                 { 0x30ae, 0x00 }, /* 0x0307 pll_multiplier maximum value on PLL input 9.6MHz ( 19.2MHz is divided on pre_pll_div) */
149                 { 0x30af, 0xd0 }, /* 0x0307 pll_multiplier maximum value on PLL input 9.6MHz ( 19.2MHz is divided on pre_pll_div) */
150                 { 0x322d, 0x04 }, /* Adjusting Processing Image Size to Scaler Toshiba Recommendation Setting */
151                 { 0x3255, 0x0f }, /* Horizontal Noise Reduction Control Toshiba Recommendation Setting */
152                 { 0x3256, 0x15 }, /* Horizontal Noise Reduction Control Toshiba Recommendation Setting */
153                 { 0x3258, 0x70 }, /* Analog Gain Control Toshiba Recommendation Setting */
154                 { 0x3259, 0x70 }, /* Analog Gain Control Toshiba Recommendation Setting */
155                 { 0x325f, 0x7c }, /* Analog Gain Control Toshiba Recommendation Setting */
156                 { 0x3302, 0x06 }, /* Pixel Reference Voltage Control Toshiba Recommendation Setting */
157                 { 0x3304, 0x00 }, /* Pixel Reference Voltage Control Toshiba Recommendation Setting */
158                 { 0x3307, 0x22 }, /* Pixel Reference Voltage Control Toshiba Recommendation Setting */
159                 { 0x3308, 0x8d }, /* Pixel Reference Voltage Control Toshiba Recommendation Setting */
160                 { 0x331e, 0x0f }, /* Black Hole Sun Correction Control Toshiba Recommendation Setting */
161                 { 0x3320, 0x30 }, /* Black Hole Sun Correction Control Toshiba Recommendation Setting */
162                 { 0x3321, 0x11 }, /* Black Hole Sun Correction Control Toshiba Recommendation Setting */
163                 { 0x3322, 0x98 }, /* Black Hole Sun Correction Control Toshiba Recommendation Setting */
164                 { 0x3323, 0x64 }, /* Black Hole Sun Correction Control Toshiba Recommendation Setting */
165                 { 0x3325, 0x83 }, /* Read Out Timing Control Toshiba Recommendation Setting */
166                 { 0x3330, 0x18 }, /* Read Out Timing Control Toshiba Recommendation Setting */
167                 { 0x333c, 0x01 }, /* Read Out Timing Control Toshiba Recommendation Setting */
168                 { 0x3345, 0x2f }, /* Black Hole Sun Correction Control Toshiba Recommendation Setting */
169                 { 0x33de, 0x38 }, /* Horizontal Noise Reduction Control Toshiba Recommendation Setting */
170                 /* Taken from v03. No idea what the rest are. */
171                 { 0x32e0, 0x05 },
172                 { 0x32e1, 0x05 },
173                 { 0x32e2, 0x04 },
174                 { 0x32e5, 0x04 },
175                 { 0x32e6, 0x04 },
176
177         };
178
179         return smiapp_write_8s(sensor, regs, ARRAY_SIZE(regs));
180 }
181
182 const struct smiapp_quirk smiapp_jt8ew9_quirk = {
183         .limits = jt8ew9_limits,
184         .post_poweron = jt8ew9_post_poweron,
185 };
186
187 static int imx125es_post_poweron(struct smiapp_sensor *sensor)
188 {
189         /* Taken from v02. No idea what the other two are. */
190         struct smiapp_reg_8 regs[] = {
191                 /*
192                  * 0x3302: clk during frame blanking:
193                  * 0x00 - HS mode, 0x01 - LP11
194                  */
195                 { 0x3302, 0x01 },
196                 { 0x302d, 0x00 },
197                 { 0x3b08, 0x8c },
198         };
199
200         return smiapp_write_8s(sensor, regs, ARRAY_SIZE(regs));
201 }
202
203 const struct smiapp_quirk smiapp_imx125es_quirk = {
204         .post_poweron = imx125es_post_poweron,
205 };
206
207 static int jt8ev1_limits(struct smiapp_sensor *sensor)
208 {
209         smiapp_replace_limit(sensor, SMIAPP_LIMIT_X_ADDR_MAX, 4271);
210         smiapp_replace_limit(sensor,
211                              SMIAPP_LIMIT_MIN_LINE_BLANKING_PCK_BIN, 184);
212
213         return 0;
214 }
215
216 static int jt8ev1_post_poweron(struct smiapp_sensor *sensor)
217 {
218         struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
219         int rval;
220
221         struct smiapp_reg_8 regs[] = {
222                 { 0x3031, 0xcd }, /* For digital binning (EQ_MONI) */
223                 { 0x30a3, 0xd0 }, /* FLASH STROBE enable */
224                 { 0x3237, 0x00 }, /* For control of pulse timing for ADC */
225                 { 0x3238, 0x43 },
226                 { 0x3301, 0x06 }, /* For analog bias for sensor */
227                 { 0x3302, 0x06 },
228                 { 0x3304, 0x00 },
229                 { 0x3305, 0x88 },
230                 { 0x332a, 0x14 },
231                 { 0x332c, 0x6b },
232                 { 0x3336, 0x01 },
233                 { 0x333f, 0x1f },
234                 { 0x3355, 0x00 },
235                 { 0x3356, 0x20 },
236                 { 0x33bf, 0x20 }, /* Adjust the FBC speed */
237                 { 0x33c9, 0x20 },
238                 { 0x33ce, 0x30 }, /* Adjust the parameter for logic function */
239                 { 0x33cf, 0xec }, /* For Black sun */
240                 { 0x3328, 0x80 }, /* Ugh. No idea what's this. */
241         };
242
243         struct smiapp_reg_8 regs_96[] = {
244                 { 0x30ae, 0x00 }, /* For control of ADC clock */
245                 { 0x30af, 0xd0 },
246                 { 0x30b0, 0x01 },
247         };
248
249         rval = smiapp_write_8s(sensor, regs, ARRAY_SIZE(regs));
250         if (rval < 0)
251                 return rval;
252
253         switch (sensor->platform_data->ext_clk) {
254         case 9600000:
255                 return smiapp_write_8s(sensor, regs_96,
256                                        ARRAY_SIZE(regs_96));
257         default:
258                 dev_warn(&client->dev, "no MSRs for %d Hz ext_clk\n",
259                          sensor->platform_data->ext_clk);
260                 return 0;
261         }
262 }
263
264 static int jt8ev1_pre_streamon(struct smiapp_sensor *sensor)
265 {
266         return smiapp_write_8(sensor, 0x3328, 0x00);
267 }
268
269 static int jt8ev1_post_streamoff(struct smiapp_sensor *sensor)
270 {
271         int rval;
272
273         /* Workaround: allows fast standby to work properly */
274         rval = smiapp_write_8(sensor, 0x3205, 0x04);
275         if (rval < 0)
276                 return rval;
277
278         /* Wait for 1 ms + one line => 2 ms is likely enough */
279         usleep_range(2000, 2000);
280
281         /* Restore it */
282         rval = smiapp_write_8(sensor, 0x3205, 0x00);
283         if (rval < 0)
284                 return rval;
285
286         return smiapp_write_8(sensor, 0x3328, 0x80);
287 }
288
289 const struct smiapp_quirk smiapp_jt8ev1_quirk = {
290         .limits = jt8ev1_limits,
291         .post_poweron = jt8ev1_post_poweron,
292         .pre_streamon = jt8ev1_pre_streamon,
293         .post_streamoff = jt8ev1_post_streamoff,
294         .flags = SMIAPP_QUIRK_FLAG_OP_PIX_CLOCK_PER_LANE,
295 };
296
297 static int tcm8500md_limits(struct smiapp_sensor *sensor)
298 {
299         smiapp_replace_limit(sensor, SMIAPP_LIMIT_MIN_PLL_IP_FREQ_HZ, 2700000);
300
301         return 0;
302 }
303
304 const struct smiapp_quirk smiapp_tcm8500md_quirk = {
305         .limits = tcm8500md_limits,
306 };