27d1e9f6a93388bacb67070b844e243956b2a1d2
[linux-2.6-microblaze.git] / drivers / staging / media / atomisp / pci / atomisp_v4l2.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Support for Medifield PNW Camera Imaging ISP subsystem.
4  *
5  * Copyright (c) 2010-2017 Intel Corporation. All Rights Reserved.
6  *
7  * Copyright (c) 2010 Silicon Hive www.siliconhive.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 version
11  * 2 as published by the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  *
19  */
20 #include <linux/module.h>
21 #include <linux/pci.h>
22 #include <linux/pm_runtime.h>
23 #include <linux/pm_qos.h>
24 #include <linux/timer.h>
25 #include <linux/delay.h>
26 #include <linux/dmi.h>
27 #include <linux/interrupt.h>
28
29 #include <asm/iosf_mbi.h>
30
31 #include "../../include/linux/atomisp_gmin_platform.h"
32
33 #include "atomisp_cmd.h"
34 #include "atomisp_common.h"
35 #include "atomisp_fops.h"
36 #include "atomisp_file.h"
37 #include "atomisp_ioctl.h"
38 #include "atomisp_internal.h"
39 #include "atomisp_acc.h"
40 #include "atomisp-regs.h"
41 #include "atomisp_dfs_tables.h"
42 #include "atomisp_drvfs.h"
43 #include "hmm/hmm.h"
44 #include "atomisp_trace_event.h"
45
46 #include "sh_css_firmware.h"
47
48 #include "device_access.h"
49
50 /* Timeouts to wait for all subdevs to be registered */
51 #define SUBDEV_WAIT_TIMEOUT             50 /* ms */
52 #define SUBDEV_WAIT_TIMEOUT_MAX_COUNT   40 /* up to 2 seconds */
53
54 /* G-Min addition: pull this in from intel_mid_pm.h */
55 #define CSTATE_EXIT_LATENCY_C1  1
56
57 static uint skip_fwload;
58 module_param(skip_fwload, uint, 0644);
59 MODULE_PARM_DESC(skip_fwload, "Skip atomisp firmware load");
60
61 /* set reserved memory pool size in page */
62 static unsigned int repool_pgnr = 32768;
63 module_param(repool_pgnr, uint, 0644);
64 MODULE_PARM_DESC(repool_pgnr,
65                  "Set the reserved memory pool size in page (default:32768)");
66
67 /* set dynamic memory pool size in page */
68 unsigned int dypool_pgnr = UINT_MAX;
69 module_param(dypool_pgnr, uint, 0644);
70 MODULE_PARM_DESC(dypool_pgnr,
71                  "Set the dynamic memory pool size in page (default: unlimited)");
72
73 bool dypool_enable = true;
74 module_param(dypool_enable, bool, 0644);
75 MODULE_PARM_DESC(dypool_enable,
76                  "dynamic memory pool enable/disable (default:enabled)");
77
78 /* memory optimization: deferred firmware loading */
79 bool defer_fw_load;
80 module_param(defer_fw_load, bool, 0644);
81 MODULE_PARM_DESC(defer_fw_load,
82                  "Defer FW loading until device is opened (default:disable)");
83
84 /* cross componnet debug message flag */
85 int dbg_level;
86 module_param(dbg_level, int, 0644);
87 MODULE_PARM_DESC(dbg_level, "debug message level (default:0)");
88
89 /* log function switch */
90 int dbg_func = 2;
91 module_param(dbg_func, int, 0644);
92 MODULE_PARM_DESC(dbg_func,
93                  "log function switch non/trace_printk/printk (default:printk)");
94
95 int mipicsi_flag;
96 module_param(mipicsi_flag, int, 0644);
97 MODULE_PARM_DESC(mipicsi_flag, "mipi csi compression predictor algorithm");
98
99 static char firmware_name[256];
100 module_param_string(firmware_name, firmware_name, sizeof(firmware_name), 0);
101 MODULE_PARM_DESC(firmware_name, "Firmware file name. Allows overriding the default firmware name.");
102
103 /*set to 16x16 since this is the amount of lines and pixels the sensor
104 exports extra. If these are kept at the 10x8 that they were on, in yuv
105 downscaling modes incorrect resolutions where requested to the sensor
106 driver with strange outcomes as a result. The proper way tot do this
107 would be to have a list of tables the specify the sensor res, mipi rec,
108 output res, and isp output res. however since we do not have this yet,
109 the chosen solution is the next best thing. */
110 int pad_w = 16;
111 module_param(pad_w, int, 0644);
112 MODULE_PARM_DESC(pad_w, "extra data for ISP processing");
113
114 int pad_h = 16;
115 module_param(pad_h, int, 0644);
116 MODULE_PARM_DESC(pad_h, "extra data for ISP processing");
117
118 /*
119  * FIXME: this is a hack to make easier to support ISP2401 variant.
120  * As a given system will either be ISP2401 or not, we can just use
121  * a boolean, in order to replace existing #ifdef ISP2401 everywhere.
122  *
123  * Once this driver gets into a better shape, however, the best would
124  * be to replace this to something stored inside atomisp allocated
125  * structures.
126  */
127 bool atomisp_hw_is_isp2401;
128
129 /* Types of atomisp hardware */
130 #define HW_IS_ISP2400 0
131 #define HW_IS_ISP2401 1
132
133 struct device *atomisp_dev;
134
135 void __iomem *atomisp_io_base;
136
137 static const struct atomisp_freq_scaling_rule dfs_rules_merr[] = {
138         {
139                 .width = ISP_FREQ_RULE_ANY,
140                 .height = ISP_FREQ_RULE_ANY,
141                 .fps = ISP_FREQ_RULE_ANY,
142                 .isp_freq = ISP_FREQ_400MHZ,
143                 .run_mode = ATOMISP_RUN_MODE_VIDEO,
144         },
145         {
146                 .width = ISP_FREQ_RULE_ANY,
147                 .height = ISP_FREQ_RULE_ANY,
148                 .fps = ISP_FREQ_RULE_ANY,
149                 .isp_freq = ISP_FREQ_400MHZ,
150                 .run_mode = ATOMISP_RUN_MODE_STILL_CAPTURE,
151         },
152         {
153                 .width = ISP_FREQ_RULE_ANY,
154                 .height = ISP_FREQ_RULE_ANY,
155                 .fps = ISP_FREQ_RULE_ANY,
156                 .isp_freq = ISP_FREQ_400MHZ,
157                 .run_mode = ATOMISP_RUN_MODE_CONTINUOUS_CAPTURE,
158         },
159         {
160                 .width = ISP_FREQ_RULE_ANY,
161                 .height = ISP_FREQ_RULE_ANY,
162                 .fps = ISP_FREQ_RULE_ANY,
163                 .isp_freq = ISP_FREQ_400MHZ,
164                 .run_mode = ATOMISP_RUN_MODE_PREVIEW,
165         },
166         {
167                 .width = ISP_FREQ_RULE_ANY,
168                 .height = ISP_FREQ_RULE_ANY,
169                 .fps = ISP_FREQ_RULE_ANY,
170                 .isp_freq = ISP_FREQ_457MHZ,
171                 .run_mode = ATOMISP_RUN_MODE_SDV,
172         },
173 };
174
175 /* Merrifield and Moorefield DFS rules */
176 static const struct atomisp_dfs_config dfs_config_merr = {
177         .lowest_freq = ISP_FREQ_200MHZ,
178         .max_freq_at_vmin = ISP_FREQ_400MHZ,
179         .highest_freq = ISP_FREQ_457MHZ,
180         .dfs_table = dfs_rules_merr,
181         .dfs_table_size = ARRAY_SIZE(dfs_rules_merr),
182 };
183
184 static const struct atomisp_freq_scaling_rule dfs_rules_merr_1179[] = {
185         {
186                 .width = ISP_FREQ_RULE_ANY,
187                 .height = ISP_FREQ_RULE_ANY,
188                 .fps = ISP_FREQ_RULE_ANY,
189                 .isp_freq = ISP_FREQ_400MHZ,
190                 .run_mode = ATOMISP_RUN_MODE_VIDEO,
191         },
192         {
193                 .width = ISP_FREQ_RULE_ANY,
194                 .height = ISP_FREQ_RULE_ANY,
195                 .fps = ISP_FREQ_RULE_ANY,
196                 .isp_freq = ISP_FREQ_400MHZ,
197                 .run_mode = ATOMISP_RUN_MODE_STILL_CAPTURE,
198         },
199         {
200                 .width = ISP_FREQ_RULE_ANY,
201                 .height = ISP_FREQ_RULE_ANY,
202                 .fps = ISP_FREQ_RULE_ANY,
203                 .isp_freq = ISP_FREQ_400MHZ,
204                 .run_mode = ATOMISP_RUN_MODE_CONTINUOUS_CAPTURE,
205         },
206         {
207                 .width = ISP_FREQ_RULE_ANY,
208                 .height = ISP_FREQ_RULE_ANY,
209                 .fps = ISP_FREQ_RULE_ANY,
210                 .isp_freq = ISP_FREQ_400MHZ,
211                 .run_mode = ATOMISP_RUN_MODE_PREVIEW,
212         },
213         {
214                 .width = ISP_FREQ_RULE_ANY,
215                 .height = ISP_FREQ_RULE_ANY,
216                 .fps = ISP_FREQ_RULE_ANY,
217                 .isp_freq = ISP_FREQ_400MHZ,
218                 .run_mode = ATOMISP_RUN_MODE_SDV,
219         },
220 };
221
222 static const struct atomisp_dfs_config dfs_config_merr_1179 = {
223         .lowest_freq = ISP_FREQ_200MHZ,
224         .max_freq_at_vmin = ISP_FREQ_400MHZ,
225         .highest_freq = ISP_FREQ_400MHZ,
226         .dfs_table = dfs_rules_merr_1179,
227         .dfs_table_size = ARRAY_SIZE(dfs_rules_merr_1179),
228 };
229
230 static const struct atomisp_freq_scaling_rule dfs_rules_merr_117a[] = {
231         {
232                 .width = 1920,
233                 .height = 1080,
234                 .fps = 30,
235                 .isp_freq = ISP_FREQ_266MHZ,
236                 .run_mode = ATOMISP_RUN_MODE_VIDEO,
237         },
238         {
239                 .width = 1080,
240                 .height = 1920,
241                 .fps = 30,
242                 .isp_freq = ISP_FREQ_266MHZ,
243                 .run_mode = ATOMISP_RUN_MODE_VIDEO,
244         },
245         {
246                 .width = 1920,
247                 .height = 1080,
248                 .fps = 45,
249                 .isp_freq = ISP_FREQ_320MHZ,
250                 .run_mode = ATOMISP_RUN_MODE_VIDEO,
251         },
252         {
253                 .width = 1080,
254                 .height = 1920,
255                 .fps = 45,
256                 .isp_freq = ISP_FREQ_320MHZ,
257                 .run_mode = ATOMISP_RUN_MODE_VIDEO,
258         },
259         {
260                 .width = ISP_FREQ_RULE_ANY,
261                 .height = ISP_FREQ_RULE_ANY,
262                 .fps = 60,
263                 .isp_freq = ISP_FREQ_356MHZ,
264                 .run_mode = ATOMISP_RUN_MODE_VIDEO,
265         },
266         {
267                 .width = ISP_FREQ_RULE_ANY,
268                 .height = ISP_FREQ_RULE_ANY,
269                 .fps = ISP_FREQ_RULE_ANY,
270                 .isp_freq = ISP_FREQ_200MHZ,
271                 .run_mode = ATOMISP_RUN_MODE_VIDEO,
272         },
273         {
274                 .width = ISP_FREQ_RULE_ANY,
275                 .height = ISP_FREQ_RULE_ANY,
276                 .fps = ISP_FREQ_RULE_ANY,
277                 .isp_freq = ISP_FREQ_400MHZ,
278                 .run_mode = ATOMISP_RUN_MODE_STILL_CAPTURE,
279         },
280         {
281                 .width = ISP_FREQ_RULE_ANY,
282                 .height = ISP_FREQ_RULE_ANY,
283                 .fps = ISP_FREQ_RULE_ANY,
284                 .isp_freq = ISP_FREQ_400MHZ,
285                 .run_mode = ATOMISP_RUN_MODE_CONTINUOUS_CAPTURE,
286         },
287         {
288                 .width = ISP_FREQ_RULE_ANY,
289                 .height = ISP_FREQ_RULE_ANY,
290                 .fps = ISP_FREQ_RULE_ANY,
291                 .isp_freq = ISP_FREQ_200MHZ,
292                 .run_mode = ATOMISP_RUN_MODE_PREVIEW,
293         },
294         {
295                 .width = ISP_FREQ_RULE_ANY,
296                 .height = ISP_FREQ_RULE_ANY,
297                 .fps = ISP_FREQ_RULE_ANY,
298                 .isp_freq = ISP_FREQ_400MHZ,
299                 .run_mode = ATOMISP_RUN_MODE_SDV,
300         },
301 };
302
303 static struct atomisp_dfs_config dfs_config_merr_117a = {
304         .lowest_freq = ISP_FREQ_200MHZ,
305         .max_freq_at_vmin = ISP_FREQ_200MHZ,
306         .highest_freq = ISP_FREQ_400MHZ,
307         .dfs_table = dfs_rules_merr_117a,
308         .dfs_table_size = ARRAY_SIZE(dfs_rules_merr_117a),
309 };
310
311 static const struct atomisp_freq_scaling_rule dfs_rules_byt[] = {
312         {
313                 .width = ISP_FREQ_RULE_ANY,
314                 .height = ISP_FREQ_RULE_ANY,
315                 .fps = ISP_FREQ_RULE_ANY,
316                 .isp_freq = ISP_FREQ_400MHZ,
317                 .run_mode = ATOMISP_RUN_MODE_VIDEO,
318         },
319         {
320                 .width = ISP_FREQ_RULE_ANY,
321                 .height = ISP_FREQ_RULE_ANY,
322                 .fps = ISP_FREQ_RULE_ANY,
323                 .isp_freq = ISP_FREQ_400MHZ,
324                 .run_mode = ATOMISP_RUN_MODE_STILL_CAPTURE,
325         },
326         {
327                 .width = ISP_FREQ_RULE_ANY,
328                 .height = ISP_FREQ_RULE_ANY,
329                 .fps = ISP_FREQ_RULE_ANY,
330                 .isp_freq = ISP_FREQ_400MHZ,
331                 .run_mode = ATOMISP_RUN_MODE_CONTINUOUS_CAPTURE,
332         },
333         {
334                 .width = ISP_FREQ_RULE_ANY,
335                 .height = ISP_FREQ_RULE_ANY,
336                 .fps = ISP_FREQ_RULE_ANY,
337                 .isp_freq = ISP_FREQ_400MHZ,
338                 .run_mode = ATOMISP_RUN_MODE_PREVIEW,
339         },
340         {
341                 .width = ISP_FREQ_RULE_ANY,
342                 .height = ISP_FREQ_RULE_ANY,
343                 .fps = ISP_FREQ_RULE_ANY,
344                 .isp_freq = ISP_FREQ_400MHZ,
345                 .run_mode = ATOMISP_RUN_MODE_SDV,
346         },
347 };
348
349 static const struct atomisp_dfs_config dfs_config_byt = {
350         .lowest_freq = ISP_FREQ_200MHZ,
351         .max_freq_at_vmin = ISP_FREQ_400MHZ,
352         .highest_freq = ISP_FREQ_400MHZ,
353         .dfs_table = dfs_rules_byt,
354         .dfs_table_size = ARRAY_SIZE(dfs_rules_byt),
355 };
356
357 static const struct atomisp_freq_scaling_rule dfs_rules_byt_cr[] = {
358         {
359                 .width = ISP_FREQ_RULE_ANY,
360                 .height = ISP_FREQ_RULE_ANY,
361                 .fps = ISP_FREQ_RULE_ANY,
362                 .isp_freq = ISP_FREQ_320MHZ,
363                 .run_mode = ATOMISP_RUN_MODE_VIDEO,
364         },
365         {
366                 .width = ISP_FREQ_RULE_ANY,
367                 .height = ISP_FREQ_RULE_ANY,
368                 .fps = ISP_FREQ_RULE_ANY,
369                 .isp_freq = ISP_FREQ_320MHZ,
370                 .run_mode = ATOMISP_RUN_MODE_STILL_CAPTURE,
371         },
372         {
373                 .width = ISP_FREQ_RULE_ANY,
374                 .height = ISP_FREQ_RULE_ANY,
375                 .fps = ISP_FREQ_RULE_ANY,
376                 .isp_freq = ISP_FREQ_320MHZ,
377                 .run_mode = ATOMISP_RUN_MODE_CONTINUOUS_CAPTURE,
378         },
379         {
380                 .width = ISP_FREQ_RULE_ANY,
381                 .height = ISP_FREQ_RULE_ANY,
382                 .fps = ISP_FREQ_RULE_ANY,
383                 .isp_freq = ISP_FREQ_320MHZ,
384                 .run_mode = ATOMISP_RUN_MODE_PREVIEW,
385         },
386         {
387                 .width = ISP_FREQ_RULE_ANY,
388                 .height = ISP_FREQ_RULE_ANY,
389                 .fps = ISP_FREQ_RULE_ANY,
390                 .isp_freq = ISP_FREQ_320MHZ,
391                 .run_mode = ATOMISP_RUN_MODE_SDV,
392         },
393 };
394
395 #ifdef FIXME
396 /*
397  * Disable this, as it is used only when this is true:
398  *      INTEL_MID_BOARD(3, TABLET, BYT, BLK, PRO, CRV2) ||
399  *      INTEL_MID_BOARD(3, TABLET, BYT, BLK, ENG, CRV2))
400  * However, the original code is commented
401  */
402 static const struct atomisp_dfs_config dfs_config_byt_cr = {
403         .lowest_freq = ISP_FREQ_200MHZ,
404         .max_freq_at_vmin = ISP_FREQ_320MHZ,
405         .highest_freq = ISP_FREQ_320MHZ,
406         .dfs_table = dfs_rules_byt_cr,
407         .dfs_table_size = ARRAY_SIZE(dfs_rules_byt_cr),
408 };
409 #endif
410
411 static const struct atomisp_freq_scaling_rule dfs_rules_cht[] = {
412         {
413                 .width = ISP_FREQ_RULE_ANY,
414                 .height = ISP_FREQ_RULE_ANY,
415                 .fps = ISP_FREQ_RULE_ANY,
416                 .isp_freq = ISP_FREQ_320MHZ,
417                 .run_mode = ATOMISP_RUN_MODE_VIDEO,
418         },
419         {
420                 .width = ISP_FREQ_RULE_ANY,
421                 .height = ISP_FREQ_RULE_ANY,
422                 .fps = ISP_FREQ_RULE_ANY,
423                 .isp_freq = ISP_FREQ_356MHZ,
424                 .run_mode = ATOMISP_RUN_MODE_STILL_CAPTURE,
425         },
426         {
427                 .width = ISP_FREQ_RULE_ANY,
428                 .height = ISP_FREQ_RULE_ANY,
429                 .fps = ISP_FREQ_RULE_ANY,
430                 .isp_freq = ISP_FREQ_320MHZ,
431                 .run_mode = ATOMISP_RUN_MODE_CONTINUOUS_CAPTURE,
432         },
433         {
434                 .width = ISP_FREQ_RULE_ANY,
435                 .height = ISP_FREQ_RULE_ANY,
436                 .fps = ISP_FREQ_RULE_ANY,
437                 .isp_freq = ISP_FREQ_320MHZ,
438                 .run_mode = ATOMISP_RUN_MODE_PREVIEW,
439         },
440         {
441                 .width = 1280,
442                 .height = 720,
443                 .fps = ISP_FREQ_RULE_ANY,
444                 .isp_freq = ISP_FREQ_320MHZ,
445                 .run_mode = ATOMISP_RUN_MODE_SDV,
446         },
447         {
448                 .width = ISP_FREQ_RULE_ANY,
449                 .height = ISP_FREQ_RULE_ANY,
450                 .fps = ISP_FREQ_RULE_ANY,
451                 .isp_freq = ISP_FREQ_356MHZ,
452                 .run_mode = ATOMISP_RUN_MODE_SDV,
453         },
454 };
455
456 static const struct atomisp_freq_scaling_rule dfs_rules_cht_soc[] = {
457         {
458                 .width = ISP_FREQ_RULE_ANY,
459                 .height = ISP_FREQ_RULE_ANY,
460                 .fps = ISP_FREQ_RULE_ANY,
461                 .isp_freq = ISP_FREQ_356MHZ,
462                 .run_mode = ATOMISP_RUN_MODE_VIDEO,
463         },
464         {
465                 .width = ISP_FREQ_RULE_ANY,
466                 .height = ISP_FREQ_RULE_ANY,
467                 .fps = ISP_FREQ_RULE_ANY,
468                 .isp_freq = ISP_FREQ_356MHZ,
469                 .run_mode = ATOMISP_RUN_MODE_STILL_CAPTURE,
470         },
471         {
472                 .width = ISP_FREQ_RULE_ANY,
473                 .height = ISP_FREQ_RULE_ANY,
474                 .fps = ISP_FREQ_RULE_ANY,
475                 .isp_freq = ISP_FREQ_320MHZ,
476                 .run_mode = ATOMISP_RUN_MODE_CONTINUOUS_CAPTURE,
477         },
478         {
479                 .width = ISP_FREQ_RULE_ANY,
480                 .height = ISP_FREQ_RULE_ANY,
481                 .fps = ISP_FREQ_RULE_ANY,
482                 .isp_freq = ISP_FREQ_320MHZ,
483                 .run_mode = ATOMISP_RUN_MODE_PREVIEW,
484         },
485         {
486                 .width = ISP_FREQ_RULE_ANY,
487                 .height = ISP_FREQ_RULE_ANY,
488                 .fps = ISP_FREQ_RULE_ANY,
489                 .isp_freq = ISP_FREQ_356MHZ,
490                 .run_mode = ATOMISP_RUN_MODE_SDV,
491         },
492 };
493
494 static const struct atomisp_dfs_config dfs_config_cht = {
495         .lowest_freq = ISP_FREQ_100MHZ,
496         .max_freq_at_vmin = ISP_FREQ_356MHZ,
497         .highest_freq = ISP_FREQ_356MHZ,
498         .dfs_table = dfs_rules_cht,
499         .dfs_table_size = ARRAY_SIZE(dfs_rules_cht),
500 };
501
502 /* This one should be visible also by atomisp_cmd.c */
503 const struct atomisp_dfs_config dfs_config_cht_soc = {
504         .lowest_freq = ISP_FREQ_100MHZ,
505         .max_freq_at_vmin = ISP_FREQ_356MHZ,
506         .highest_freq = ISP_FREQ_356MHZ,
507         .dfs_table = dfs_rules_cht_soc,
508         .dfs_table_size = ARRAY_SIZE(dfs_rules_cht_soc),
509 };
510
511 int atomisp_video_init(struct atomisp_video_pipe *video, const char *name)
512 {
513         int ret;
514         const char *direction;
515
516         switch (video->type) {
517         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
518                 direction = "output";
519                 video->pad.flags = MEDIA_PAD_FL_SINK;
520                 video->vdev.fops = &atomisp_fops;
521                 video->vdev.ioctl_ops = &atomisp_ioctl_ops;
522                 break;
523         case V4L2_BUF_TYPE_VIDEO_OUTPUT:
524                 direction = "input";
525                 video->pad.flags = MEDIA_PAD_FL_SOURCE;
526                 video->vdev.fops = &atomisp_file_fops;
527                 video->vdev.ioctl_ops = &atomisp_file_ioctl_ops;
528                 break;
529         default:
530                 return -EINVAL;
531         }
532
533         ret = media_entity_pads_init(&video->vdev.entity, 1, &video->pad);
534         if (ret < 0)
535                 return ret;
536
537         /* Initialize the video device. */
538         snprintf(video->vdev.name, sizeof(video->vdev.name),
539                  "ATOMISP ISP %s %s", name, direction);
540         video->vdev.release = video_device_release_empty;
541         video_set_drvdata(&video->vdev, video->isp);
542
543         return 0;
544 }
545
546 void atomisp_acc_init(struct atomisp_acc_pipe *video, const char *name)
547 {
548         video->vdev.fops = &atomisp_fops;
549         video->vdev.ioctl_ops = &atomisp_ioctl_ops;
550
551         /* Initialize the video device. */
552         snprintf(video->vdev.name, sizeof(video->vdev.name),
553                  "ATOMISP ISP %s", name);
554         video->vdev.release = video_device_release_empty;
555         video_set_drvdata(&video->vdev, video->isp);
556 }
557
558 void atomisp_video_unregister(struct atomisp_video_pipe *video)
559 {
560         if (video_is_registered(&video->vdev)) {
561                 media_entity_cleanup(&video->vdev.entity);
562                 video_unregister_device(&video->vdev);
563         }
564 }
565
566 void atomisp_acc_unregister(struct atomisp_acc_pipe *video)
567 {
568         if (video_is_registered(&video->vdev))
569                 video_unregister_device(&video->vdev);
570 }
571
572 static int atomisp_save_iunit_reg(struct atomisp_device *isp)
573 {
574         struct pci_dev *dev = isp->pdev;
575
576         dev_dbg(isp->dev, "%s\n", __func__);
577
578         pci_read_config_word(dev, PCI_COMMAND, &isp->saved_regs.pcicmdsts);
579         /* isp->saved_regs.ispmmadr is set from the atomisp_pci_probe() */
580         pci_read_config_dword(dev, PCI_MSI_CAPID, &isp->saved_regs.msicap);
581         pci_read_config_dword(dev, PCI_MSI_ADDR, &isp->saved_regs.msi_addr);
582         pci_read_config_word(dev, PCI_MSI_DATA,  &isp->saved_regs.msi_data);
583         pci_read_config_byte(dev, PCI_INTERRUPT_LINE, &isp->saved_regs.intr);
584         pci_read_config_dword(dev, PCI_INTERRUPT_CTRL,
585                               &isp->saved_regs.interrupt_control);
586
587         pci_read_config_dword(dev, MRFLD_PCI_PMCS,
588                               &isp->saved_regs.pmcs);
589         /* Ensure read/write combining is enabled. */
590         pci_read_config_dword(dev, PCI_I_CONTROL,
591                               &isp->saved_regs.i_control);
592         isp->saved_regs.i_control |=
593             MRFLD_PCI_I_CONTROL_ENABLE_READ_COMBINING |
594             MRFLD_PCI_I_CONTROL_ENABLE_WRITE_COMBINING;
595         pci_read_config_dword(dev, MRFLD_PCI_CSI_ACCESS_CTRL_VIOL,
596                               &isp->saved_regs.csi_access_viol);
597         pci_read_config_dword(dev, MRFLD_PCI_CSI_RCOMP_CONTROL,
598                               &isp->saved_regs.csi_rcomp_config);
599         /*
600          * Hardware bugs require setting CSI_HS_OVR_CLK_GATE_ON_UPDATE.
601          * ANN/CHV: RCOMP updates do not happen when using CSI2+ path
602          * and sensor sending "continuous clock".
603          * TNG/ANN/CHV: MIPI packets are lost if the HS entry sequence
604          * is missed, and IUNIT can hang.
605          * For both issues, setting this bit is a workaround.
606          */
607         isp->saved_regs.csi_rcomp_config |=
608             MRFLD_PCI_CSI_HS_OVR_CLK_GATE_ON_UPDATE;
609         pci_read_config_dword(dev, MRFLD_PCI_CSI_AFE_TRIM_CONTROL,
610                               &isp->saved_regs.csi_afe_dly);
611         pci_read_config_dword(dev, MRFLD_PCI_CSI_CONTROL,
612                               &isp->saved_regs.csi_control);
613         if (isp->media_dev.hw_revision >=
614             (ATOMISP_HW_REVISION_ISP2401 << ATOMISP_HW_REVISION_SHIFT))
615                 isp->saved_regs.csi_control |=
616                     MRFLD_PCI_CSI_CONTROL_PARPATHEN;
617         /*
618          * On CHT CSI_READY bit should be enabled before stream on
619          */
620         if (IS_CHT && (isp->media_dev.hw_revision >= ((ATOMISP_HW_REVISION_ISP2401 <<
621                        ATOMISP_HW_REVISION_SHIFT) | ATOMISP_HW_STEPPING_B0)))
622                 isp->saved_regs.csi_control |=
623                     MRFLD_PCI_CSI_CONTROL_CSI_READY;
624         pci_read_config_dword(dev, MRFLD_PCI_CSI_AFE_RCOMP_CONTROL,
625                               &isp->saved_regs.csi_afe_rcomp_config);
626         pci_read_config_dword(dev, MRFLD_PCI_CSI_AFE_HS_CONTROL,
627                               &isp->saved_regs.csi_afe_hs_control);
628         pci_read_config_dword(dev, MRFLD_PCI_CSI_DEADLINE_CONTROL,
629                               &isp->saved_regs.csi_deadline_control);
630         return 0;
631 }
632
633 static int __maybe_unused atomisp_restore_iunit_reg(struct atomisp_device *isp)
634 {
635         struct pci_dev *dev = isp->pdev;
636
637         dev_dbg(isp->dev, "%s\n", __func__);
638
639         pci_write_config_word(dev, PCI_COMMAND, isp->saved_regs.pcicmdsts);
640         pci_write_config_dword(dev, PCI_BASE_ADDRESS_0,
641                                isp->saved_regs.ispmmadr);
642         pci_write_config_dword(dev, PCI_MSI_CAPID, isp->saved_regs.msicap);
643         pci_write_config_dword(dev, PCI_MSI_ADDR, isp->saved_regs.msi_addr);
644         pci_write_config_word(dev, PCI_MSI_DATA, isp->saved_regs.msi_data);
645         pci_write_config_byte(dev, PCI_INTERRUPT_LINE, isp->saved_regs.intr);
646         pci_write_config_dword(dev, PCI_INTERRUPT_CTRL,
647                                isp->saved_regs.interrupt_control);
648         pci_write_config_dword(dev, PCI_I_CONTROL,
649                                isp->saved_regs.i_control);
650
651         pci_write_config_dword(dev, MRFLD_PCI_PMCS,
652                                isp->saved_regs.pmcs);
653         pci_write_config_dword(dev, MRFLD_PCI_CSI_ACCESS_CTRL_VIOL,
654                                isp->saved_regs.csi_access_viol);
655         pci_write_config_dword(dev, MRFLD_PCI_CSI_RCOMP_CONTROL,
656                                isp->saved_regs.csi_rcomp_config);
657         pci_write_config_dword(dev, MRFLD_PCI_CSI_AFE_TRIM_CONTROL,
658                                isp->saved_regs.csi_afe_dly);
659         pci_write_config_dword(dev, MRFLD_PCI_CSI_CONTROL,
660                                isp->saved_regs.csi_control);
661         pci_write_config_dword(dev, MRFLD_PCI_CSI_AFE_RCOMP_CONTROL,
662                                isp->saved_regs.csi_afe_rcomp_config);
663         pci_write_config_dword(dev, MRFLD_PCI_CSI_AFE_HS_CONTROL,
664                                isp->saved_regs.csi_afe_hs_control);
665         pci_write_config_dword(dev, MRFLD_PCI_CSI_DEADLINE_CONTROL,
666                                isp->saved_regs.csi_deadline_control);
667
668         /*
669          * for MRFLD, Software/firmware needs to write a 1 to bit0
670          * of the register at CSI_RECEIVER_SELECTION_REG to enable
671          * SH CSI backend write 0 will enable Arasan CSI backend,
672          * which has bugs(like sighting:4567697 and 4567699) and
673          * will be removed in B0
674          */
675         atomisp_css2_hw_store_32(MRFLD_CSI_RECEIVER_SELECTION_REG, 1);
676         return 0;
677 }
678
679 static int atomisp_mrfld_pre_power_down(struct atomisp_device *isp)
680 {
681         struct pci_dev *dev = isp->pdev;
682         u32 irq;
683         unsigned long flags;
684
685         spin_lock_irqsave(&isp->lock, flags);
686         if (isp->sw_contex.power_state == ATOM_ISP_POWER_DOWN) {
687                 spin_unlock_irqrestore(&isp->lock, flags);
688                 dev_dbg(isp->dev, "<%s %d.\n", __func__, __LINE__);
689                 return 0;
690         }
691         /*
692          * MRFLD HAS requirement: cannot power off i-unit if
693          * ISP has IRQ not serviced.
694          * So, here we need to check if there is any pending
695          * IRQ, if so, waiting for it to be served
696          */
697         pci_read_config_dword(dev, PCI_INTERRUPT_CTRL, &irq);
698         irq = irq & 1 << INTR_IIR;
699         pci_write_config_dword(dev, PCI_INTERRUPT_CTRL, irq);
700
701         pci_read_config_dword(dev, PCI_INTERRUPT_CTRL, &irq);
702         if (!(irq & (1 << INTR_IIR)))
703                 goto done;
704
705         atomisp_css2_hw_store_32(MRFLD_INTR_CLEAR_REG, 0xFFFFFFFF);
706         atomisp_load_uint32(MRFLD_INTR_STATUS_REG, &irq);
707         if (irq != 0) {
708                 dev_err(isp->dev,
709                         "%s: fail to clear isp interrupt status reg=0x%x\n",
710                         __func__, irq);
711                 spin_unlock_irqrestore(&isp->lock, flags);
712                 return -EAGAIN;
713         } else {
714                 pci_read_config_dword(dev, PCI_INTERRUPT_CTRL, &irq);
715                 irq = irq & 1 << INTR_IIR;
716                 pci_write_config_dword(dev, PCI_INTERRUPT_CTRL, irq);
717
718                 pci_read_config_dword(dev, PCI_INTERRUPT_CTRL, &irq);
719                 if (!(irq & (1 << INTR_IIR))) {
720                         atomisp_css2_hw_store_32(MRFLD_INTR_ENABLE_REG, 0x0);
721                         goto done;
722                 }
723                 dev_err(isp->dev,
724                         "%s: error in iunit interrupt. status reg=0x%x\n",
725                         __func__, irq);
726                 spin_unlock_irqrestore(&isp->lock, flags);
727                 return -EAGAIN;
728         }
729 done:
730         /*
731         * MRFLD WORKAROUND:
732         * before powering off IUNIT, clear the pending interrupts
733         * and disable the interrupt. driver should avoid writing 0
734         * to IIR. It could block subsequent interrupt messages.
735         * HW sighting:4568410.
736         */
737         pci_read_config_dword(dev, PCI_INTERRUPT_CTRL, &irq);
738         irq &= ~(1 << INTR_IER);
739         pci_write_config_dword(dev, PCI_INTERRUPT_CTRL, irq);
740
741         atomisp_msi_irq_uninit(isp, dev);
742         atomisp_freq_scaling(isp, ATOMISP_DFS_MODE_LOW, true);
743         spin_unlock_irqrestore(&isp->lock, flags);
744
745         return 0;
746 }
747
748 /*
749 * WA for DDR DVFS enable/disable
750 * By default, ISP will force DDR DVFS 1600MHz before disable DVFS
751 */
752 static void punit_ddr_dvfs_enable(bool enable)
753 {
754         int door_bell = 1 << 8;
755         int max_wait = 30;
756         int reg;
757
758         iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, MRFLD_ISPSSDVFS, &reg);
759         if (enable) {
760                 reg &= ~(MRFLD_BIT0 | MRFLD_BIT1);
761         } else {
762                 reg |= (MRFLD_BIT1 | door_bell);
763                 reg &= ~(MRFLD_BIT0);
764         }
765         iosf_mbi_write(BT_MBI_UNIT_PMC, MBI_REG_WRITE, MRFLD_ISPSSDVFS, reg);
766
767         /* Check Req_ACK to see freq status, wait until door_bell is cleared */
768         while ((reg & door_bell) && max_wait--) {
769                 iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, MRFLD_ISPSSDVFS, &reg);
770                 usleep_range(100, 500);
771         }
772
773         if (max_wait == -1)
774                 pr_info("DDR DVFS, door bell is not cleared within 3ms\n");
775 }
776
777 static int atomisp_mrfld_power(struct atomisp_device *isp, bool enable)
778 {
779         unsigned long timeout;
780         u32 val = enable ? MRFLD_ISPSSPM0_IUNIT_POWER_ON :
781                            MRFLD_ISPSSPM0_IUNIT_POWER_OFF;
782
783         dev_dbg(isp->dev, "IUNIT power-%s.\n", enable ? "on" : "off");
784
785         /*WA:Enable DVFS*/
786         if (IS_CHT && enable)
787                 punit_ddr_dvfs_enable(true);
788
789         /*
790          * FIXME:WA for ECS28A, with this sleep, CTS
791          * android.hardware.camera2.cts.CameraDeviceTest#testCameraDeviceAbort
792          * PASS, no impact on other platforms
793         */
794         if (IS_BYT && enable)
795                 msleep(10);
796
797         /* Write to ISPSSPM0 bit[1:0] to power on/off the IUNIT */
798         iosf_mbi_modify(BT_MBI_UNIT_PMC, MBI_REG_READ, MRFLD_ISPSSPM0,
799                         val, MRFLD_ISPSSPM0_ISPSSC_MASK);
800
801         /*WA:Enable DVFS*/
802         if (IS_CHT && !enable)
803                 punit_ddr_dvfs_enable(true);
804
805         /*
806          * There should be no IUNIT access while power-down is
807          * in progress. HW sighting: 4567865.
808          * Wait up to 50 ms for the IUNIT to shut down.
809          * And we do the same for power on.
810          */
811         timeout = jiffies + msecs_to_jiffies(50);
812         do {
813                 u32 tmp;
814
815                 /* Wait until ISPSSPM0 bit[25:24] shows the right value */
816                 iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, MRFLD_ISPSSPM0, &tmp);
817                 tmp = (tmp & MRFLD_ISPSSPM0_ISPSSC_MASK) >> MRFLD_ISPSSPM0_ISPSSS_OFFSET;
818                 if (tmp == val) {
819                         trace_ipu_cstate(enable);
820                         return 0;
821                 }
822
823                 if (time_after(jiffies, timeout))
824                         break;
825
826                 /* FIXME: experienced value for delay */
827                 usleep_range(100, 150);
828         } while (1);
829
830         if (enable)
831                 msleep(10);
832
833         dev_err(isp->dev, "IUNIT power-%s timeout.\n", enable ? "on" : "off");
834         return -EBUSY;
835 }
836
837 /* Workaround for pmu_nc_set_power_state not ready in MRFLD */
838 int atomisp_mrfld_power_down(struct atomisp_device *isp)
839 {
840         return 0;
841 // FIXME: at least with ISP2401, the code below causes the driver to break
842 //      return atomisp_mrfld_power(isp, false);
843 }
844
845 /* Workaround for pmu_nc_set_power_state not ready in MRFLD */
846 int atomisp_mrfld_power_up(struct atomisp_device *isp)
847 {
848         return 0;
849 // FIXME: at least with ISP2401, the code below causes the driver to break
850 //      return atomisp_mrfld_power(isp, true);
851 }
852
853 int atomisp_runtime_suspend(struct device *dev)
854 {
855         struct atomisp_device *isp = (struct atomisp_device *)
856                                      dev_get_drvdata(dev);
857         int ret;
858
859         ret = atomisp_mrfld_pre_power_down(isp);
860         if (ret)
861                 return ret;
862
863         /*Turn off the ISP d-phy*/
864         ret = atomisp_ospm_dphy_down(isp);
865         if (ret)
866                 return ret;
867         cpu_latency_qos_update_request(&isp->pm_qos, PM_QOS_DEFAULT_VALUE);
868         return atomisp_mrfld_power_down(isp);
869 }
870
871 int atomisp_runtime_resume(struct device *dev)
872 {
873         struct atomisp_device *isp = (struct atomisp_device *)
874                                      dev_get_drvdata(dev);
875         int ret;
876
877         ret = atomisp_mrfld_power_up(isp);
878         if (ret)
879                 return ret;
880
881         cpu_latency_qos_update_request(&isp->pm_qos, isp->max_isr_latency);
882         if (isp->sw_contex.power_state == ATOM_ISP_POWER_DOWN) {
883                 /*Turn on ISP d-phy */
884                 ret = atomisp_ospm_dphy_up(isp);
885                 if (ret) {
886                         dev_err(isp->dev, "Failed to power up ISP!.\n");
887                         return -EINVAL;
888                 }
889         }
890
891         /*restore register values for iUnit and iUnitPHY registers*/
892         if (isp->saved_regs.pcicmdsts)
893                 atomisp_restore_iunit_reg(isp);
894
895         atomisp_freq_scaling(isp, ATOMISP_DFS_MODE_LOW, true);
896         return 0;
897 }
898
899 static int __maybe_unused atomisp_suspend(struct device *dev)
900 {
901         struct atomisp_device *isp = (struct atomisp_device *)
902                                      dev_get_drvdata(dev);
903         /* FIXME: only has one isp_subdev at present */
904         struct atomisp_sub_device *asd = &isp->asd[0];
905         unsigned long flags;
906         int ret;
907
908         /*
909          * FIXME: Suspend is not supported by sensors. Abort if any video
910          * node was opened.
911          */
912         if (atomisp_dev_users(isp))
913                 return -EBUSY;
914
915         spin_lock_irqsave(&isp->lock, flags);
916         if (asd->streaming != ATOMISP_DEVICE_STREAMING_DISABLED) {
917                 spin_unlock_irqrestore(&isp->lock, flags);
918                 dev_err(isp->dev, "atomisp cannot suspend at this time.\n");
919                 return -EINVAL;
920         }
921         spin_unlock_irqrestore(&isp->lock, flags);
922
923         ret = atomisp_mrfld_pre_power_down(isp);
924         if (ret)
925                 return ret;
926
927         /*Turn off the ISP d-phy */
928         ret = atomisp_ospm_dphy_down(isp);
929         if (ret) {
930                 dev_err(isp->dev, "fail to power off ISP\n");
931                 return ret;
932         }
933         cpu_latency_qos_update_request(&isp->pm_qos, PM_QOS_DEFAULT_VALUE);
934         return atomisp_mrfld_power_down(isp);
935 }
936
937 static int __maybe_unused atomisp_resume(struct device *dev)
938 {
939         struct atomisp_device *isp = (struct atomisp_device *)
940                                      dev_get_drvdata(dev);
941         int ret;
942
943         ret = atomisp_mrfld_power_up(isp);
944         if (ret)
945                 return ret;
946
947         cpu_latency_qos_update_request(&isp->pm_qos, isp->max_isr_latency);
948
949         /*Turn on ISP d-phy */
950         ret = atomisp_ospm_dphy_up(isp);
951         if (ret) {
952                 dev_err(isp->dev, "Failed to power up ISP!.\n");
953                 return -EINVAL;
954         }
955
956         /*restore register values for iUnit and iUnitPHY registers*/
957         if (isp->saved_regs.pcicmdsts)
958                 atomisp_restore_iunit_reg(isp);
959
960         atomisp_freq_scaling(isp, ATOMISP_DFS_MODE_LOW, true);
961         return 0;
962 }
963
964 int atomisp_csi_lane_config(struct atomisp_device *isp)
965 {
966         static const struct {
967                 u8 code;
968                 u8 lanes[MRFLD_PORT_NUM];
969         } portconfigs[] = {
970                 /* Tangier/Merrifield available lane configurations */
971                 { 0x00, { 4, 1, 0 } },          /* 00000 */
972                 { 0x01, { 3, 1, 0 } },          /* 00001 */
973                 { 0x02, { 2, 1, 0 } },          /* 00010 */
974                 { 0x03, { 1, 1, 0 } },          /* 00011 */
975                 { 0x04, { 2, 1, 2 } },          /* 00100 */
976                 { 0x08, { 3, 1, 1 } },          /* 01000 */
977                 { 0x09, { 2, 1, 1 } },          /* 01001 */
978                 { 0x0a, { 1, 1, 1 } },          /* 01010 */
979
980                 /* Anniedale/Moorefield only configurations */
981                 { 0x10, { 4, 2, 0 } },          /* 10000 */
982                 { 0x11, { 3, 2, 0 } },          /* 10001 */
983                 { 0x12, { 2, 2, 0 } },          /* 10010 */
984                 { 0x13, { 1, 2, 0 } },          /* 10011 */
985                 { 0x14, { 2, 2, 2 } },          /* 10100 */
986                 { 0x18, { 3, 2, 1 } },          /* 11000 */
987                 { 0x19, { 2, 2, 1 } },          /* 11001 */
988                 { 0x1a, { 1, 2, 1 } },          /* 11010 */
989         };
990
991         unsigned int i, j;
992         u8 sensor_lanes[MRFLD_PORT_NUM] = { 0 };
993         u32 csi_control;
994         int nportconfigs;
995         u32 port_config_mask;
996         int port3_lanes_shift;
997
998         if (isp->media_dev.hw_revision <
999             ATOMISP_HW_REVISION_ISP2401_LEGACY <<
1000             ATOMISP_HW_REVISION_SHIFT) {
1001                 /* Merrifield */
1002                 port_config_mask = MRFLD_PORT_CONFIG_MASK;
1003                 port3_lanes_shift = MRFLD_PORT3_LANES_SHIFT;
1004         } else {
1005                 /* Moorefield / Cherryview */
1006                 port_config_mask = CHV_PORT_CONFIG_MASK;
1007                 port3_lanes_shift = CHV_PORT3_LANES_SHIFT;
1008         }
1009
1010         if (isp->media_dev.hw_revision <
1011             ATOMISP_HW_REVISION_ISP2401 <<
1012             ATOMISP_HW_REVISION_SHIFT) {
1013                 /* Merrifield / Moorefield legacy input system */
1014                 nportconfigs = MRFLD_PORT_CONFIG_NUM;
1015         } else {
1016                 /* Moorefield / Cherryview new input system */
1017                 nportconfigs = ARRAY_SIZE(portconfigs);
1018         }
1019
1020         for (i = 0; i < isp->input_cnt; i++) {
1021                 struct camera_mipi_info *mipi_info;
1022
1023                 if (isp->inputs[i].type != RAW_CAMERA &&
1024                     isp->inputs[i].type != SOC_CAMERA)
1025                         continue;
1026
1027                 mipi_info = atomisp_to_sensor_mipi_info(isp->inputs[i].camera);
1028                 if (!mipi_info)
1029                         continue;
1030
1031                 switch (mipi_info->port) {
1032                 case ATOMISP_CAMERA_PORT_PRIMARY:
1033                         sensor_lanes[0] = mipi_info->num_lanes;
1034                         break;
1035                 case ATOMISP_CAMERA_PORT_SECONDARY:
1036                         sensor_lanes[1] = mipi_info->num_lanes;
1037                         break;
1038                 case ATOMISP_CAMERA_PORT_TERTIARY:
1039                         sensor_lanes[2] = mipi_info->num_lanes;
1040                         break;
1041                 default:
1042                         dev_err(isp->dev,
1043                                 "%s: invalid port: %d for the %dth sensor\n",
1044                                 __func__, mipi_info->port, i);
1045                         return -EINVAL;
1046                 }
1047         }
1048
1049         for (i = 0; i < nportconfigs; i++) {
1050                 for (j = 0; j < MRFLD_PORT_NUM; j++)
1051                         if (sensor_lanes[j] &&
1052                             sensor_lanes[j] != portconfigs[i].lanes[j])
1053                                 break;
1054
1055                 if (j == MRFLD_PORT_NUM)
1056                         break;                  /* Found matching setting */
1057         }
1058
1059         if (i >= nportconfigs) {
1060                 dev_err(isp->dev,
1061                         "%s: could not find the CSI port setting for %d-%d-%d\n",
1062                         __func__,
1063                         sensor_lanes[0], sensor_lanes[1], sensor_lanes[2]);
1064                 return -EINVAL;
1065         }
1066
1067         pci_read_config_dword(isp->pdev, MRFLD_PCI_CSI_CONTROL, &csi_control);
1068         csi_control &= ~port_config_mask;
1069         csi_control |= (portconfigs[i].code << MRFLD_PORT_CONFIGCODE_SHIFT)
1070                        | (portconfigs[i].lanes[0] ? 0 : (1 << MRFLD_PORT1_ENABLE_SHIFT))
1071                        | (portconfigs[i].lanes[1] ? 0 : (1 << MRFLD_PORT2_ENABLE_SHIFT))
1072                        | (portconfigs[i].lanes[2] ? 0 : (1 << MRFLD_PORT3_ENABLE_SHIFT))
1073                        | (((1 << portconfigs[i].lanes[0]) - 1) << MRFLD_PORT1_LANES_SHIFT)
1074                        | (((1 << portconfigs[i].lanes[1]) - 1) << MRFLD_PORT2_LANES_SHIFT)
1075                        | (((1 << portconfigs[i].lanes[2]) - 1) << port3_lanes_shift);
1076
1077         pci_write_config_dword(isp->pdev, MRFLD_PCI_CSI_CONTROL, csi_control);
1078
1079         dev_dbg(isp->dev,
1080                 "%s: the portconfig is %d-%d-%d, CSI_CONTROL is 0x%08X\n",
1081                 __func__, portconfigs[i].lanes[0], portconfigs[i].lanes[1],
1082                 portconfigs[i].lanes[2], csi_control);
1083
1084         return 0;
1085 }
1086
1087 static int atomisp_subdev_probe(struct atomisp_device *isp)
1088 {
1089         const struct atomisp_platform_data *pdata;
1090         struct intel_v4l2_subdev_table *subdevs;
1091         int ret, raw_index = -1, count;
1092
1093         pdata = atomisp_get_platform_data();
1094         if (!pdata) {
1095                 dev_err(isp->dev, "no platform data available\n");
1096                 return 0;
1097         }
1098
1099         /* FIXME: should return -EPROBE_DEFER if not all subdevs were probed */
1100         for (count = 0; count < SUBDEV_WAIT_TIMEOUT_MAX_COUNT; count++) {
1101                 int camera_count = 0;
1102
1103                 for (subdevs = pdata->subdevs; subdevs->type; ++subdevs) {
1104                         if (subdevs->type == RAW_CAMERA ||
1105                             subdevs->type == SOC_CAMERA)
1106                                 camera_count++;
1107                 }
1108                 if (camera_count)
1109                         break;
1110                 msleep(SUBDEV_WAIT_TIMEOUT);
1111         }
1112         /* Wait more time to give more time for subdev init code to finish */
1113         msleep(5 * SUBDEV_WAIT_TIMEOUT);
1114
1115         /* FIXME: should, instead, use I2C probe */
1116
1117         for (subdevs = pdata->subdevs; subdevs->type; ++subdevs) {
1118                 struct v4l2_subdev *subdev;
1119                 struct i2c_board_info *board_info =
1120                             &subdevs->v4l2_subdev.board_info;
1121                 struct i2c_adapter *adapter =
1122                     i2c_get_adapter(subdevs->v4l2_subdev.i2c_adapter_id);
1123                 int sensor_num, i;
1124
1125                 dev_info(isp->dev, "Probing Subdev %s\n", board_info->type);
1126
1127                 if (!adapter) {
1128                         dev_err(isp->dev,
1129                                 "Failed to find i2c adapter for subdev %s\n",
1130                                 board_info->type);
1131                         break;
1132                 }
1133
1134                 /* In G-Min, the sensor devices will already be probed
1135                  * (via ACPI) and registered, do not create new
1136                  * ones */
1137                 subdev = atomisp_gmin_find_subdev(adapter, board_info);
1138                 if (!subdev) {
1139                         dev_warn(isp->dev, "Subdev %s not found\n",
1140                                  board_info->type);
1141                         continue;
1142                 }
1143                 ret = v4l2_device_register_subdev(&isp->v4l2_dev, subdev);
1144                 if (ret) {
1145                         dev_warn(isp->dev, "Subdev %s detection fail\n",
1146                                  board_info->type);
1147                         continue;
1148                 }
1149
1150                 if (!subdev) {
1151                         dev_warn(isp->dev, "Subdev %s detection fail\n",
1152                                  board_info->type);
1153                         continue;
1154                 }
1155
1156                 dev_info(isp->dev, "Subdev %s successfully register\n",
1157                          board_info->type);
1158
1159                 switch (subdevs->type) {
1160                 case RAW_CAMERA:
1161                         dev_dbg(isp->dev, "raw_index: %d\n", raw_index);
1162                         raw_index = isp->input_cnt;
1163                         /* fall through */
1164                 case SOC_CAMERA:
1165                         dev_dbg(isp->dev, "SOC_INDEX: %d\n", isp->input_cnt);
1166                         if (isp->input_cnt >= ATOM_ISP_MAX_INPUTS) {
1167                                 dev_warn(isp->dev,
1168                                          "too many atomisp inputs, ignored\n");
1169                                 break;
1170                         }
1171
1172                         isp->inputs[isp->input_cnt].type = subdevs->type;
1173                         isp->inputs[isp->input_cnt].port = subdevs->port;
1174                         isp->inputs[isp->input_cnt].camera = subdev;
1175                         isp->inputs[isp->input_cnt].sensor_index = 0;
1176                         /*
1177                          * initialize the subdev frame size, then next we can
1178                          * judge whether frame_size store effective value via
1179                          * pixel_format.
1180                          */
1181                         isp->inputs[isp->input_cnt].frame_size.pixel_format = 0;
1182                         isp->inputs[isp->input_cnt].camera_caps =
1183                             atomisp_get_default_camera_caps();
1184                         sensor_num = isp->inputs[isp->input_cnt]
1185                                      .camera_caps->sensor_num;
1186                         isp->input_cnt++;
1187                         for (i = 1; i < sensor_num; i++) {
1188                                 if (isp->input_cnt >= ATOM_ISP_MAX_INPUTS) {
1189                                         dev_warn(isp->dev,
1190                                                  "atomisp inputs out of range\n");
1191                                         break;
1192                                 }
1193                                 isp->inputs[isp->input_cnt] =
1194                                     isp->inputs[isp->input_cnt - 1];
1195                                 isp->inputs[isp->input_cnt].sensor_index = i;
1196                                 isp->input_cnt++;
1197                         }
1198                         break;
1199                 case CAMERA_MOTOR:
1200                         if (isp->motor) {
1201                                 dev_warn(isp->dev,
1202                                          "too many atomisp motors, ignored %s\n",
1203                                          board_info->type);
1204                                 continue;
1205                         }
1206                         isp->motor = subdev;
1207                         break;
1208                 case LED_FLASH:
1209                 case XENON_FLASH:
1210                         if (isp->flash) {
1211                                 dev_warn(isp->dev,
1212                                          "too many atomisp flash devices, ignored %s\n",
1213                                          board_info->type);
1214                                 continue;
1215                         }
1216                         isp->flash = subdev;
1217                         break;
1218                 default:
1219                         dev_dbg(isp->dev, "unknown subdev probed\n");
1220                         break;
1221                 }
1222         }
1223
1224         /*
1225          * HACK: Currently VCM belongs to primary sensor only, but correct
1226          * approach must be to acquire from platform code which sensor
1227          * owns it.
1228          */
1229         if (isp->motor && raw_index >= 0)
1230                 isp->inputs[raw_index].motor = isp->motor;
1231
1232         /* Proceed even if no modules detected. For COS mode and no modules. */
1233         if (!isp->input_cnt)
1234                 dev_warn(isp->dev, "no camera attached or fail to detect\n");
1235         else
1236                 dev_info(isp->dev, "detected %d camera sensors\n",
1237                          isp->input_cnt);
1238
1239         return atomisp_csi_lane_config(isp);
1240 }
1241
1242 static void atomisp_unregister_entities(struct atomisp_device *isp)
1243 {
1244         unsigned int i;
1245         struct v4l2_subdev *sd, *next;
1246
1247         for (i = 0; i < isp->num_of_streams; i++)
1248                 atomisp_subdev_unregister_entities(&isp->asd[i]);
1249         atomisp_tpg_unregister_entities(&isp->tpg);
1250         atomisp_file_input_unregister_entities(&isp->file_dev);
1251         for (i = 0; i < ATOMISP_CAMERA_NR_PORTS; i++)
1252                 atomisp_mipi_csi2_unregister_entities(&isp->csi2_port[i]);
1253
1254         list_for_each_entry_safe(sd, next, &isp->v4l2_dev.subdevs, list)
1255         v4l2_device_unregister_subdev(sd);
1256
1257         v4l2_device_unregister(&isp->v4l2_dev);
1258         media_device_unregister(&isp->media_dev);
1259 }
1260
1261 static int atomisp_register_entities(struct atomisp_device *isp)
1262 {
1263         int ret = 0;
1264         unsigned int i;
1265
1266         isp->media_dev.dev = isp->dev;
1267
1268         strlcpy(isp->media_dev.model, "Intel Atom ISP",
1269                 sizeof(isp->media_dev.model));
1270
1271         media_device_init(&isp->media_dev);
1272         isp->v4l2_dev.mdev = &isp->media_dev;
1273         ret = v4l2_device_register(isp->dev, &isp->v4l2_dev);
1274         if (ret < 0) {
1275                 dev_err(isp->dev, "%s: V4L2 device registration failed (%d)\n",
1276                         __func__, ret);
1277                 goto v4l2_device_failed;
1278         }
1279
1280         ret = atomisp_subdev_probe(isp);
1281         if (ret < 0)
1282                 goto csi_and_subdev_probe_failed;
1283
1284         /* Register internal entities */
1285         for (i = 0; i < ATOMISP_CAMERA_NR_PORTS; i++) {
1286                 ret = atomisp_mipi_csi2_register_entities(&isp->csi2_port[i],
1287                         &isp->v4l2_dev);
1288                 if (ret == 0)
1289                         continue;
1290
1291                 /* error case */
1292                 dev_err(isp->dev, "failed to register the CSI port: %d\n", i);
1293                 /* deregister all registered CSI ports */
1294                 while (i--)
1295                         atomisp_mipi_csi2_unregister_entities(
1296                             &isp->csi2_port[i]);
1297
1298                 goto csi_and_subdev_probe_failed;
1299         }
1300
1301         ret =
1302             atomisp_file_input_register_entities(&isp->file_dev, &isp->v4l2_dev);
1303         if (ret < 0) {
1304                 dev_err(isp->dev, "atomisp_file_input_register_entities\n");
1305                 goto file_input_register_failed;
1306         }
1307
1308         ret = atomisp_tpg_register_entities(&isp->tpg, &isp->v4l2_dev);
1309         if (ret < 0) {
1310                 dev_err(isp->dev, "atomisp_tpg_register_entities\n");
1311                 goto tpg_register_failed;
1312         }
1313
1314         for (i = 0; i < isp->num_of_streams; i++) {
1315                 struct atomisp_sub_device *asd = &isp->asd[i];
1316
1317                 ret = atomisp_subdev_register_entities(asd, &isp->v4l2_dev);
1318                 if (ret < 0) {
1319                         dev_err(isp->dev,
1320                                 "atomisp_subdev_register_entities fail\n");
1321                         for (; i > 0; i--)
1322                                 atomisp_subdev_unregister_entities(
1323                                     &isp->asd[i - 1]);
1324                         goto subdev_register_failed;
1325                 }
1326         }
1327
1328         for (i = 0; i < isp->num_of_streams; i++) {
1329                 struct atomisp_sub_device *asd = &isp->asd[i];
1330
1331                 init_completion(&asd->init_done);
1332
1333                 asd->delayed_init_workq =
1334                     alloc_workqueue(isp->v4l2_dev.name, WQ_CPU_INTENSIVE,
1335                                     1);
1336                 if (!asd->delayed_init_workq) {
1337                         dev_err(isp->dev,
1338                                 "Failed to initialize delayed init workq\n");
1339                         ret = -ENOMEM;
1340
1341                         for (; i > 0; i--)
1342                                 destroy_workqueue(isp->asd[i - 1].
1343                                                   delayed_init_workq);
1344                         goto wq_alloc_failed;
1345                 }
1346                 INIT_WORK(&asd->delayed_init_work, atomisp_delayed_init_work);
1347         }
1348
1349         for (i = 0; i < isp->input_cnt; i++) {
1350                 if (isp->inputs[i].port >= ATOMISP_CAMERA_NR_PORTS) {
1351                         dev_err(isp->dev, "isp->inputs port %d not supported\n",
1352                                 isp->inputs[i].port);
1353                         ret = -EINVAL;
1354                         goto link_failed;
1355                 }
1356         }
1357
1358         dev_dbg(isp->dev,
1359                 "FILE_INPUT enable, camera_cnt: %d\n", isp->input_cnt);
1360         isp->inputs[isp->input_cnt].type = FILE_INPUT;
1361         isp->inputs[isp->input_cnt].port = -1;
1362         isp->inputs[isp->input_cnt].camera_caps =
1363             atomisp_get_default_camera_caps();
1364         isp->inputs[isp->input_cnt++].camera = &isp->file_dev.sd;
1365
1366         if (isp->input_cnt < ATOM_ISP_MAX_INPUTS) {
1367                 dev_dbg(isp->dev,
1368                         "TPG detected, camera_cnt: %d\n", isp->input_cnt);
1369                 isp->inputs[isp->input_cnt].type = TEST_PATTERN;
1370                 isp->inputs[isp->input_cnt].port = -1;
1371                 isp->inputs[isp->input_cnt].camera_caps =
1372                     atomisp_get_default_camera_caps();
1373                 isp->inputs[isp->input_cnt++].camera = &isp->tpg.sd;
1374         } else {
1375                 dev_warn(isp->dev, "too many atomisp inputs, TPG ignored.\n");
1376         }
1377
1378         ret = v4l2_device_register_subdev_nodes(&isp->v4l2_dev);
1379         if (ret < 0)
1380                 goto link_failed;
1381
1382         return media_device_register(&isp->media_dev);
1383
1384 link_failed:
1385         for (i = 0; i < isp->num_of_streams; i++)
1386                 destroy_workqueue(isp->asd[i].
1387                                   delayed_init_workq);
1388 wq_alloc_failed:
1389         for (i = 0; i < isp->num_of_streams; i++)
1390                 atomisp_subdev_unregister_entities(
1391                     &isp->asd[i]);
1392 subdev_register_failed:
1393         atomisp_tpg_unregister_entities(&isp->tpg);
1394 tpg_register_failed:
1395         atomisp_file_input_unregister_entities(&isp->file_dev);
1396 file_input_register_failed:
1397         for (i = 0; i < ATOMISP_CAMERA_NR_PORTS; i++)
1398                 atomisp_mipi_csi2_unregister_entities(&isp->csi2_port[i]);
1399 csi_and_subdev_probe_failed:
1400         v4l2_device_unregister(&isp->v4l2_dev);
1401 v4l2_device_failed:
1402         media_device_unregister(&isp->media_dev);
1403         media_device_cleanup(&isp->media_dev);
1404         return ret;
1405 }
1406
1407 static int atomisp_initialize_modules(struct atomisp_device *isp)
1408 {
1409         int ret;
1410
1411         ret = atomisp_mipi_csi2_init(isp);
1412         if (ret < 0) {
1413                 dev_err(isp->dev, "mipi csi2 initialization failed\n");
1414                 goto error_mipi_csi2;
1415         }
1416
1417         ret = atomisp_file_input_init(isp);
1418         if (ret < 0) {
1419                 dev_err(isp->dev,
1420                         "file input device initialization failed\n");
1421                 goto error_file_input;
1422         }
1423
1424         ret = atomisp_tpg_init(isp);
1425         if (ret < 0) {
1426                 dev_err(isp->dev, "tpg initialization failed\n");
1427                 goto error_tpg;
1428         }
1429
1430         ret = atomisp_subdev_init(isp);
1431         if (ret < 0) {
1432                 dev_err(isp->dev, "ISP subdev initialization failed\n");
1433                 goto error_isp_subdev;
1434         }
1435
1436         return 0;
1437
1438 error_isp_subdev:
1439 error_tpg:
1440         atomisp_tpg_cleanup(isp);
1441 error_file_input:
1442         atomisp_file_input_cleanup(isp);
1443 error_mipi_csi2:
1444         atomisp_mipi_csi2_cleanup(isp);
1445         return ret;
1446 }
1447
1448 static void atomisp_uninitialize_modules(struct atomisp_device *isp)
1449 {
1450         atomisp_tpg_cleanup(isp);
1451         atomisp_file_input_cleanup(isp);
1452         atomisp_mipi_csi2_cleanup(isp);
1453 }
1454
1455 const struct firmware *
1456 atomisp_load_firmware(struct atomisp_device *isp)
1457 {
1458         const struct firmware *fw;
1459         int rc;
1460         char *fw_path = NULL;
1461
1462         if (skip_fwload)
1463                 return NULL;
1464
1465         if (firmware_name[0] != '\0') {
1466                 fw_path = firmware_name;
1467         } else {
1468                 if ((isp->media_dev.hw_revision  >> ATOMISP_HW_REVISION_SHIFT)
1469                     == ATOMISP_HW_REVISION_ISP2401)
1470                         fw_path = "shisp_2401a0_v21.bin";
1471
1472                 if (isp->media_dev.hw_revision ==
1473                     ((ATOMISP_HW_REVISION_ISP2401_LEGACY << ATOMISP_HW_REVISION_SHIFT)
1474                     | ATOMISP_HW_STEPPING_A0))
1475                         fw_path = "shisp_2401a0_legacy_v21.bin";
1476
1477                 if (isp->media_dev.hw_revision ==
1478                     ((ATOMISP_HW_REVISION_ISP2400 << ATOMISP_HW_REVISION_SHIFT)
1479                     | ATOMISP_HW_STEPPING_B0))
1480                         fw_path = "shisp_2400b0_v21.bin";
1481         }
1482
1483         if (!fw_path) {
1484                 dev_err(isp->dev, "Unsupported hw_revision 0x%x\n",
1485                         isp->media_dev.hw_revision);
1486                 return NULL;
1487         }
1488
1489         rc = request_firmware(&fw, fw_path, isp->dev);
1490         if (rc) {
1491                 dev_err(isp->dev,
1492                         "atomisp: Error %d while requesting firmware %s\n",
1493                         rc, fw_path);
1494                 return NULL;
1495         }
1496
1497         return fw;
1498 }
1499
1500 /*
1501  * Check for flags the driver was compiled with against the PCI
1502  * device. Always returns true on other than ISP 2400.
1503  */
1504 static bool is_valid_device(struct pci_dev *dev,
1505                             const struct pci_device_id *id)
1506 {
1507         unsigned int a0_max_id = 0;
1508         const char *name;
1509         const char *product;
1510
1511         product = dmi_get_system_info(DMI_PRODUCT_NAME);
1512
1513         switch (id->device & ATOMISP_PCI_DEVICE_SOC_MASK) {
1514         case ATOMISP_PCI_DEVICE_SOC_MRFLD:
1515                 a0_max_id = ATOMISP_PCI_REV_MRFLD_A0_MAX;
1516                 atomisp_hw_is_isp2401 = false;
1517                 name = "Merrifield";
1518                 break;
1519         case ATOMISP_PCI_DEVICE_SOC_BYT:
1520                 a0_max_id = ATOMISP_PCI_REV_BYT_A0_MAX;
1521                 atomisp_hw_is_isp2401 = false;
1522                 name = "Baytrail";
1523                 break;
1524         case ATOMISP_PCI_DEVICE_SOC_ANN:
1525                 name = "Anniedale";
1526                 atomisp_hw_is_isp2401 = true;
1527                 break;
1528         case ATOMISP_PCI_DEVICE_SOC_CHT:
1529                 name = "Cherrytrail";
1530                 atomisp_hw_is_isp2401 = true;
1531                 break;
1532         default:
1533                 dev_err(&dev->dev, "%s: unknown device ID %x04:%x04\n",
1534                         product, id->vendor, id->device);
1535                 return false;
1536         }
1537
1538         if (dev->revision <= ATOMISP_PCI_REV_BYT_A0_MAX) {
1539                 dev_err(&dev->dev, "%s revision %d is not unsupported\n",
1540                         name, dev->revision);
1541                 return false;
1542         }
1543
1544         /*
1545          * FIXME:
1546          * remove the if once the driver become generic
1547          */
1548
1549 #if defined(ISP2400)
1550         if (atomisp_hw_is_isp2401) {
1551                 dev_err(&dev->dev, "Support for %s (ISP2401) was disabled at compile time\n",
1552                         name);
1553                 return false;
1554         }
1555 #else
1556         if (!atomisp_hw_is_isp2401) {
1557                 dev_err(&dev->dev, "Support for %s (ISP2400) was disabled at compile time\n",
1558                         name);
1559                 return false;
1560         }
1561 #endif
1562
1563         dev_info(&dev->dev, "Detected %s version %d (ISP240%c) on %s\n",
1564                 name, dev->revision,
1565                 atomisp_hw_is_isp2401 ? '1' : '0',
1566                 product);
1567
1568         return true;
1569 }
1570
1571 static int init_atomisp_wdts(struct atomisp_device *isp)
1572 {
1573         int i, err;
1574
1575         atomic_set(&isp->wdt_work_queued, 0);
1576         isp->wdt_work_queue = alloc_workqueue(isp->v4l2_dev.name, 0, 1);
1577         if (!isp->wdt_work_queue) {
1578                 dev_err(isp->dev, "Failed to initialize wdt work queue\n");
1579                 err = -ENOMEM;
1580                 goto alloc_fail;
1581         }
1582         INIT_WORK(&isp->wdt_work, atomisp_wdt_work);
1583
1584         for (i = 0; i < isp->num_of_streams; i++) {
1585                 struct atomisp_sub_device *asd = &isp->asd[i];
1586
1587                 if (!atomisp_hw_is_isp2401)
1588                         timer_setup(&asd->wdt, atomisp_wdt, 0);
1589                 else {
1590                         timer_setup(&asd->video_out_capture.wdt,
1591                                     atomisp_wdt, 0);
1592                         timer_setup(&asd->video_out_preview.wdt,
1593                                     atomisp_wdt, 0);
1594                         timer_setup(&asd->video_out_vf.wdt, atomisp_wdt, 0);
1595                         timer_setup(&asd->video_out_video_capture.wdt,
1596                                     atomisp_wdt, 0);
1597                 }
1598         }
1599         return 0;
1600 alloc_fail:
1601         return err;
1602 }
1603
1604 #define ATOM_ISP_PCI_BAR        0
1605
1606 static int atomisp_pci_probe(struct pci_dev *dev,
1607                              const struct pci_device_id *id)
1608 {
1609         const struct atomisp_platform_data *pdata;
1610         struct atomisp_device *isp;
1611         unsigned int start;
1612         void __iomem *base;
1613         int err, val;
1614         u32 irq;
1615
1616         if (!is_valid_device(dev, id))
1617                 return -ENODEV;
1618
1619         /* Pointer to struct device. */
1620         atomisp_dev = &dev->dev;
1621
1622         pdata = atomisp_get_platform_data();
1623         if (!pdata)
1624                 dev_warn(&dev->dev, "no platform data available\n");
1625
1626         err = pcim_enable_device(dev);
1627         if (err) {
1628                 dev_err(&dev->dev, "Failed to enable CI ISP device (%d)\n",
1629                         err);
1630                 return err;
1631         }
1632
1633         start = pci_resource_start(dev, ATOM_ISP_PCI_BAR);
1634         dev_dbg(&dev->dev, "start: 0x%x\n", start);
1635
1636         err = pcim_iomap_regions(dev, 1 << ATOM_ISP_PCI_BAR, pci_name(dev));
1637         if (err) {
1638                 dev_err(&dev->dev, "Failed to I/O memory remapping (%d)\n",
1639                         err);
1640                 goto ioremap_fail;
1641         }
1642
1643         base = pcim_iomap_table(dev)[ATOM_ISP_PCI_BAR];
1644         dev_dbg(&dev->dev, "base: %p\n", base);
1645
1646         atomisp_io_base = base;
1647
1648         dev_dbg(&dev->dev, "atomisp_io_base: %p\n", atomisp_io_base);
1649
1650         isp = devm_kzalloc(&dev->dev, sizeof(struct atomisp_device), GFP_KERNEL);
1651         if (!isp) {
1652                 err = -ENOMEM;
1653                 goto atomisp_dev_alloc_fail;
1654         }
1655         isp->pdev = dev;
1656         isp->dev = &dev->dev;
1657         isp->sw_contex.power_state = ATOM_ISP_POWER_UP;
1658         isp->saved_regs.ispmmadr = start;
1659
1660         rt_mutex_init(&isp->mutex);
1661         mutex_init(&isp->streamoff_mutex);
1662         spin_lock_init(&isp->lock);
1663
1664         /* This is not a true PCI device on SoC, so the delay is not needed. */
1665         isp->pdev->d3_delay = 0;
1666
1667         switch (id->device & ATOMISP_PCI_DEVICE_SOC_MASK) {
1668         case ATOMISP_PCI_DEVICE_SOC_MRFLD:
1669                 isp->media_dev.hw_revision =
1670                     (ATOMISP_HW_REVISION_ISP2400
1671                      << ATOMISP_HW_REVISION_SHIFT) |
1672                     ATOMISP_HW_STEPPING_B0;
1673
1674                 switch (id->device) {
1675                 case ATOMISP_PCI_DEVICE_SOC_MRFLD_1179:
1676                         isp->dfs = &dfs_config_merr_1179;
1677                         break;
1678                 case ATOMISP_PCI_DEVICE_SOC_MRFLD_117A:
1679                         isp->dfs = &dfs_config_merr_117a;
1680
1681                         break;
1682                 default:
1683                         isp->dfs = &dfs_config_merr;
1684                         break;
1685                 }
1686                 isp->hpll_freq = HPLL_FREQ_1600MHZ;
1687                 break;
1688         case ATOMISP_PCI_DEVICE_SOC_BYT:
1689                 isp->media_dev.hw_revision =
1690                     (ATOMISP_HW_REVISION_ISP2400
1691                      << ATOMISP_HW_REVISION_SHIFT) |
1692                     ATOMISP_HW_STEPPING_B0;
1693 #ifdef FIXME
1694                 if (INTEL_MID_BOARD(3, TABLET, BYT, BLK, PRO, CRV2) ||
1695                     INTEL_MID_BOARD(3, TABLET, BYT, BLK, ENG, CRV2)) {
1696                         isp->dfs = &dfs_config_byt_cr;
1697                         isp->hpll_freq = HPLL_FREQ_2000MHZ;
1698                 } else
1699 #endif
1700                 {
1701                         isp->dfs = &dfs_config_byt;
1702                         isp->hpll_freq = HPLL_FREQ_1600MHZ;
1703                 }
1704                 /* HPLL frequency is known to be device-specific, but we don't
1705                  * have specs yet for exactly how it varies.  Default to
1706                  * BYT-CR but let provisioning set it via EFI variable */
1707                 isp->hpll_freq = gmin_get_var_int(&dev->dev, false, "HpllFreq",
1708                                                   HPLL_FREQ_2000MHZ);
1709
1710                 /*
1711                  * for BYT/CHT we are put isp into D3cold to avoid pci registers access
1712                  * in power off. Set d3cold_delay to 0 since default 100ms is not
1713                  * necessary.
1714                  */
1715                 isp->pdev->d3cold_delay = 0;
1716                 break;
1717         case ATOMISP_PCI_DEVICE_SOC_ANN:
1718                 isp->media_dev.hw_revision = (
1719 #ifdef ISP2401_NEW_INPUT_SYSTEM
1720                                                  ATOMISP_HW_REVISION_ISP2401
1721 #else
1722                                                  ATOMISP_HW_REVISION_ISP2401_LEGACY
1723 #endif
1724                                                  << ATOMISP_HW_REVISION_SHIFT);
1725                 isp->media_dev.hw_revision |= isp->pdev->revision < 2 ?
1726                                               ATOMISP_HW_STEPPING_A0 : ATOMISP_HW_STEPPING_B0;
1727                 isp->dfs = &dfs_config_merr;
1728                 isp->hpll_freq = HPLL_FREQ_1600MHZ;
1729                 break;
1730         case ATOMISP_PCI_DEVICE_SOC_CHT:
1731                 isp->media_dev.hw_revision = (
1732 #ifdef ISP2401_NEW_INPUT_SYSTEM
1733                                                  ATOMISP_HW_REVISION_ISP2401
1734 #else
1735                                                  ATOMISP_HW_REVISION_ISP2401_LEGACY
1736 #endif
1737                                                  << ATOMISP_HW_REVISION_SHIFT);
1738                 isp->media_dev.hw_revision |= isp->pdev->revision < 2 ?
1739                                               ATOMISP_HW_STEPPING_A0 : ATOMISP_HW_STEPPING_B0;
1740
1741                 isp->dfs = &dfs_config_cht;
1742                 isp->pdev->d3cold_delay = 0;
1743
1744                 iosf_mbi_read(CCK_PORT, MBI_REG_READ, CCK_FUSE_REG_0, &val);
1745                 switch (val & CCK_FUSE_HPLL_FREQ_MASK) {
1746                 case 0x00:
1747                         isp->hpll_freq = HPLL_FREQ_800MHZ;
1748                         break;
1749                 case 0x01:
1750                         isp->hpll_freq = HPLL_FREQ_1600MHZ;
1751                         break;
1752                 case 0x02:
1753                         isp->hpll_freq = HPLL_FREQ_2000MHZ;
1754                         break;
1755                 default:
1756                         isp->hpll_freq = HPLL_FREQ_1600MHZ;
1757                         dev_warn(isp->dev,
1758                                  "read HPLL from cck failed.default 1600MHz.\n");
1759                 }
1760                 break;
1761         default:
1762                 dev_err(&dev->dev, "un-supported IUNIT device\n");
1763                 err = -ENODEV;
1764                 goto atomisp_dev_alloc_fail;
1765         }
1766
1767         dev_info(&dev->dev, "ISP HPLL frequency base = %d MHz\n",
1768                  isp->hpll_freq);
1769
1770         isp->max_isr_latency = ATOMISP_MAX_ISR_LATENCY;
1771
1772         /* Load isp firmware from user space */
1773         if (!defer_fw_load) {
1774                 isp->firmware = atomisp_load_firmware(isp);
1775                 if (!isp->firmware) {
1776                         err = -ENOENT;
1777                         dev_dbg(&dev->dev, "Firmware load failed\n");
1778                         goto load_fw_fail;
1779                 }
1780
1781                 err = sh_css_check_firmware_version(isp->dev,
1782                                                     isp->firmware->data);
1783                 if (err) {
1784                         dev_dbg(&dev->dev, "Firmware version check failed\n");
1785                         goto fw_validation_fail;
1786                 }
1787         } else {
1788                 dev_info(&dev->dev, "Firmware load will be deferred\n");
1789         }
1790
1791         pci_set_master(dev);
1792         pci_set_drvdata(dev, isp);
1793
1794         err = pci_enable_msi(dev);
1795         if (err) {
1796                 dev_err(&dev->dev, "Failed to enable msi (%d)\n", err);
1797                 goto enable_msi_fail;
1798         }
1799
1800         atomisp_msi_irq_init(isp, dev);
1801
1802         cpu_latency_qos_add_request(&isp->pm_qos, PM_QOS_DEFAULT_VALUE);
1803
1804         /*
1805          * for MRFLD, Software/firmware needs to write a 1 to bit 0 of
1806          * the register at CSI_RECEIVER_SELECTION_REG to enable SH CSI
1807          * backend write 0 will enable Arasan CSI backend, which has
1808          * bugs(like sighting:4567697 and 4567699) and will be removed
1809          * in B0
1810          */
1811         atomisp_css2_hw_store_32(MRFLD_CSI_RECEIVER_SELECTION_REG, 1);
1812
1813         if ((id->device & ATOMISP_PCI_DEVICE_SOC_MASK) ==
1814             ATOMISP_PCI_DEVICE_SOC_MRFLD) {
1815                 u32 csi_afe_trim;
1816
1817                 /*
1818                  * Workaround for imbalance data eye issue which is observed
1819                  * on TNG B0.
1820                  */
1821                 pci_read_config_dword(dev, MRFLD_PCI_CSI_AFE_TRIM_CONTROL,
1822                                       &csi_afe_trim);
1823                 csi_afe_trim &= ~((MRFLD_PCI_CSI_HSRXCLKTRIM_MASK <<
1824                                    MRFLD_PCI_CSI1_HSRXCLKTRIM_SHIFT) |
1825                                   (MRFLD_PCI_CSI_HSRXCLKTRIM_MASK <<
1826                                    MRFLD_PCI_CSI2_HSRXCLKTRIM_SHIFT) |
1827                                   (MRFLD_PCI_CSI_HSRXCLKTRIM_MASK <<
1828                                    MRFLD_PCI_CSI3_HSRXCLKTRIM_SHIFT));
1829                 csi_afe_trim |= (MRFLD_PCI_CSI1_HSRXCLKTRIM <<
1830                                  MRFLD_PCI_CSI1_HSRXCLKTRIM_SHIFT) |
1831                                 (MRFLD_PCI_CSI2_HSRXCLKTRIM <<
1832                                  MRFLD_PCI_CSI2_HSRXCLKTRIM_SHIFT) |
1833                                 (MRFLD_PCI_CSI3_HSRXCLKTRIM <<
1834                                  MRFLD_PCI_CSI3_HSRXCLKTRIM_SHIFT);
1835                 pci_write_config_dword(dev, MRFLD_PCI_CSI_AFE_TRIM_CONTROL,
1836                                        csi_afe_trim);
1837         }
1838
1839         err = atomisp_initialize_modules(isp);
1840         if (err < 0) {
1841                 dev_err(&dev->dev, "atomisp_initialize_modules (%d)\n", err);
1842                 goto initialize_modules_fail;
1843         }
1844
1845         err = atomisp_register_entities(isp);
1846         if (err < 0) {
1847                 dev_err(&dev->dev, "atomisp_register_entities failed (%d)\n",
1848                         err);
1849                 goto register_entities_fail;
1850         }
1851         err = atomisp_create_pads_links(isp);
1852         if (err < 0)
1853                 goto register_entities_fail;
1854         /* init atomisp wdts */
1855         if (init_atomisp_wdts(isp) != 0)
1856                 goto wdt_work_queue_fail;
1857
1858         /* save the iunit context only once after all the values are init'ed. */
1859         atomisp_save_iunit_reg(isp);
1860
1861         pm_runtime_put_noidle(&dev->dev);
1862         pm_runtime_allow(&dev->dev);
1863
1864         hmm_init_mem_stat(repool_pgnr, dypool_enable, dypool_pgnr);
1865         err = hmm_pool_register(repool_pgnr, HMM_POOL_TYPE_RESERVED);
1866         if (err) {
1867                 dev_err(&dev->dev, "Failed to register reserved memory pool.\n");
1868                 goto hmm_pool_fail;
1869         }
1870
1871         /* Init ISP memory management */
1872         hmm_init();
1873
1874         err = devm_request_threaded_irq(&dev->dev, dev->irq,
1875                                         atomisp_isr, atomisp_isr_thread,
1876                                         IRQF_SHARED, "isp_irq", isp);
1877         if (err) {
1878                 dev_err(&dev->dev, "Failed to request irq (%d)\n", err);
1879                 goto request_irq_fail;
1880         }
1881
1882         /* Load firmware into ISP memory */
1883         if (!defer_fw_load) {
1884                 err = atomisp_css_load_firmware(isp);
1885                 if (err) {
1886                         dev_err(&dev->dev, "Failed to init css.\n");
1887                         goto css_init_fail;
1888                 }
1889         } else {
1890                 dev_dbg(&dev->dev, "Skip css init.\n");
1891         }
1892         /* Clear FW image from memory */
1893         release_firmware(isp->firmware);
1894         isp->firmware = NULL;
1895         isp->css_env.isp_css_fw.data = NULL;
1896
1897         atomisp_drvfs_init(&dev->driver->driver, isp);
1898
1899         return 0;
1900
1901 css_init_fail:
1902         devm_free_irq(&dev->dev, dev->irq, isp);
1903 request_irq_fail:
1904         hmm_cleanup();
1905         hmm_pool_unregister(HMM_POOL_TYPE_RESERVED);
1906 hmm_pool_fail:
1907         destroy_workqueue(isp->wdt_work_queue);
1908 wdt_work_queue_fail:
1909         atomisp_acc_cleanup(isp);
1910         atomisp_unregister_entities(isp);
1911 register_entities_fail:
1912         atomisp_uninitialize_modules(isp);
1913 initialize_modules_fail:
1914         cpu_latency_qos_remove_request(&isp->pm_qos);
1915         atomisp_msi_irq_uninit(isp, dev);
1916         pci_disable_msi(dev);
1917 enable_msi_fail:
1918 fw_validation_fail:
1919         release_firmware(isp->firmware);
1920 load_fw_fail:
1921         /*
1922          * Switch off ISP, as keeping it powered on would prevent
1923          * reaching S0ix states.
1924          *
1925          * The following lines have been copied from atomisp suspend path
1926          */
1927
1928         pci_read_config_dword(dev, PCI_INTERRUPT_CTRL, &irq);
1929         irq = irq & 1 << INTR_IIR;
1930         pci_write_config_dword(dev, PCI_INTERRUPT_CTRL, irq);
1931
1932         pci_read_config_dword(dev, PCI_INTERRUPT_CTRL, &irq);
1933         irq &= ~(1 << INTR_IER);
1934         pci_write_config_dword(dev, PCI_INTERRUPT_CTRL, irq);
1935
1936         atomisp_msi_irq_uninit(isp, dev);
1937
1938         atomisp_ospm_dphy_down(isp);
1939
1940         /* Address later when we worry about the ...field chips */
1941         if (IS_ENABLED(CONFIG_PM) && atomisp_mrfld_power_down(isp))
1942                 dev_err(&dev->dev, "Failed to switch off ISP\n");
1943
1944 atomisp_dev_alloc_fail:
1945         pcim_iounmap_regions(dev, 1 << ATOM_ISP_PCI_BAR);
1946
1947 ioremap_fail:
1948         return err;
1949 }
1950
1951 static void atomisp_pci_remove(struct pci_dev *dev)
1952 {
1953         struct atomisp_device *isp = (struct atomisp_device *)
1954                                      pci_get_drvdata(dev);
1955
1956         dev_info(&dev->dev, "Removing atomisp driver\n");
1957
1958         atomisp_drvfs_exit();
1959
1960         atomisp_acc_cleanup(isp);
1961
1962         ia_css_unload_firmware();
1963         hmm_cleanup();
1964
1965         pm_runtime_forbid(&dev->dev);
1966         pm_runtime_get_noresume(&dev->dev);
1967         cpu_latency_qos_remove_request(&isp->pm_qos);
1968
1969         atomisp_msi_irq_uninit(isp, dev);
1970         atomisp_unregister_entities(isp);
1971
1972         destroy_workqueue(isp->wdt_work_queue);
1973         atomisp_file_input_cleanup(isp);
1974
1975         release_firmware(isp->firmware);
1976
1977         hmm_pool_unregister(HMM_POOL_TYPE_RESERVED);
1978 }
1979
1980 static const struct pci_device_id atomisp_pci_tbl[] = {
1981         /* Merrifield */
1982         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, ATOMISP_PCI_DEVICE_SOC_MRFLD)},
1983         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, ATOMISP_PCI_DEVICE_SOC_MRFLD_1179)},
1984         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, ATOMISP_PCI_DEVICE_SOC_MRFLD_117A)},
1985         /* Baytrail */
1986         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, ATOMISP_PCI_DEVICE_SOC_BYT)},
1987         /* Anniedale (Merrifield+ / Moorefield) */
1988         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, ATOMISP_PCI_DEVICE_SOC_ANN)},
1989         /* Cherrytrail */
1990         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, ATOMISP_PCI_DEVICE_SOC_CHT)},
1991         {0,}
1992 };
1993
1994 MODULE_DEVICE_TABLE(pci, atomisp_pci_tbl);
1995
1996 static const struct dev_pm_ops atomisp_pm_ops = {
1997         .runtime_suspend = atomisp_runtime_suspend,
1998         .runtime_resume = atomisp_runtime_resume,
1999         .suspend = atomisp_suspend,
2000         .resume = atomisp_resume,
2001 };
2002
2003 static struct pci_driver atomisp_pci_driver = {
2004         .driver = {
2005                 .pm = &atomisp_pm_ops,
2006         },
2007         .name = "atomisp-isp2",
2008         .id_table = atomisp_pci_tbl,
2009         .probe = atomisp_pci_probe,
2010         .remove = atomisp_pci_remove,
2011 };
2012
2013 module_pci_driver(atomisp_pci_driver);
2014
2015 MODULE_AUTHOR("Wen Wang <wen.w.wang@intel.com>");
2016 MODULE_AUTHOR("Xiaolin Zhang <xiaolin.zhang@intel.com>");
2017 MODULE_LICENSE("GPL");
2018 MODULE_DESCRIPTION("Intel ATOM Platform ISP Driver");