thp7312.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2021 THine Electronics, Inc.
  4. * Copyright (C) 2023 Ideas on Board Oy
  5. */
  6. #include <linux/unaligned.h>
  7. #include <linux/clk.h>
  8. #include <linux/delay.h>
  9. #include <linux/device.h>
  10. #include <linux/firmware.h>
  11. #include <linux/gpio/consumer.h>
  12. #include <linux/i2c.h>
  13. #include <linux/init.h>
  14. #include <linux/iopoll.h>
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/mtd/spi-nor.h>
  18. #include <linux/pm_runtime.h>
  19. #include <linux/property.h>
  20. #include <linux/regulator/consumer.h>
  21. #include <linux/slab.h>
  22. #include <linux/types.h>
  23. #include <media/v4l2-async.h>
  24. #include <media/v4l2-cci.h>
  25. #include <media/v4l2-ctrls.h>
  26. #include <media/v4l2-device.h>
  27. #include <media/v4l2-event.h>
  28. #include <media/v4l2-fwnode.h>
  29. #include <media/v4l2-subdev.h>
  30. #include <uapi/linux/thp7312.h>
  31. /* ISP registers */
  32. #define THP7312_REG_FIRMWARE_VERSION_1 CCI_REG8(0xf000)
  33. #define THP7312_REG_CAMERA_STATUS CCI_REG8(0xf001)
  34. #define THP7312_REG_FIRMWARE_VERSION_2 CCI_REG8(0xf005)
  35. #define THP7312_REG_SET_OUTPUT_ENABLE CCI_REG8(0xf008)
  36. #define THP7312_OUTPUT_ENABLE 0x01
  37. #define THP7312_OUTPUT_DISABLE 0x00
  38. #define THP7312_REG_SET_OUTPUT_COLOR_COMPRESSION CCI_REG8(0xf009)
  39. #define THP7312_REG_SET_OUTPUT_COLOR_UYVY 0x00
  40. #define THP7312_REG_SET_OUTPUT_COLOR_YUY2 0x04
  41. #define THP7312_REG_FLIP_MIRROR CCI_REG8(0xf00c)
  42. #define THP7312_REG_FLIP_MIRROR_FLIP BIT(0)
  43. #define THP7312_REG_FLIP_MIRROR_MIRROR BIT(1)
  44. #define THP7312_REG_VIDEO_IMAGE_SIZE CCI_REG8(0xf00d)
  45. #define THP7312_VIDEO_IMAGE_SIZE_640x360 0x52
  46. #define THP7312_VIDEO_IMAGE_SIZE_640x460 0x03
  47. #define THP7312_VIDEO_IMAGE_SIZE_1280x720 0x0a
  48. #define THP7312_VIDEO_IMAGE_SIZE_1920x1080 0x0b
  49. #define THP7312_VIDEO_IMAGE_SIZE_3840x2160 0x0d
  50. #define THP7312_VIDEO_IMAGE_SIZE_4160x3120 0x14
  51. #define THP7312_VIDEO_IMAGE_SIZE_2016x1512 0x20
  52. #define THP7312_VIDEO_IMAGE_SIZE_2048x1536 0x21
  53. #define THP7312_REG_VIDEO_FRAME_RATE_MODE CCI_REG8(0xf00f)
  54. #define THP7312_VIDEO_FRAME_RATE_MODE1 0x80
  55. #define THP7312_VIDEO_FRAME_RATE_MODE2 0x81
  56. #define THP7312_VIDEO_FRAME_RATE_MODE3 0x82
  57. #define THP7312_REG_SET_DRIVING_MODE CCI_REG8(0xf010)
  58. #define THP7312_REG_DRIVING_MODE_STATUS CCI_REG8(0xf011)
  59. #define THP7312_REG_JPEG_COMPRESSION_FACTOR CCI_REG8(0xf01b)
  60. #define THP7312_REG_AE_EXPOSURE_COMPENSATION CCI_REG8(0xf022)
  61. #define THP7312_REG_AE_FLICKER_MODE CCI_REG8(0xf023)
  62. #define THP7312_AE_FLICKER_MODE_50 0x00
  63. #define THP7312_AE_FLICKER_MODE_60 0x01
  64. #define THP7312_AE_FLICKER_MODE_DISABLE 0x80
  65. #define THP7312_REG_AE_FIX_FRAME_RATE CCI_REG8(0xf02e)
  66. #define THP7312_REG_MANUAL_WB_RED_GAIN CCI_REG8(0xf036)
  67. #define THP7312_REG_MANUAL_WB_BLUE_GAIN CCI_REG8(0xf037)
  68. #define THP7312_REG_WB_MODE CCI_REG8(0xf039)
  69. #define THP7312_WB_MODE_AUTO 0x00
  70. #define THP7312_WB_MODE_MANUAL 0x11
  71. #define THP7312_REG_MANUAL_FOCUS_POSITION CCI_REG16(0xf03c)
  72. #define THP7312_REG_AF_CONTROL CCI_REG8(0xf040)
  73. #define THP7312_REG_AF_CONTROL_AF 0x01
  74. #define THP7312_REG_AF_CONTROL_MANUAL 0x10
  75. #define THP7312_REG_AF_CONTROL_LOCK 0x80
  76. #define THP7312_REG_AF_SETTING CCI_REG8(0xf041)
  77. #define THP7312_REG_AF_SETTING_ONESHOT_CONTRAST 0x00
  78. #define THP7312_REG_AF_SETTING_ONESHOT_PDAF 0x40
  79. #define THP7312_REG_AF_SETTING_ONESHOT_HYBRID 0x80
  80. #define THP7312_REG_AF_SETTING_CONTINUOUS_CONTRAST 0x30
  81. #define THP7312_REG_AF_SETTING_CONTINUOUS_PDAF 0x70
  82. #define THP7312_REG_AF_SETTING_CONTINUOUS_HYBRID 0xf0
  83. #define THP7312_REG_AF_SUPPORT CCI_REG8(0xf043)
  84. #define THP7312_AF_SUPPORT_PDAF BIT(1)
  85. #define THP7312_AF_SUPPORT_CONTRAST BIT(0)
  86. #define THP7312_REG_SATURATION CCI_REG8(0xf052)
  87. #define THP7312_REG_SHARPNESS CCI_REG8(0xf053)
  88. #define THP7312_REG_BRIGHTNESS CCI_REG8(0xf056)
  89. #define THP7312_REG_CONTRAST CCI_REG8(0xf057)
  90. #define THP7312_REG_NOISE_REDUCTION CCI_REG8(0xf059)
  91. #define THP7312_REG_NOISE_REDUCTION_FIXED BIT(7)
  92. #define TH7312_REG_CUSTOM_MIPI_SET CCI_REG8(0xf0f6)
  93. #define TH7312_REG_CUSTOM_MIPI_STATUS CCI_REG8(0xf0f7)
  94. #define TH7312_REG_CUSTOM_MIPI_RD CCI_REG8(0xf0f8)
  95. #define TH7312_REG_CUSTOM_MIPI_TD CCI_REG8(0xf0f9)
  96. /*
  97. * Firmware update registers. Those use a different address space than the
  98. * normal operation ISP registers.
  99. */
  100. #define THP7312_REG_FW_DRIVABILITY CCI_REG32(0xd65c)
  101. #define THP7312_REG_FW_DEST_BANK_ADDR CCI_REG32(0xff08)
  102. #define THP7312_REG_FW_VERIFY_RESULT CCI_REG8(0xff60)
  103. #define THP7312_REG_FW_RESET_FLASH CCI_REG8(0xff61)
  104. #define THP7312_REG_FW_MEMORY_IO_SETTING CCI_REG8(0xff62)
  105. #define THP7312_FW_MEMORY_IO_GPIO0 1
  106. #define THP7312_FW_MEMORY_IO_GPIO1 0
  107. #define THP7312_REG_FW_CRC_RESULT CCI_REG32(0xff64)
  108. #define THP7312_REG_FW_STATUS CCI_REG8(0xfffc)
  109. #define THP7312_FW_VERSION(major, minor) (((major) << 8) | (minor))
  110. #define THP7312_FW_VERSION_MAJOR(v) ((v) >> 8)
  111. #define THP7312_FW_VERSION_MINOR(v) ((v) & 0xff)
  112. enum thp7312_focus_method {
  113. THP7312_FOCUS_METHOD_CONTRAST,
  114. THP7312_FOCUS_METHOD_PDAF,
  115. THP7312_FOCUS_METHOD_HYBRID,
  116. };
  117. /*
  118. * enum thp7312_focus_state - State of the focus handler
  119. *
  120. * @THP7312_FOCUS_STATE_MANUAL: Manual focus, controlled through the
  121. * V4L2_CID_FOCUS_ABSOLUTE control
  122. * @THP7312_FOCUS_STATE_AUTO: Continuous auto-focus
  123. * @THP7312_FOCUS_STATE_LOCKED: Lock the focus to a fixed position. This state
  124. * is entered when switching from auto to manual mode.
  125. * @THP7312_FOCUS_STATE_ONESHOT: One-shot auto-focus
  126. *
  127. * Valid transitions are as follow:
  128. *
  129. * digraph fsm {
  130. * node [shape=circle];
  131. *
  132. * manual [label="MANUAL"];
  133. * auto [label="AUTO"];
  134. * locked [label="LOCKED"];
  135. * oneshot [label="ONESHOT"];
  136. *
  137. * manual -> auto [label="FOCUS_AUTO <- true"]
  138. * locked -> auto [label="FOCUS_AUTO <- true"]
  139. * oneshot -> auto [label="FOCUS_AUTO <- true"]
  140. * auto -> locked [label="FOCUS_AUTO <- false"]
  141. *
  142. * locked -> manual [label="FOCUS_ABSOLUTE <- *"]
  143. * oneshot -> manual [label="FOCUS_ABSOLUTE <- *"]
  144. *
  145. * manual -> oneshot [label="FOCUS_START <- *"]
  146. * locked -> oneshot [label="FOCUS_START <- *"]
  147. * }
  148. */
  149. enum thp7312_focus_state {
  150. THP7312_FOCUS_STATE_MANUAL,
  151. THP7312_FOCUS_STATE_AUTO,
  152. THP7312_FOCUS_STATE_LOCKED,
  153. THP7312_FOCUS_STATE_ONESHOT,
  154. };
  155. enum thp7312_boot_mode {
  156. THP7312_BOOT_MODE_2WIRE_SLAVE = 0,
  157. THP7312_BOOT_MODE_SPI_MASTER = 1,
  158. };
  159. struct thp7312_frame_rate {
  160. u32 fps;
  161. u32 link_freq;
  162. u8 reg_frame_rate_mode;
  163. };
  164. struct thp7312_mode_info {
  165. u32 width;
  166. u32 height;
  167. u8 reg_image_size;
  168. const struct thp7312_frame_rate *rates;
  169. };
  170. static const u32 thp7312_colour_fmts[] = {
  171. MEDIA_BUS_FMT_YUYV8_1X16,
  172. };
  173. /* regulator supplies */
  174. static const char * const thp7312_supply_name[] = {
  175. "vddcore",
  176. "vhtermrx",
  177. "vddtx",
  178. "vddhost",
  179. "vddcmos",
  180. "vddgpio-0",
  181. "vddgpio-1",
  182. };
  183. static const struct thp7312_mode_info thp7312_mode_info_data[] = {
  184. {
  185. .width = 1920,
  186. .height = 1080,
  187. .reg_image_size = THP7312_VIDEO_IMAGE_SIZE_1920x1080,
  188. .rates = (const struct thp7312_frame_rate[]) {
  189. { 30, 300000000, 0x81 },
  190. { 60, 387500000, 0x82 },
  191. { 0 }
  192. },
  193. }, {
  194. .width = 2048,
  195. .height = 1536,
  196. .reg_image_size = THP7312_VIDEO_IMAGE_SIZE_2048x1536,
  197. .rates = (const struct thp7312_frame_rate[]) {
  198. { 30, 300000000, 0x81 },
  199. { 0 }
  200. }
  201. }, {
  202. .width = 3840,
  203. .height = 2160,
  204. .reg_image_size = THP7312_VIDEO_IMAGE_SIZE_3840x2160,
  205. .rates = (const struct thp7312_frame_rate[]) {
  206. { 30, 600000000, 0x81 },
  207. { 0 }
  208. },
  209. }, {
  210. .width = 4160,
  211. .height = 3120,
  212. .reg_image_size = THP7312_VIDEO_IMAGE_SIZE_4160x3120,
  213. .rates = (const struct thp7312_frame_rate[]) {
  214. { 20, 600000000, 0x81 },
  215. { 0 }
  216. },
  217. },
  218. };
  219. struct thp7312_device;
  220. struct thp7312_sensor_info {
  221. const char *model;
  222. };
  223. struct thp7312_sensor {
  224. const struct thp7312_sensor_info *info;
  225. u8 lane_remap;
  226. };
  227. struct thp7312_device {
  228. struct device *dev;
  229. struct regmap *regmap;
  230. struct v4l2_subdev sd;
  231. struct media_pad pad;
  232. struct gpio_desc *reset_gpio;
  233. struct regulator_bulk_data supplies[ARRAY_SIZE(thp7312_supply_name)];
  234. struct clk *iclk;
  235. u8 lane_remap;
  236. struct thp7312_sensor sensors[1];
  237. enum thp7312_boot_mode boot_mode;
  238. struct v4l2_ctrl_handler ctrl_handler;
  239. bool ctrls_applied;
  240. s64 link_freq;
  241. struct {
  242. struct v4l2_ctrl *hflip;
  243. struct v4l2_ctrl *vflip;
  244. };
  245. struct {
  246. struct v4l2_ctrl *focus_auto;
  247. struct v4l2_ctrl *focus_absolute;
  248. struct v4l2_ctrl *focus_start;
  249. struct v4l2_ctrl *focus_method;
  250. };
  251. enum thp7312_focus_state focus_state;
  252. struct {
  253. struct v4l2_ctrl *noise_reduction_auto;
  254. struct v4l2_ctrl *noise_reduction_absolute;
  255. };
  256. /* Lock to protect fw_cancel */
  257. struct mutex fw_lock;
  258. struct fw_upload *fwl;
  259. u8 *fw_write_buf;
  260. bool fw_cancel;
  261. u16 fw_version;
  262. };
  263. static const struct thp7312_sensor_info thp7312_sensor_info[] = {
  264. {
  265. .model = "sony,imx258",
  266. },
  267. };
  268. static inline struct thp7312_device *to_thp7312_dev(struct v4l2_subdev *sd)
  269. {
  270. return container_of(sd, struct thp7312_device, sd);
  271. }
  272. static const struct thp7312_mode_info *
  273. thp7312_find_mode(unsigned int width, unsigned int height, bool nearest)
  274. {
  275. const struct thp7312_mode_info *mode;
  276. mode = v4l2_find_nearest_size(thp7312_mode_info_data,
  277. ARRAY_SIZE(thp7312_mode_info_data),
  278. width, height, width, height);
  279. if (!nearest && (mode->width != width || mode->height != height))
  280. return NULL;
  281. return mode;
  282. }
  283. static const struct thp7312_frame_rate *
  284. thp7312_find_rate(const struct thp7312_mode_info *mode, unsigned int fps,
  285. bool nearest)
  286. {
  287. const struct thp7312_frame_rate *best_rate = NULL;
  288. const struct thp7312_frame_rate *rate;
  289. unsigned int best_delta = UINT_MAX;
  290. if (!mode)
  291. return NULL;
  292. for (rate = mode->rates; rate->fps && best_delta; ++rate) {
  293. unsigned int delta = abs(rate->fps - fps);
  294. if (delta <= best_delta) {
  295. best_delta = delta;
  296. best_rate = rate;
  297. }
  298. }
  299. if (!nearest && best_delta)
  300. return NULL;
  301. return best_rate;
  302. }
  303. /* -----------------------------------------------------------------------------
  304. * Device Access & Configuration
  305. */
  306. #define thp7312_read_poll_timeout(dev, addr, val, cond, sleep_us, timeout_us) \
  307. ({ \
  308. int __ret, __err; \
  309. __ret = read_poll_timeout(cci_read, __err, __err || (cond), sleep_us, \
  310. timeout_us, false, (dev)->regmap, addr, \
  311. &(val), NULL); \
  312. __ret ? : __err; \
  313. })
  314. static int thp7312_map_data_lanes(u8 *lane_remap, const u8 *lanes, u8 num_lanes)
  315. {
  316. u8 used_lanes = 0;
  317. u8 val = 0;
  318. unsigned int i;
  319. /*
  320. * The value that we write to the register is the index in the
  321. * data-lanes array, so we need to do a conversion. Do this in the same
  322. * pass as validating data-lanes.
  323. */
  324. for (i = 0; i < num_lanes; i++) {
  325. if (lanes[i] < 1 || lanes[i] > 4)
  326. return -EINVAL;
  327. if (used_lanes & (BIT(lanes[i])))
  328. return -EINVAL;
  329. used_lanes |= BIT(lanes[i]);
  330. /*
  331. * data-lanes is 1-indexed while the field position in the
  332. * register is 0-indexed.
  333. */
  334. val |= i << ((lanes[i] - 1) * 2);
  335. }
  336. *lane_remap = val;
  337. return 0;
  338. }
  339. static int thp7312_set_mipi_lanes(struct thp7312_device *thp7312)
  340. {
  341. struct device *dev = thp7312->dev;
  342. int ret = 0;
  343. u64 val;
  344. cci_write(thp7312->regmap, TH7312_REG_CUSTOM_MIPI_RD,
  345. thp7312->sensors[0].lane_remap, &ret);
  346. cci_write(thp7312->regmap, TH7312_REG_CUSTOM_MIPI_TD,
  347. thp7312->lane_remap, &ret);
  348. cci_write(thp7312->regmap, TH7312_REG_CUSTOM_MIPI_SET, 1, &ret);
  349. if (ret)
  350. return ret;
  351. ret = thp7312_read_poll_timeout(thp7312, TH7312_REG_CUSTOM_MIPI_STATUS,
  352. val, val == 0x00, 100000, 2000000);
  353. if (ret) {
  354. dev_err(dev, "Failed to poll MIPI lane status: %d\n", ret);
  355. return ret;
  356. }
  357. return 0;
  358. }
  359. static int thp7312_change_mode(struct thp7312_device *thp7312,
  360. const struct thp7312_mode_info *mode,
  361. const struct thp7312_frame_rate *rate)
  362. {
  363. struct device *dev = thp7312->dev;
  364. u64 val = 0;
  365. int ret;
  366. ret = thp7312_read_poll_timeout(thp7312, THP7312_REG_CAMERA_STATUS, val,
  367. val == 0x80, 20000, 200000);
  368. if (ret < 0) {
  369. dev_err(dev, "%s(): failed to poll ISP: %d\n", __func__, ret);
  370. return ret;
  371. }
  372. cci_write(thp7312->regmap, THP7312_REG_VIDEO_IMAGE_SIZE,
  373. mode->reg_image_size, &ret);
  374. cci_write(thp7312->regmap, THP7312_REG_VIDEO_FRAME_RATE_MODE,
  375. rate->reg_frame_rate_mode, &ret);
  376. cci_write(thp7312->regmap, THP7312_REG_JPEG_COMPRESSION_FACTOR, 0x5e,
  377. &ret);
  378. cci_write(thp7312->regmap, THP7312_REG_SET_DRIVING_MODE, 0x01, &ret);
  379. if (ret)
  380. return ret;
  381. ret = thp7312_read_poll_timeout(thp7312, THP7312_REG_DRIVING_MODE_STATUS,
  382. val, val == 0x01, 20000, 100000);
  383. if (ret < 0) {
  384. dev_err(dev, "%s(): failed\n", __func__);
  385. return ret;
  386. }
  387. return 0;
  388. }
  389. static int thp7312_set_framefmt(struct thp7312_device *thp7312,
  390. struct v4l2_mbus_framefmt *format)
  391. {
  392. u8 val;
  393. switch (format->code) {
  394. case MEDIA_BUS_FMT_UYVY8_1X16:
  395. /* YUV422, UYVY */
  396. val = THP7312_REG_SET_OUTPUT_COLOR_UYVY;
  397. break;
  398. case MEDIA_BUS_FMT_YUYV8_1X16:
  399. /* YUV422, YUYV */
  400. val = THP7312_REG_SET_OUTPUT_COLOR_YUY2;
  401. break;
  402. default:
  403. /* Should never happen */
  404. return -EINVAL;
  405. }
  406. return cci_write(thp7312->regmap,
  407. THP7312_REG_SET_OUTPUT_COLOR_COMPRESSION, val, NULL);
  408. }
  409. static int thp7312_init_mode(struct thp7312_device *thp7312,
  410. struct v4l2_subdev_state *sd_state)
  411. {
  412. const struct thp7312_mode_info *mode;
  413. const struct thp7312_frame_rate *rate;
  414. struct v4l2_mbus_framefmt *fmt;
  415. struct v4l2_fract *interval;
  416. int ret;
  417. /*
  418. * TODO: The mode and rate should be cached in the subdev state, once
  419. * support for extending states will be available.
  420. */
  421. fmt = v4l2_subdev_state_get_format(sd_state, 0);
  422. interval = v4l2_subdev_state_get_interval(sd_state, 0);
  423. mode = thp7312_find_mode(fmt->width, fmt->height, false);
  424. rate = thp7312_find_rate(mode, interval->denominator, false);
  425. if (WARN_ON(!mode || !rate))
  426. return -EINVAL;
  427. ret = thp7312_set_framefmt(thp7312, fmt);
  428. if (ret)
  429. return ret;
  430. return thp7312_change_mode(thp7312, mode, rate);
  431. }
  432. static int thp7312_stream_enable(struct thp7312_device *thp7312, bool enable)
  433. {
  434. return cci_write(thp7312->regmap, THP7312_REG_SET_OUTPUT_ENABLE,
  435. enable ? THP7312_OUTPUT_ENABLE : THP7312_OUTPUT_DISABLE,
  436. NULL);
  437. }
  438. static int thp7312_check_status_stream_mode(struct thp7312_device *thp7312)
  439. {
  440. struct device *dev = thp7312->dev;
  441. u64 status = 0;
  442. int ret;
  443. while (status != 0x80) {
  444. ret = cci_read(thp7312->regmap, THP7312_REG_CAMERA_STATUS,
  445. &status, NULL);
  446. if (ret)
  447. return ret;
  448. if (status == 0x80) {
  449. dev_dbg(dev, "Camera initialization done\n");
  450. return 0;
  451. }
  452. if (status != 0x00) {
  453. dev_err(dev, "Invalid camera status %llx\n", status);
  454. return -EINVAL;
  455. }
  456. dev_dbg(dev, "Camera initializing...\n");
  457. usleep_range(70000, 80000);
  458. }
  459. return 0;
  460. }
  461. static void thp7312_reset(struct thp7312_device *thp7312)
  462. {
  463. unsigned long rate;
  464. gpiod_set_value_cansleep(thp7312->reset_gpio, 1);
  465. /*
  466. * The minimum reset duration is 8 clock cycles, make it 10 to provide
  467. * a safety margin.
  468. */
  469. rate = clk_get_rate(thp7312->iclk);
  470. fsleep(DIV_ROUND_UP(10 * USEC_PER_SEC, rate));
  471. gpiod_set_value_cansleep(thp7312->reset_gpio, 0);
  472. /*
  473. * TODO: The documentation states that the device needs 2ms to
  474. * initialize after reset is deasserted. It then proceeds to load the
  475. * firmware from the flash memory, which takes an unspecified amount of
  476. * time. Check if this delay could be reduced.
  477. */
  478. fsleep(300000);
  479. }
  480. /* -----------------------------------------------------------------------------
  481. * Power Management
  482. */
  483. static void __thp7312_power_off(struct thp7312_device *thp7312)
  484. {
  485. regulator_bulk_disable(ARRAY_SIZE(thp7312->supplies), thp7312->supplies);
  486. clk_disable_unprepare(thp7312->iclk);
  487. }
  488. static void thp7312_power_off(struct thp7312_device *thp7312)
  489. {
  490. __thp7312_power_off(thp7312);
  491. }
  492. static int __thp7312_power_on(struct thp7312_device *thp7312)
  493. {
  494. struct device *dev = thp7312->dev;
  495. int ret;
  496. ret = regulator_bulk_enable(ARRAY_SIZE(thp7312->supplies),
  497. thp7312->supplies);
  498. if (ret < 0)
  499. return ret;
  500. ret = clk_prepare_enable(thp7312->iclk);
  501. if (ret < 0) {
  502. dev_err(dev, "clk prepare enable failed\n");
  503. regulator_bulk_disable(ARRAY_SIZE(thp7312->supplies),
  504. thp7312->supplies);
  505. return ret;
  506. }
  507. /*
  508. * We cannot assume that turning off and on again will reset, so do a
  509. * software reset on power up.
  510. */
  511. thp7312_reset(thp7312);
  512. return 0;
  513. }
  514. static int thp7312_power_on(struct thp7312_device *thp7312)
  515. {
  516. int ret;
  517. ret = __thp7312_power_on(thp7312);
  518. if (ret < 0)
  519. return ret;
  520. ret = thp7312_check_status_stream_mode(thp7312);
  521. if (ret < 0)
  522. goto error;
  523. ret = thp7312_set_mipi_lanes(thp7312);
  524. if (ret)
  525. goto error;
  526. return 0;
  527. error:
  528. thp7312_power_off(thp7312);
  529. return ret;
  530. }
  531. static int __maybe_unused thp7312_pm_runtime_suspend(struct device *dev)
  532. {
  533. struct v4l2_subdev *sd = dev_get_drvdata(dev);
  534. struct thp7312_device *thp7312 = to_thp7312_dev(sd);
  535. thp7312_power_off(thp7312);
  536. thp7312->ctrls_applied = false;
  537. return 0;
  538. }
  539. static int __maybe_unused thp7312_pm_runtime_resume(struct device *dev)
  540. {
  541. struct v4l2_subdev *sd = dev_get_drvdata(dev);
  542. struct thp7312_device *thp7312 = to_thp7312_dev(sd);
  543. return thp7312_power_on(thp7312);
  544. }
  545. static const struct dev_pm_ops thp7312_pm_ops = {
  546. SET_RUNTIME_PM_OPS(thp7312_pm_runtime_suspend,
  547. thp7312_pm_runtime_resume, NULL)
  548. };
  549. /* -----------------------------------------------------------------------------
  550. * V4L2 Subdev Operations
  551. */
  552. static bool thp7312_find_bus_code(u32 code)
  553. {
  554. unsigned int i;
  555. for (i = 0; i < ARRAY_SIZE(thp7312_colour_fmts); ++i) {
  556. if (thp7312_colour_fmts[i] == code)
  557. return true;
  558. }
  559. return false;
  560. }
  561. static int thp7312_enum_mbus_code(struct v4l2_subdev *sd,
  562. struct v4l2_subdev_state *sd_state,
  563. struct v4l2_subdev_mbus_code_enum *code)
  564. {
  565. if (code->index >= ARRAY_SIZE(thp7312_colour_fmts))
  566. return -EINVAL;
  567. code->code = thp7312_colour_fmts[code->index];
  568. return 0;
  569. }
  570. static int thp7312_enum_frame_size(struct v4l2_subdev *sd,
  571. struct v4l2_subdev_state *sd_state,
  572. struct v4l2_subdev_frame_size_enum *fse)
  573. {
  574. if (!thp7312_find_bus_code(fse->code))
  575. return -EINVAL;
  576. if (fse->index >= ARRAY_SIZE(thp7312_mode_info_data))
  577. return -EINVAL;
  578. fse->min_width = thp7312_mode_info_data[fse->index].width;
  579. fse->max_width = fse->min_width;
  580. fse->min_height = thp7312_mode_info_data[fse->index].height;
  581. fse->max_height = fse->min_height;
  582. return 0;
  583. }
  584. static int thp7312_enum_frame_interval(struct v4l2_subdev *sd,
  585. struct v4l2_subdev_state *sd_state,
  586. struct v4l2_subdev_frame_interval_enum *fie)
  587. {
  588. const struct thp7312_frame_rate *rate;
  589. const struct thp7312_mode_info *mode;
  590. unsigned int index = fie->index;
  591. if (!thp7312_find_bus_code(fie->code))
  592. return -EINVAL;
  593. mode = thp7312_find_mode(fie->width, fie->height, false);
  594. if (!mode)
  595. return -EINVAL;
  596. for (rate = mode->rates; rate->fps; ++rate, --index) {
  597. if (!index) {
  598. fie->interval.numerator = 1;
  599. fie->interval.denominator = rate->fps;
  600. return 0;
  601. }
  602. }
  603. return -EINVAL;
  604. }
  605. static int thp7312_set_fmt(struct v4l2_subdev *sd,
  606. struct v4l2_subdev_state *sd_state,
  607. struct v4l2_subdev_format *format)
  608. {
  609. struct thp7312_device *thp7312 = to_thp7312_dev(sd);
  610. struct v4l2_mbus_framefmt *mbus_fmt = &format->format;
  611. struct v4l2_mbus_framefmt *fmt;
  612. struct v4l2_fract *interval;
  613. const struct thp7312_mode_info *mode;
  614. if (!thp7312_find_bus_code(mbus_fmt->code))
  615. mbus_fmt->code = thp7312_colour_fmts[0];
  616. mode = thp7312_find_mode(mbus_fmt->width, mbus_fmt->height, true);
  617. fmt = v4l2_subdev_state_get_format(sd_state, 0);
  618. fmt->code = mbus_fmt->code;
  619. fmt->width = mode->width;
  620. fmt->height = mode->height;
  621. fmt->colorspace = V4L2_COLORSPACE_SRGB;
  622. fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace);
  623. fmt->quantization = V4L2_QUANTIZATION_FULL_RANGE;
  624. fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace);
  625. *mbus_fmt = *fmt;
  626. interval = v4l2_subdev_state_get_interval(sd_state, 0);
  627. interval->numerator = 1;
  628. interval->denominator = mode->rates[0].fps;
  629. if (format->which == V4L2_SUBDEV_FORMAT_ACTIVE)
  630. thp7312->link_freq = mode->rates[0].link_freq;
  631. return 0;
  632. }
  633. static int thp7312_set_frame_interval(struct v4l2_subdev *sd,
  634. struct v4l2_subdev_state *sd_state,
  635. struct v4l2_subdev_frame_interval *fi)
  636. {
  637. struct thp7312_device *thp7312 = to_thp7312_dev(sd);
  638. const struct thp7312_mode_info *mode;
  639. const struct thp7312_frame_rate *rate;
  640. const struct v4l2_mbus_framefmt *fmt;
  641. struct v4l2_fract *interval;
  642. unsigned int fps;
  643. /* Avoid divisions by 0, pick the highest frame if the interval is 0. */
  644. fps = fi->interval.numerator
  645. ? DIV_ROUND_CLOSEST(fi->interval.denominator, fi->interval.numerator)
  646. : UINT_MAX;
  647. fmt = v4l2_subdev_state_get_format(sd_state, 0);
  648. mode = thp7312_find_mode(fmt->width, fmt->height, false);
  649. rate = thp7312_find_rate(mode, fps, true);
  650. interval = v4l2_subdev_state_get_interval(sd_state, 0);
  651. interval->numerator = 1;
  652. interval->denominator = rate->fps;
  653. if (fi->which == V4L2_SUBDEV_FORMAT_ACTIVE)
  654. thp7312->link_freq = rate->link_freq;
  655. fi->interval = *interval;
  656. return 0;
  657. }
  658. static int thp7312_s_stream(struct v4l2_subdev *sd, int enable)
  659. {
  660. struct thp7312_device *thp7312 = to_thp7312_dev(sd);
  661. struct v4l2_subdev_state *sd_state;
  662. int ret;
  663. sd_state = v4l2_subdev_lock_and_get_active_state(sd);
  664. if (!enable) {
  665. thp7312_stream_enable(thp7312, false);
  666. pm_runtime_mark_last_busy(thp7312->dev);
  667. pm_runtime_put_autosuspend(thp7312->dev);
  668. v4l2_subdev_unlock_state(sd_state);
  669. return 0;
  670. }
  671. ret = pm_runtime_resume_and_get(thp7312->dev);
  672. if (ret)
  673. goto finish_unlock;
  674. ret = thp7312_init_mode(thp7312, sd_state);
  675. if (ret)
  676. goto finish_pm;
  677. if (!thp7312->ctrls_applied) {
  678. ret = __v4l2_ctrl_handler_setup(&thp7312->ctrl_handler);
  679. if (ret)
  680. goto finish_pm;
  681. thp7312->ctrls_applied = true;
  682. }
  683. ret = thp7312_stream_enable(thp7312, true);
  684. if (ret)
  685. goto finish_pm;
  686. goto finish_unlock;
  687. finish_pm:
  688. pm_runtime_mark_last_busy(thp7312->dev);
  689. pm_runtime_put_autosuspend(thp7312->dev);
  690. finish_unlock:
  691. v4l2_subdev_unlock_state(sd_state);
  692. return ret;
  693. }
  694. static int thp7312_init_state(struct v4l2_subdev *sd,
  695. struct v4l2_subdev_state *sd_state)
  696. {
  697. const struct thp7312_mode_info *default_mode = &thp7312_mode_info_data[0];
  698. struct v4l2_mbus_framefmt *fmt;
  699. struct v4l2_fract *interval;
  700. fmt = v4l2_subdev_state_get_format(sd_state, 0);
  701. interval = v4l2_subdev_state_get_interval(sd_state, 0);
  702. /*
  703. * default init sequence initialize thp7312 to
  704. * YUV422 YUYV VGA@30fps
  705. */
  706. fmt->code = MEDIA_BUS_FMT_YUYV8_1X16;
  707. fmt->colorspace = V4L2_COLORSPACE_SRGB;
  708. fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace);
  709. fmt->quantization = V4L2_QUANTIZATION_FULL_RANGE;
  710. fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace);
  711. fmt->width = default_mode->width;
  712. fmt->height = default_mode->height;
  713. fmt->field = V4L2_FIELD_NONE;
  714. interval->numerator = 1;
  715. interval->denominator = default_mode->rates[0].fps;
  716. return 0;
  717. }
  718. static const struct v4l2_subdev_core_ops thp7312_core_ops = {
  719. .log_status = v4l2_ctrl_subdev_log_status,
  720. .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
  721. .unsubscribe_event = v4l2_event_subdev_unsubscribe,
  722. };
  723. static const struct v4l2_subdev_video_ops thp7312_video_ops = {
  724. .s_stream = thp7312_s_stream,
  725. };
  726. static const struct v4l2_subdev_pad_ops thp7312_pad_ops = {
  727. .enum_mbus_code = thp7312_enum_mbus_code,
  728. .get_fmt = v4l2_subdev_get_fmt,
  729. .set_fmt = thp7312_set_fmt,
  730. .get_frame_interval = v4l2_subdev_get_frame_interval,
  731. .set_frame_interval = thp7312_set_frame_interval,
  732. .enum_frame_size = thp7312_enum_frame_size,
  733. .enum_frame_interval = thp7312_enum_frame_interval,
  734. };
  735. static const struct v4l2_subdev_ops thp7312_subdev_ops = {
  736. .core = &thp7312_core_ops,
  737. .video = &thp7312_video_ops,
  738. .pad = &thp7312_pad_ops,
  739. };
  740. static const struct v4l2_subdev_internal_ops thp7312_internal_ops = {
  741. .init_state = thp7312_init_state,
  742. };
  743. /* -----------------------------------------------------------------------------
  744. * V4L2 Control Operations
  745. */
  746. static inline struct thp7312_device *to_thp7312_from_ctrl(struct v4l2_ctrl *ctrl)
  747. {
  748. return container_of(ctrl->handler, struct thp7312_device, ctrl_handler);
  749. }
  750. /* 0: 3000cm, 18: 8cm */
  751. static const u16 thp7312_focus_values[] = {
  752. 3000, 1000, 600, 450, 350,
  753. 290, 240, 200, 170, 150,
  754. 140, 130, 120, 110, 100,
  755. 93, 87, 83, 80,
  756. };
  757. static int thp7312_set_focus(struct thp7312_device *thp7312)
  758. {
  759. enum thp7312_focus_state new_state = thp7312->focus_state;
  760. bool continuous;
  761. u8 af_control;
  762. u8 af_setting;
  763. int ret = 0;
  764. /* Start by programming the manual focus position if it has changed. */
  765. if (thp7312->focus_absolute->is_new) {
  766. unsigned int value;
  767. value = thp7312_focus_values[thp7312->focus_absolute->val];
  768. ret = cci_write(thp7312->regmap,
  769. THP7312_REG_MANUAL_FOCUS_POSITION, value, NULL);
  770. if (ret)
  771. return ret;
  772. }
  773. /* Calculate the new focus state. */
  774. switch (thp7312->focus_state) {
  775. case THP7312_FOCUS_STATE_MANUAL:
  776. default:
  777. if (thp7312->focus_auto->val)
  778. new_state = THP7312_FOCUS_STATE_AUTO;
  779. else if (thp7312->focus_start->is_new)
  780. new_state = THP7312_FOCUS_STATE_ONESHOT;
  781. break;
  782. case THP7312_FOCUS_STATE_AUTO:
  783. if (!thp7312->focus_auto->val)
  784. new_state = THP7312_FOCUS_STATE_LOCKED;
  785. break;
  786. case THP7312_FOCUS_STATE_LOCKED:
  787. if (thp7312->focus_auto->val)
  788. new_state = THP7312_FOCUS_STATE_AUTO;
  789. else if (thp7312->focus_start->is_new)
  790. new_state = THP7312_FOCUS_STATE_ONESHOT;
  791. else if (thp7312->focus_absolute->is_new)
  792. new_state = THP7312_FOCUS_STATE_MANUAL;
  793. break;
  794. case THP7312_FOCUS_STATE_ONESHOT:
  795. if (thp7312->focus_auto->val)
  796. new_state = THP7312_FOCUS_STATE_AUTO;
  797. else if (thp7312->focus_start->is_new)
  798. new_state = THP7312_FOCUS_STATE_ONESHOT;
  799. else if (thp7312->focus_absolute->is_new)
  800. new_state = THP7312_FOCUS_STATE_MANUAL;
  801. break;
  802. }
  803. /*
  804. * If neither the state nor the focus method has changed, and no new
  805. * one-shot focus is requested, there's nothing new to program to the
  806. * hardware.
  807. */
  808. if (thp7312->focus_state == new_state &&
  809. !thp7312->focus_method->is_new && !thp7312->focus_start->is_new)
  810. return 0;
  811. continuous = new_state == THP7312_FOCUS_STATE_MANUAL ||
  812. new_state == THP7312_FOCUS_STATE_ONESHOT;
  813. switch (thp7312->focus_method->val) {
  814. case THP7312_FOCUS_METHOD_CONTRAST:
  815. default:
  816. af_setting = continuous
  817. ? THP7312_REG_AF_SETTING_CONTINUOUS_CONTRAST
  818. : THP7312_REG_AF_SETTING_ONESHOT_CONTRAST;
  819. break;
  820. case THP7312_FOCUS_METHOD_PDAF:
  821. af_setting = continuous
  822. ? THP7312_REG_AF_SETTING_CONTINUOUS_PDAF
  823. : THP7312_REG_AF_SETTING_ONESHOT_PDAF;
  824. break;
  825. case THP7312_FOCUS_METHOD_HYBRID:
  826. af_setting = continuous
  827. ? THP7312_REG_AF_SETTING_CONTINUOUS_HYBRID
  828. : THP7312_REG_AF_SETTING_ONESHOT_HYBRID;
  829. break;
  830. }
  831. switch (new_state) {
  832. case THP7312_FOCUS_STATE_MANUAL:
  833. default:
  834. af_control = THP7312_REG_AF_CONTROL_MANUAL;
  835. break;
  836. case THP7312_FOCUS_STATE_AUTO:
  837. case THP7312_FOCUS_STATE_ONESHOT:
  838. af_control = THP7312_REG_AF_CONTROL_AF;
  839. break;
  840. case THP7312_FOCUS_STATE_LOCKED:
  841. af_control = THP7312_REG_AF_CONTROL_LOCK;
  842. break;
  843. }
  844. cci_write(thp7312->regmap, THP7312_REG_AF_SETTING, af_setting, &ret);
  845. if (new_state == THP7312_FOCUS_STATE_MANUAL &&
  846. (thp7312->focus_state == THP7312_FOCUS_STATE_AUTO ||
  847. thp7312->focus_state == THP7312_FOCUS_STATE_ONESHOT)) {
  848. /* When switching to manual state, lock AF first. */
  849. cci_write(thp7312->regmap, THP7312_REG_AF_CONTROL,
  850. THP7312_REG_AF_CONTROL_LOCK, &ret);
  851. }
  852. cci_write(thp7312->regmap, THP7312_REG_AF_CONTROL, af_control, &ret);
  853. if (ret)
  854. return ret;
  855. thp7312->focus_state = new_state;
  856. return 0;
  857. }
  858. static int thp7312_s_ctrl(struct v4l2_ctrl *ctrl)
  859. {
  860. struct thp7312_device *thp7312 = to_thp7312_from_ctrl(ctrl);
  861. int ret = 0;
  862. u8 value;
  863. if (ctrl->flags & V4L2_CTRL_FLAG_INACTIVE)
  864. return -EINVAL;
  865. if (!pm_runtime_get_if_active(thp7312->dev))
  866. return 0;
  867. switch (ctrl->id) {
  868. case V4L2_CID_BRIGHTNESS:
  869. cci_write(thp7312->regmap, THP7312_REG_BRIGHTNESS,
  870. ctrl->val + 10, &ret);
  871. break;
  872. case V4L2_CID_THP7312_LOW_LIGHT_COMPENSATION:
  873. /* 0 = Auto adjust frame rate, 1 = Fix frame rate */
  874. cci_write(thp7312->regmap, THP7312_REG_AE_FIX_FRAME_RATE,
  875. ctrl->val ? 0 : 1, &ret);
  876. break;
  877. case V4L2_CID_FOCUS_AUTO:
  878. case V4L2_CID_FOCUS_ABSOLUTE:
  879. case V4L2_CID_AUTO_FOCUS_START:
  880. case V4L2_CID_THP7312_AUTO_FOCUS_METHOD:
  881. ret = thp7312_set_focus(thp7312);
  882. break;
  883. case V4L2_CID_HFLIP:
  884. case V4L2_CID_VFLIP:
  885. value = (thp7312->hflip->val ? THP7312_REG_FLIP_MIRROR_MIRROR : 0)
  886. | (thp7312->vflip->val ? THP7312_REG_FLIP_MIRROR_FLIP : 0);
  887. cci_write(thp7312->regmap, THP7312_REG_FLIP_MIRROR, value, &ret);
  888. break;
  889. case V4L2_CID_THP7312_NOISE_REDUCTION_AUTO:
  890. case V4L2_CID_THP7312_NOISE_REDUCTION_ABSOLUTE:
  891. value = thp7312->noise_reduction_auto->val ? 0
  892. : THP7312_REG_NOISE_REDUCTION_FIXED |
  893. thp7312->noise_reduction_absolute->val;
  894. cci_write(thp7312->regmap, THP7312_REG_NOISE_REDUCTION, value,
  895. &ret);
  896. break;
  897. case V4L2_CID_AUTO_WHITE_BALANCE:
  898. value = ctrl->val ? THP7312_WB_MODE_AUTO : THP7312_WB_MODE_MANUAL;
  899. cci_write(thp7312->regmap, THP7312_REG_WB_MODE, value, &ret);
  900. break;
  901. case V4L2_CID_RED_BALANCE:
  902. cci_write(thp7312->regmap, THP7312_REG_MANUAL_WB_RED_GAIN,
  903. ctrl->val, &ret);
  904. break;
  905. case V4L2_CID_BLUE_BALANCE:
  906. cci_write(thp7312->regmap, THP7312_REG_MANUAL_WB_BLUE_GAIN,
  907. ctrl->val, &ret);
  908. break;
  909. case V4L2_CID_AUTO_EXPOSURE_BIAS:
  910. cci_write(thp7312->regmap, THP7312_REG_AE_EXPOSURE_COMPENSATION,
  911. ctrl->val, &ret);
  912. break;
  913. case V4L2_CID_POWER_LINE_FREQUENCY:
  914. if (ctrl->val == V4L2_CID_POWER_LINE_FREQUENCY_60HZ) {
  915. value = THP7312_AE_FLICKER_MODE_60;
  916. } else if (ctrl->val == V4L2_CID_POWER_LINE_FREQUENCY_50HZ) {
  917. value = THP7312_AE_FLICKER_MODE_50;
  918. } else {
  919. if (thp7312->fw_version == THP7312_FW_VERSION(40, 3)) {
  920. /* THP7312_AE_FLICKER_MODE_DISABLE is not supported */
  921. value = THP7312_AE_FLICKER_MODE_50;
  922. } else {
  923. value = THP7312_AE_FLICKER_MODE_DISABLE;
  924. }
  925. }
  926. cci_write(thp7312->regmap, THP7312_REG_AE_FLICKER_MODE,
  927. value, &ret);
  928. break;
  929. case V4L2_CID_SATURATION:
  930. cci_write(thp7312->regmap, THP7312_REG_SATURATION,
  931. ctrl->val, &ret);
  932. break;
  933. case V4L2_CID_CONTRAST:
  934. cci_write(thp7312->regmap, THP7312_REG_CONTRAST,
  935. ctrl->val, &ret);
  936. break;
  937. case V4L2_CID_SHARPNESS:
  938. cci_write(thp7312->regmap, THP7312_REG_SHARPNESS,
  939. ctrl->val, &ret);
  940. break;
  941. default:
  942. break;
  943. }
  944. pm_runtime_mark_last_busy(thp7312->dev);
  945. pm_runtime_put_autosuspend(thp7312->dev);
  946. return ret;
  947. }
  948. static const struct v4l2_ctrl_ops thp7312_ctrl_ops = {
  949. .s_ctrl = thp7312_s_ctrl,
  950. };
  951. /*
  952. * Refer to Documentation/userspace-api/media/drivers/thp7312.rst for details.
  953. */
  954. static const struct v4l2_ctrl_config thp7312_ctrl_focus_method_cdaf = {
  955. .ops = &thp7312_ctrl_ops,
  956. .id = V4L2_CID_THP7312_AUTO_FOCUS_METHOD,
  957. .name = "Auto-Focus Method",
  958. .type = V4L2_CTRL_TYPE_INTEGER,
  959. .min = THP7312_FOCUS_METHOD_CONTRAST,
  960. .def = THP7312_FOCUS_METHOD_CONTRAST,
  961. .max = THP7312_FOCUS_METHOD_CONTRAST,
  962. .step = 1,
  963. };
  964. static const struct v4l2_ctrl_config thp7312_ctrl_focus_method_pdaf = {
  965. .ops = &thp7312_ctrl_ops,
  966. .id = V4L2_CID_THP7312_AUTO_FOCUS_METHOD,
  967. .name = "Auto-Focus Method",
  968. .type = V4L2_CTRL_TYPE_INTEGER,
  969. .min = THP7312_FOCUS_METHOD_CONTRAST,
  970. .def = THP7312_FOCUS_METHOD_HYBRID,
  971. .max = THP7312_FOCUS_METHOD_HYBRID,
  972. .step = 1,
  973. };
  974. static const struct v4l2_ctrl_config thp7312_v4l2_ctrls_custom[] = {
  975. {
  976. .ops = &thp7312_ctrl_ops,
  977. .id = V4L2_CID_THP7312_LOW_LIGHT_COMPENSATION,
  978. .name = "Low Light Compensation",
  979. .type = V4L2_CTRL_TYPE_BOOLEAN,
  980. .min = 0,
  981. .def = 1,
  982. .max = 1,
  983. .step = 1,
  984. }, {
  985. .ops = &thp7312_ctrl_ops,
  986. .id = V4L2_CID_THP7312_NOISE_REDUCTION_AUTO,
  987. .name = "Noise Reduction Auto",
  988. .type = V4L2_CTRL_TYPE_BOOLEAN,
  989. .min = 0,
  990. .def = 1,
  991. .max = 1,
  992. .step = 1,
  993. }, {
  994. .ops = &thp7312_ctrl_ops,
  995. .id = V4L2_CID_THP7312_NOISE_REDUCTION_ABSOLUTE,
  996. .name = "Noise Reduction Level",
  997. .type = V4L2_CTRL_TYPE_INTEGER,
  998. .min = 0,
  999. .def = 0,
  1000. .max = 10,
  1001. .step = 1,
  1002. },
  1003. };
  1004. static const s64 exp_bias_qmenu[] = {
  1005. -2000, -1667, -1333, -1000, -667, -333, 0, 333, 667, 1000, 1333, 1667, 2000
  1006. };
  1007. static int thp7312_init_controls(struct thp7312_device *thp7312)
  1008. {
  1009. struct v4l2_ctrl_handler *hdl = &thp7312->ctrl_handler;
  1010. struct device *dev = thp7312->dev;
  1011. struct v4l2_fwnode_device_properties props;
  1012. struct v4l2_ctrl *link_freq;
  1013. unsigned int num_controls;
  1014. unsigned int i;
  1015. u8 af_support;
  1016. int ret;
  1017. /*
  1018. * Check what auto-focus methods the connected sensor supports, if any.
  1019. * Firmwares before v90.03 didn't expose the AF_SUPPORT register,
  1020. * consider both CDAF and PDAF as supported in that case.
  1021. */
  1022. if (thp7312->fw_version >= THP7312_FW_VERSION(90, 3)) {
  1023. u64 val;
  1024. ret = cci_read(thp7312->regmap, THP7312_REG_AF_SUPPORT, &val,
  1025. NULL);
  1026. if (ret)
  1027. return ret;
  1028. af_support = val & (THP7312_AF_SUPPORT_PDAF |
  1029. THP7312_AF_SUPPORT_CONTRAST);
  1030. } else {
  1031. af_support = THP7312_AF_SUPPORT_PDAF
  1032. | THP7312_AF_SUPPORT_CONTRAST;
  1033. }
  1034. num_controls = 14 + ARRAY_SIZE(thp7312_v4l2_ctrls_custom)
  1035. + (af_support ? 4 : 0);
  1036. v4l2_ctrl_handler_init(hdl, num_controls);
  1037. if (af_support) {
  1038. const struct v4l2_ctrl_config *af_method;
  1039. af_method = af_support & THP7312_AF_SUPPORT_PDAF
  1040. ? &thp7312_ctrl_focus_method_pdaf
  1041. : &thp7312_ctrl_focus_method_cdaf;
  1042. thp7312->focus_state = THP7312_FOCUS_STATE_MANUAL;
  1043. thp7312->focus_auto =
  1044. v4l2_ctrl_new_std(hdl, &thp7312_ctrl_ops,
  1045. V4L2_CID_FOCUS_AUTO,
  1046. 0, 1, 1, 1);
  1047. thp7312->focus_absolute =
  1048. v4l2_ctrl_new_std(hdl, &thp7312_ctrl_ops,
  1049. V4L2_CID_FOCUS_ABSOLUTE,
  1050. 0, ARRAY_SIZE(thp7312_focus_values),
  1051. 1, 0);
  1052. thp7312->focus_method =
  1053. v4l2_ctrl_new_custom(hdl, af_method, NULL);
  1054. thp7312->focus_start =
  1055. v4l2_ctrl_new_std(hdl, &thp7312_ctrl_ops,
  1056. V4L2_CID_AUTO_FOCUS_START,
  1057. 1, 1, 1, 1);
  1058. v4l2_ctrl_cluster(4, &thp7312->focus_auto);
  1059. }
  1060. v4l2_ctrl_new_std(hdl, &thp7312_ctrl_ops, V4L2_CID_AUTO_WHITE_BALANCE,
  1061. 0, 1, 1, 1);
  1062. /* 32: 1x, 255: 7.95x */
  1063. v4l2_ctrl_new_std(hdl, &thp7312_ctrl_ops, V4L2_CID_RED_BALANCE,
  1064. 32, 255, 1, 64);
  1065. /* 32: 1x, 255: 7.95x */
  1066. v4l2_ctrl_new_std(hdl, &thp7312_ctrl_ops, V4L2_CID_BLUE_BALANCE,
  1067. 32, 255, 1, 50);
  1068. v4l2_ctrl_new_std(hdl, &thp7312_ctrl_ops, V4L2_CID_BRIGHTNESS,
  1069. -10, 10, 1, 0);
  1070. v4l2_ctrl_new_std(hdl, &thp7312_ctrl_ops, V4L2_CID_SATURATION,
  1071. 0, 31, 1, 10);
  1072. v4l2_ctrl_new_std(hdl, &thp7312_ctrl_ops, V4L2_CID_CONTRAST,
  1073. 0, 20, 1, 10);
  1074. v4l2_ctrl_new_std(hdl, &thp7312_ctrl_ops, V4L2_CID_SHARPNESS,
  1075. 0, 31, 1, 8);
  1076. thp7312->hflip = v4l2_ctrl_new_std(hdl, &thp7312_ctrl_ops,
  1077. V4L2_CID_HFLIP, 0, 1, 1, 0);
  1078. thp7312->vflip = v4l2_ctrl_new_std(hdl, &thp7312_ctrl_ops,
  1079. V4L2_CID_VFLIP, 0, 1, 1, 0);
  1080. v4l2_ctrl_cluster(2, &thp7312->hflip);
  1081. v4l2_ctrl_new_int_menu(hdl, &thp7312_ctrl_ops,
  1082. V4L2_CID_AUTO_EXPOSURE_BIAS,
  1083. ARRAY_SIZE(exp_bias_qmenu) - 1,
  1084. ARRAY_SIZE(exp_bias_qmenu) / 2, exp_bias_qmenu);
  1085. v4l2_ctrl_new_std_menu(hdl, &thp7312_ctrl_ops,
  1086. V4L2_CID_POWER_LINE_FREQUENCY,
  1087. V4L2_CID_POWER_LINE_FREQUENCY_60HZ, 0,
  1088. V4L2_CID_POWER_LINE_FREQUENCY_50HZ);
  1089. thp7312->link_freq = thp7312_mode_info_data[0].rates[0].link_freq;
  1090. link_freq = v4l2_ctrl_new_int_menu(hdl, &thp7312_ctrl_ops,
  1091. V4L2_CID_LINK_FREQ, 0, 0,
  1092. &thp7312->link_freq);
  1093. /* Set properties from fwnode (e.g. rotation, orientation). */
  1094. ret = v4l2_fwnode_device_parse(dev, &props);
  1095. if (ret) {
  1096. dev_err(dev, "Failed to parse fwnode: %d\n", ret);
  1097. goto error;
  1098. }
  1099. ret = v4l2_ctrl_new_fwnode_properties(hdl, &thp7312_ctrl_ops, &props);
  1100. if (ret) {
  1101. dev_err(dev, "Failed to create new v4l2 ctrl for fwnode properties: %d\n", ret);
  1102. goto error;
  1103. }
  1104. for (i = 0; i < ARRAY_SIZE(thp7312_v4l2_ctrls_custom); i++) {
  1105. const struct v4l2_ctrl_config *ctrl_cfg =
  1106. &thp7312_v4l2_ctrls_custom[i];
  1107. struct v4l2_ctrl *ctrl;
  1108. ctrl = v4l2_ctrl_new_custom(hdl, ctrl_cfg, NULL);
  1109. if (ctrl_cfg->id == V4L2_CID_THP7312_NOISE_REDUCTION_AUTO)
  1110. thp7312->noise_reduction_auto = ctrl;
  1111. else if (ctrl_cfg->id == V4L2_CID_THP7312_NOISE_REDUCTION_ABSOLUTE)
  1112. thp7312->noise_reduction_absolute = ctrl;
  1113. }
  1114. v4l2_ctrl_cluster(2, &thp7312->noise_reduction_auto);
  1115. if (hdl->error) {
  1116. dev_err(dev, "v4l2_ctrl_handler error\n");
  1117. ret = hdl->error;
  1118. goto error;
  1119. }
  1120. link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
  1121. return ret;
  1122. error:
  1123. v4l2_ctrl_handler_free(hdl);
  1124. return ret;
  1125. }
  1126. /* -----------------------------------------------------------------------------
  1127. * Firmware Update
  1128. */
  1129. /*
  1130. * The firmware data is made of 128kB of RAM firmware, followed by a
  1131. * variable-size "header". Both are stored in flash memory.
  1132. */
  1133. #define THP7312_FW_RAM_SIZE (128 * 1024)
  1134. #define THP7312_FW_MIN_SIZE (THP7312_FW_RAM_SIZE + 4)
  1135. #define THP7312_FW_MAX_SIZE (THP7312_FW_RAM_SIZE + 64 * 1024)
  1136. /*
  1137. * Data is first uploaded to the THP7312 128kB SRAM, and then written to flash.
  1138. * The SRAM is exposed over I2C as 32kB banks, and up to 4kB of data can be
  1139. * transferred in a single I2C write.
  1140. */
  1141. #define THP7312_RAM_BANK_SIZE (32 * 1024)
  1142. #define THP7312_FW_DOWNLOAD_UNIT (4 * 1024)
  1143. #define THP7312_FLASH_MEMORY_ERASE_TIMEOUT 40
  1144. #define THP7312_FLASH_MAX_REG_READ_SIZE 10
  1145. #define THP7312_FLASH_MAX_REG_DATA_SIZE 10
  1146. static const u8 thp7312_cmd_config_flash_mem_if[] = {
  1147. 0xd5, 0x18, 0x00, 0x00, 0x00, 0x80
  1148. };
  1149. static const u8 thp7312_cmd_write_to_reg[] = {
  1150. 0xd5, 0x0c, 0x80, 0x00, 0x00, 0x00
  1151. };
  1152. static const u8 thp7312_cmd_read_reg[] = {
  1153. 0xd5, 0x04
  1154. };
  1155. /*
  1156. * THP7312 Write data from RAM to Flash Memory
  1157. * Command ID FF700F
  1158. * Format: FF700F AA AA AA BB BB BB
  1159. * AA AA AA: destination start address
  1160. * BB BB BB: (write size - 1)
  1161. * Source address always starts from 0
  1162. */
  1163. static const u8 thp7312_cmd_write_ram_to_flash[] = { 0xff, 0x70, 0x0f };
  1164. /*
  1165. * THP7312 Calculate CRC command
  1166. * Command ID: FF70 09
  1167. * Format: FF70 09 AA AA AA BB BB BB
  1168. * AA AA AA: Start address of calculation
  1169. * BB BB BB: (calculate size - 1)
  1170. */
  1171. static const u8 thp7312_cmd_calc_crc[] = { 0xff, 0x70, 0x09 };
  1172. static const u8 thp7312_jedec_rdid[] = { SPINOR_OP_RDID, 0x00, 0x00, 0x00 };
  1173. static const u8 thp7312_jedec_rdsr[] = { SPINOR_OP_RDSR, 0x00, 0x00, 0x00 };
  1174. static const u8 thp7312_jedec_wen[] = { SPINOR_OP_WREN };
  1175. static int thp7312_read_firmware_version(struct thp7312_device *thp7312)
  1176. {
  1177. u64 val = 0;
  1178. int ret = 0;
  1179. u8 major;
  1180. u8 minor;
  1181. cci_read(thp7312->regmap, THP7312_REG_FIRMWARE_VERSION_1, &val, &ret);
  1182. major = val;
  1183. cci_read(thp7312->regmap, THP7312_REG_FIRMWARE_VERSION_2, &val, &ret);
  1184. minor = val;
  1185. thp7312->fw_version = THP7312_FW_VERSION(major, minor);
  1186. return ret;
  1187. }
  1188. static int thp7312_write_buf(struct thp7312_device *thp7312,
  1189. const u8 *write_buf, u16 write_size)
  1190. {
  1191. struct i2c_client *client = to_i2c_client(thp7312->dev);
  1192. int ret;
  1193. ret = i2c_master_send(client, write_buf, write_size);
  1194. return ret >= 0 ? 0 : ret;
  1195. }
  1196. static int __thp7312_flash_reg_write(struct thp7312_device *thp7312,
  1197. const u8 *write_buf, u16 write_size)
  1198. {
  1199. struct device *dev = thp7312->dev;
  1200. u8 temp_write_buf[THP7312_FLASH_MAX_REG_DATA_SIZE + 2];
  1201. int ret;
  1202. if (write_size > THP7312_FLASH_MAX_REG_DATA_SIZE) {
  1203. dev_err(dev, "%s: Write size error size = %d\n",
  1204. __func__, write_size);
  1205. return -EINVAL;
  1206. }
  1207. ret = thp7312_write_buf(thp7312, thp7312_cmd_config_flash_mem_if,
  1208. sizeof(thp7312_cmd_config_flash_mem_if));
  1209. if (ret < 0) {
  1210. dev_err(dev, "%s: Failed to config flash memory IF: %d\n",
  1211. __func__, ret);
  1212. return ret;
  1213. }
  1214. temp_write_buf[0] = 0xd5;
  1215. temp_write_buf[1] = 0x00;
  1216. memcpy((temp_write_buf + 2), write_buf, write_size);
  1217. ret = thp7312_write_buf(thp7312, temp_write_buf, write_size + 2);
  1218. if (ret < 0)
  1219. return ret;
  1220. thp7312_write_buf(thp7312, thp7312_cmd_write_to_reg,
  1221. sizeof(thp7312_cmd_write_to_reg));
  1222. return 0;
  1223. }
  1224. static int __thp7312_flash_reg_read(struct thp7312_device *thp7312,
  1225. const u8 *write_buf, u16 write_size,
  1226. u8 *read_buf, u16 read_size)
  1227. {
  1228. struct i2c_client *client = to_i2c_client(thp7312->dev);
  1229. struct i2c_msg msgs[2];
  1230. int ret;
  1231. ret = __thp7312_flash_reg_write(thp7312, write_buf, write_size);
  1232. if (ret)
  1233. return ret;
  1234. msgs[0].addr = client->addr;
  1235. msgs[0].flags = 0;
  1236. msgs[0].len = sizeof(thp7312_cmd_read_reg);
  1237. msgs[0].buf = (u8 *)thp7312_cmd_read_reg;
  1238. msgs[1].addr = client->addr;
  1239. msgs[1].flags = I2C_M_RD;
  1240. msgs[1].len = read_size;
  1241. msgs[1].buf = read_buf;
  1242. ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
  1243. return ret >= 0 ? 0 : ret;
  1244. }
  1245. #define thp7312_flash_reg_write(thp7312, wrbuf) \
  1246. __thp7312_flash_reg_write(thp7312, wrbuf, sizeof(wrbuf))
  1247. #define thp7312_flash_reg_read(thp7312, wrbuf, rdbuf) \
  1248. __thp7312_flash_reg_read(thp7312, wrbuf, sizeof(wrbuf), \
  1249. rdbuf, sizeof(rdbuf))
  1250. static enum fw_upload_err thp7312_fw_prepare_config(struct thp7312_device *thp7312)
  1251. {
  1252. struct device *dev = thp7312->dev;
  1253. int ret;
  1254. ret = cci_write(thp7312->regmap, THP7312_REG_FW_MEMORY_IO_SETTING,
  1255. THP7312_FW_MEMORY_IO_GPIO0, NULL);
  1256. if (ret) {
  1257. dev_err(dev, "Failed to set flash memory I/O\n");
  1258. return FW_UPLOAD_ERR_HW_ERROR;
  1259. }
  1260. /* Set max drivability. */
  1261. ret = cci_write(thp7312->regmap, THP7312_REG_FW_DRIVABILITY, 0x00777777,
  1262. NULL);
  1263. if (ret) {
  1264. dev_err(dev, "Failed to set drivability: %d\n", ret);
  1265. return FW_UPLOAD_ERR_HW_ERROR;
  1266. }
  1267. return FW_UPLOAD_ERR_NONE;
  1268. }
  1269. static enum fw_upload_err thp7312_fw_prepare_check(struct thp7312_device *thp7312)
  1270. {
  1271. struct device *dev = thp7312->dev;
  1272. u8 read_buf[3] = { 0 };
  1273. int ret;
  1274. /* Get JEDEC ID */
  1275. ret = thp7312_flash_reg_read(thp7312, thp7312_jedec_rdid, read_buf);
  1276. if (ret) {
  1277. dev_err(dev, "Failed to get JEDEC ID: %d\n", ret);
  1278. return FW_UPLOAD_ERR_HW_ERROR;
  1279. }
  1280. dev_dbg(dev, "Flash Memory: JEDEC ID = 0x%x 0x%x 0x%x\n",
  1281. read_buf[0], read_buf[1], read_buf[2]);
  1282. return FW_UPLOAD_ERR_NONE;
  1283. }
  1284. static enum fw_upload_err thp7312_fw_prepare_reset(struct thp7312_device *thp7312)
  1285. {
  1286. struct device *dev = thp7312->dev;
  1287. int ret;
  1288. ret = cci_write(thp7312->regmap, THP7312_REG_FW_RESET_FLASH, 0x81, NULL);
  1289. if (ret) {
  1290. dev_err(dev, "Failed to reset flash memory: %d\n", ret);
  1291. return FW_UPLOAD_ERR_HW_ERROR;
  1292. }
  1293. return FW_UPLOAD_ERR_NONE;
  1294. }
  1295. /* TODO: Erase only the amount of blocks necessary */
  1296. static enum fw_upload_err thp7312_flash_erase(struct thp7312_device *thp7312)
  1297. {
  1298. struct device *dev = thp7312->dev;
  1299. u8 read_buf[1] = { 0 };
  1300. unsigned int i;
  1301. u8 block;
  1302. int ret;
  1303. for (block = 0; block < 3; block++) {
  1304. const u8 jedec_se[] = { SPINOR_OP_SE, block, 0x00, 0x00 };
  1305. ret = thp7312_flash_reg_write(thp7312, thp7312_jedec_wen);
  1306. if (ret < 0) {
  1307. dev_err(dev, "Failed to enable flash for writing\n");
  1308. return FW_UPLOAD_ERR_RW_ERROR;
  1309. }
  1310. ret = thp7312_flash_reg_write(thp7312, jedec_se);
  1311. if (ret < 0) {
  1312. dev_err(dev, "Failed to erase flash sector\n");
  1313. return FW_UPLOAD_ERR_RW_ERROR;
  1314. }
  1315. for (i = 0; i < THP7312_FLASH_MEMORY_ERASE_TIMEOUT; i++) {
  1316. usleep_range(100000, 101000);
  1317. thp7312_flash_reg_read(thp7312, thp7312_jedec_rdsr,
  1318. read_buf);
  1319. /* Check Busy bit. Busy == 0x0 means erase complete. */
  1320. if (!(read_buf[0] & SR_WIP))
  1321. break;
  1322. }
  1323. if (i == THP7312_FLASH_MEMORY_ERASE_TIMEOUT)
  1324. return FW_UPLOAD_ERR_TIMEOUT;
  1325. }
  1326. thp7312_flash_reg_read(thp7312, thp7312_jedec_rdsr, read_buf);
  1327. /* Check WEL bit. */
  1328. if (read_buf[0] & SR_WEL)
  1329. return FW_UPLOAD_ERR_HW_ERROR;
  1330. return FW_UPLOAD_ERR_NONE;
  1331. }
  1332. static enum fw_upload_err
  1333. thp7312_write_download_data_by_unit(struct thp7312_device *thp7312,
  1334. unsigned int addr, const u8 *data,
  1335. unsigned int size)
  1336. {
  1337. struct device *dev = thp7312->dev;
  1338. u8 *write_buf = thp7312->fw_write_buf;
  1339. int ret;
  1340. dev_dbg(dev, "%s: addr = 0x%04x, data = 0x%p, size = %u\n",
  1341. __func__, addr, data, size);
  1342. write_buf[0] = (addr >> 8) & 0xff;
  1343. write_buf[1] = (addr >> 0) & 0xff;
  1344. memcpy(&write_buf[2], data, size);
  1345. /*
  1346. * THP7312 Firmware download to RAM
  1347. * Command ID (address to download): 0x0000 - 0x7fff
  1348. * Format:: 0000 XX XX XX ........ XX
  1349. */
  1350. ret = thp7312_write_buf(thp7312, write_buf, size + 2);
  1351. if (ret < 0)
  1352. dev_err(dev, "Unit transfer ERROR %s(): ret = %d\n", __func__, ret);
  1353. return ret >= 0 ? FW_UPLOAD_ERR_NONE : FW_UPLOAD_ERR_RW_ERROR;
  1354. }
  1355. static enum fw_upload_err thp7312_fw_load_to_ram(struct thp7312_device *thp7312,
  1356. const u8 *data, u32 size)
  1357. {
  1358. struct device *dev = thp7312->dev;
  1359. enum fw_upload_err ret;
  1360. unsigned int num_banks;
  1361. unsigned int i, j;
  1362. num_banks = DIV_ROUND_UP(size, THP7312_RAM_BANK_SIZE);
  1363. dev_dbg(dev, "%s: loading %u bytes in SRAM (%u banks)\n", __func__,
  1364. size, num_banks);
  1365. for (i = 0; i < num_banks; i++) {
  1366. const u32 bank_addr = 0x10000000 | (i * THP7312_RAM_BANK_SIZE);
  1367. unsigned int bank_size;
  1368. unsigned int num_chunks;
  1369. ret = cci_write(thp7312->regmap, THP7312_REG_FW_DEST_BANK_ADDR,
  1370. bank_addr, NULL);
  1371. if (ret)
  1372. return FW_UPLOAD_ERR_HW_ERROR;
  1373. bank_size = min_t(u32, size, THP7312_RAM_BANK_SIZE);
  1374. num_chunks = DIV_ROUND_UP(bank_size, THP7312_FW_DOWNLOAD_UNIT);
  1375. dev_dbg(dev, "%s: loading %u bytes in SRAM bank %u (%u chunks)\n",
  1376. __func__, bank_size, i, num_chunks);
  1377. for (j = 0 ; j < num_chunks; j++) {
  1378. unsigned int chunk_addr;
  1379. unsigned int chunk_size;
  1380. chunk_addr = j * THP7312_FW_DOWNLOAD_UNIT;
  1381. chunk_size = min_t(u32, size, THP7312_FW_DOWNLOAD_UNIT);
  1382. ret = thp7312_write_download_data_by_unit(thp7312, chunk_addr,
  1383. data, chunk_size);
  1384. if (ret != FW_UPLOAD_ERR_NONE) {
  1385. dev_err(dev, "Unit transfer ERROR at bank transfer %s(): %d\n",
  1386. __func__, j);
  1387. return ret;
  1388. }
  1389. data += chunk_size;
  1390. size -= chunk_size;
  1391. }
  1392. }
  1393. return FW_UPLOAD_ERR_NONE;
  1394. }
  1395. static enum fw_upload_err thp7312_fw_write_to_flash(struct thp7312_device *thp7312,
  1396. u32 dest, u32 write_size)
  1397. {
  1398. u8 command[sizeof(thp7312_cmd_write_ram_to_flash) + 6];
  1399. static const u32 cmd_size = sizeof(thp7312_cmd_write_ram_to_flash);
  1400. u64 val;
  1401. int ret;
  1402. memcpy(command, thp7312_cmd_write_ram_to_flash, cmd_size);
  1403. command[cmd_size] = (dest & 0xff0000) >> 16;
  1404. command[cmd_size + 1] = (dest & 0x00ff00) >> 8;
  1405. command[cmd_size + 2] = (dest & 0x0000ff);
  1406. command[cmd_size + 3] = ((write_size - 1) & 0xff0000) >> 16;
  1407. command[cmd_size + 4] = ((write_size - 1) & 0x00ff00) >> 8;
  1408. command[cmd_size + 5] = ((write_size - 1) & 0x0000ff);
  1409. ret = thp7312_write_buf(thp7312, command, sizeof(command));
  1410. if (ret < 0)
  1411. return FW_UPLOAD_ERR_RW_ERROR;
  1412. usleep_range(8000000, 8100000);
  1413. ret = cci_read(thp7312->regmap, THP7312_REG_FW_VERIFY_RESULT, &val,
  1414. NULL);
  1415. if (ret < 0)
  1416. return FW_UPLOAD_ERR_RW_ERROR;
  1417. return val ? FW_UPLOAD_ERR_HW_ERROR : FW_UPLOAD_ERR_NONE;
  1418. }
  1419. static enum fw_upload_err thp7312_fw_check_crc(struct thp7312_device *thp7312,
  1420. const u8 *fw_data, u32 fw_size)
  1421. {
  1422. struct device *dev = thp7312->dev;
  1423. u16 header_size = fw_size - THP7312_FW_RAM_SIZE;
  1424. u8 command[sizeof(thp7312_cmd_calc_crc) + 6];
  1425. static const u32 cmd_size = sizeof(thp7312_cmd_calc_crc);
  1426. u32 size = THP7312_FW_RAM_SIZE - 4;
  1427. u32 fw_crc;
  1428. u64 crc;
  1429. int ret;
  1430. memcpy(command, thp7312_cmd_calc_crc, cmd_size);
  1431. command[cmd_size] = 0;
  1432. command[cmd_size + 1] = (header_size >> 8) & 0xff;
  1433. command[cmd_size + 2] = header_size & 0xff;
  1434. command[cmd_size + 3] = (size >> 16) & 0xff;
  1435. command[cmd_size + 4] = (size >> 8) & 0xff;
  1436. command[cmd_size + 5] = size & 0xff;
  1437. ret = thp7312_write_buf(thp7312, command, sizeof(command));
  1438. if (ret < 0)
  1439. return FW_UPLOAD_ERR_RW_ERROR;
  1440. usleep_range(2000000, 2100000);
  1441. fw_crc = get_unaligned_be32(&fw_data[fw_size - 4]);
  1442. ret = cci_read(thp7312->regmap, THP7312_REG_FW_CRC_RESULT, &crc, NULL);
  1443. if (ret < 0)
  1444. return FW_UPLOAD_ERR_RW_ERROR;
  1445. if (fw_crc != crc) {
  1446. dev_err(dev, "CRC mismatch: firmware 0x%08x, flash 0x%08llx\n",
  1447. fw_crc, crc);
  1448. return FW_UPLOAD_ERR_HW_ERROR;
  1449. }
  1450. return FW_UPLOAD_ERR_NONE;
  1451. }
  1452. static enum fw_upload_err thp7312_fw_prepare(struct fw_upload *fw_upload,
  1453. const u8 *data, u32 size)
  1454. {
  1455. struct thp7312_device *thp7312 = fw_upload->dd_handle;
  1456. struct device *dev = thp7312->dev;
  1457. enum fw_upload_err ret;
  1458. mutex_lock(&thp7312->fw_lock);
  1459. thp7312->fw_cancel = false;
  1460. mutex_unlock(&thp7312->fw_lock);
  1461. if (size < THP7312_FW_MIN_SIZE || size > THP7312_FW_MAX_SIZE) {
  1462. dev_err(dev, "%s: Invalid firmware size %d; must be between %d and %d\n",
  1463. __func__, size, THP7312_FW_MIN_SIZE, THP7312_FW_MAX_SIZE);
  1464. return FW_UPLOAD_ERR_INVALID_SIZE;
  1465. }
  1466. ret = thp7312_fw_prepare_config(thp7312);
  1467. if (ret != FW_UPLOAD_ERR_NONE)
  1468. return ret;
  1469. ret = thp7312_fw_prepare_check(thp7312);
  1470. if (ret != FW_UPLOAD_ERR_NONE)
  1471. return ret;
  1472. ret = thp7312_fw_prepare_reset(thp7312);
  1473. if (ret != FW_UPLOAD_ERR_NONE)
  1474. return ret;
  1475. mutex_lock(&thp7312->fw_lock);
  1476. ret = thp7312->fw_cancel ? FW_UPLOAD_ERR_CANCELED : FW_UPLOAD_ERR_NONE;
  1477. mutex_unlock(&thp7312->fw_lock);
  1478. return ret;
  1479. }
  1480. static enum fw_upload_err thp7312_fw_write(struct fw_upload *fw_upload,
  1481. const u8 *data, u32 offset,
  1482. u32 size, u32 *written)
  1483. {
  1484. struct thp7312_device *thp7312 = fw_upload->dd_handle;
  1485. struct device *dev = thp7312->dev;
  1486. u16 header_size = size - THP7312_FW_RAM_SIZE;
  1487. enum fw_upload_err ret;
  1488. bool cancel;
  1489. mutex_lock(&thp7312->fw_lock);
  1490. cancel = thp7312->fw_cancel;
  1491. mutex_unlock(&thp7312->fw_lock);
  1492. if (cancel)
  1493. return FW_UPLOAD_ERR_CANCELED;
  1494. ret = thp7312_flash_erase(thp7312);
  1495. if (ret != FW_UPLOAD_ERR_NONE)
  1496. return ret;
  1497. ret = thp7312_fw_load_to_ram(thp7312, data, THP7312_FW_RAM_SIZE);
  1498. if (ret != FW_UPLOAD_ERR_NONE)
  1499. return ret;
  1500. ret = thp7312_fw_write_to_flash(thp7312, 0, 0x1ffff);
  1501. if (ret != FW_UPLOAD_ERR_NONE)
  1502. return ret;
  1503. ret = thp7312_fw_load_to_ram(thp7312, data + THP7312_FW_RAM_SIZE, header_size);
  1504. if (ret != FW_UPLOAD_ERR_NONE)
  1505. return ret;
  1506. ret = thp7312_fw_write_to_flash(thp7312, 0x20000, header_size - 1);
  1507. if (ret != FW_UPLOAD_ERR_NONE)
  1508. return ret;
  1509. ret = thp7312_fw_check_crc(thp7312, data, size);
  1510. if (ret != FW_UPLOAD_ERR_NONE)
  1511. return ret;
  1512. dev_info(dev, "Successfully wrote firmware\n");
  1513. *written = size;
  1514. return FW_UPLOAD_ERR_NONE;
  1515. }
  1516. static enum fw_upload_err thp7312_fw_poll_complete(struct fw_upload *fw_upload)
  1517. {
  1518. return FW_UPLOAD_ERR_NONE;
  1519. }
  1520. /*
  1521. * This may be called asynchronously with an on-going update. All other
  1522. * functions are called sequentially in a single thread. To avoid contention on
  1523. * register accesses, only update the cancel_request flag. Other functions will
  1524. * check this flag and handle the cancel request synchronously.
  1525. */
  1526. static void thp7312_fw_cancel(struct fw_upload *fw_upload)
  1527. {
  1528. struct thp7312_device *thp7312 = fw_upload->dd_handle;
  1529. mutex_lock(&thp7312->fw_lock);
  1530. thp7312->fw_cancel = true;
  1531. mutex_unlock(&thp7312->fw_lock);
  1532. }
  1533. static const struct fw_upload_ops thp7312_fw_upload_ops = {
  1534. .prepare = thp7312_fw_prepare,
  1535. .write = thp7312_fw_write,
  1536. .poll_complete = thp7312_fw_poll_complete,
  1537. .cancel = thp7312_fw_cancel,
  1538. };
  1539. static int thp7312_register_flash_mode(struct thp7312_device *thp7312)
  1540. {
  1541. struct device *dev = thp7312->dev;
  1542. struct fw_upload *fwl;
  1543. u64 val;
  1544. int ret;
  1545. dev_info(dev, "booted in flash mode\n");
  1546. mutex_init(&thp7312->fw_lock);
  1547. thp7312->fw_write_buf = devm_kzalloc(dev, THP7312_FW_DOWNLOAD_UNIT + 2,
  1548. GFP_KERNEL);
  1549. if (!thp7312->fw_write_buf)
  1550. return -ENOMEM;
  1551. ret = __thp7312_power_on(thp7312);
  1552. if (ret < 0)
  1553. return dev_err_probe(dev, ret, "Failed to power on\n");
  1554. ret = cci_read(thp7312->regmap, THP7312_REG_FW_STATUS, &val, NULL);
  1555. if (ret) {
  1556. dev_err_probe(dev, ret, "Camera status read failed\n");
  1557. goto error;
  1558. }
  1559. fwl = firmware_upload_register(THIS_MODULE, dev, "thp7312-firmware",
  1560. &thp7312_fw_upload_ops, thp7312);
  1561. if (IS_ERR(fwl)) {
  1562. ret = PTR_ERR(fwl);
  1563. dev_err_probe(dev, ret, "Failed to register firmware upload\n");
  1564. goto error;
  1565. }
  1566. thp7312->fwl = fwl;
  1567. return 0;
  1568. error:
  1569. __thp7312_power_off(thp7312);
  1570. return ret;
  1571. }
  1572. /* -----------------------------------------------------------------------------
  1573. * Probe & Remove
  1574. */
  1575. static int thp7312_get_regulators(struct thp7312_device *thp7312)
  1576. {
  1577. unsigned int i;
  1578. for (i = 0; i < ARRAY_SIZE(thp7312->supplies); i++)
  1579. thp7312->supplies[i].supply = thp7312_supply_name[i];
  1580. return devm_regulator_bulk_get(thp7312->dev,
  1581. ARRAY_SIZE(thp7312->supplies),
  1582. thp7312->supplies);
  1583. }
  1584. static int thp7312_sensor_parse_dt(struct thp7312_device *thp7312,
  1585. struct fwnode_handle *node)
  1586. {
  1587. struct device *dev = thp7312->dev;
  1588. struct thp7312_sensor *sensor;
  1589. const char *model;
  1590. u8 data_lanes[4];
  1591. u32 values[4];
  1592. unsigned int i;
  1593. u32 reg;
  1594. int ret;
  1595. /* Retrieve the sensor index from the reg property. */
  1596. ret = fwnode_property_read_u32(node, "reg", &reg);
  1597. if (ret < 0) {
  1598. dev_err(dev, "'reg' property missing in sensor node\n");
  1599. return -EINVAL;
  1600. }
  1601. if (reg >= ARRAY_SIZE(thp7312->sensors)) {
  1602. dev_err(dev, "Out-of-bounds 'reg' value %u\n", reg);
  1603. return -EINVAL;
  1604. }
  1605. sensor = &thp7312->sensors[reg];
  1606. if (sensor->info) {
  1607. dev_err(dev, "Duplicate entry for sensor %u\n", reg);
  1608. return -EINVAL;
  1609. }
  1610. ret = fwnode_property_read_string(node, "thine,model", &model);
  1611. if (ret < 0) {
  1612. dev_err(dev, "'thine,model' property missing in sensor node\n");
  1613. return -EINVAL;
  1614. }
  1615. for (i = 0; i < ARRAY_SIZE(thp7312_sensor_info); i++) {
  1616. const struct thp7312_sensor_info *info =
  1617. &thp7312_sensor_info[i];
  1618. if (!strcmp(info->model, model)) {
  1619. sensor->info = info;
  1620. break;
  1621. }
  1622. }
  1623. if (!sensor->info) {
  1624. dev_err(dev, "Unsupported sensor model %s\n", model);
  1625. return -EINVAL;
  1626. }
  1627. ret = fwnode_property_read_u32_array(node, "data-lanes", values,
  1628. ARRAY_SIZE(values));
  1629. if (ret < 0) {
  1630. dev_err(dev, "Failed to read property data-lanes: %d\n", ret);
  1631. return ret;
  1632. }
  1633. for (i = 0; i < ARRAY_SIZE(data_lanes); ++i)
  1634. data_lanes[i] = values[i];
  1635. ret = thp7312_map_data_lanes(&sensor->lane_remap, data_lanes,
  1636. ARRAY_SIZE(data_lanes));
  1637. if (ret) {
  1638. dev_err(dev, "Invalid sensor@%u data-lanes value\n", reg);
  1639. return ret;
  1640. }
  1641. return 0;
  1642. }
  1643. static int thp7312_parse_dt(struct thp7312_device *thp7312)
  1644. {
  1645. struct v4l2_fwnode_endpoint ep = {
  1646. .bus_type = V4L2_MBUS_CSI2_DPHY,
  1647. };
  1648. struct device *dev = thp7312->dev;
  1649. struct fwnode_handle *endpoint;
  1650. struct fwnode_handle *sensors;
  1651. unsigned int num_sensors = 0;
  1652. struct fwnode_handle *node;
  1653. int ret;
  1654. endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(dev), NULL);
  1655. if (!endpoint)
  1656. return dev_err_probe(dev, -EINVAL, "Endpoint node not found\n");
  1657. ret = v4l2_fwnode_endpoint_parse(endpoint, &ep);
  1658. fwnode_handle_put(endpoint);
  1659. if (ret)
  1660. return dev_err_probe(dev, ret, "Could not parse endpoint\n");
  1661. ret = thp7312_map_data_lanes(&thp7312->lane_remap,
  1662. ep.bus.mipi_csi2.data_lanes,
  1663. ep.bus.mipi_csi2.num_data_lanes);
  1664. if (ret) {
  1665. dev_err(dev, "Invalid data-lanes value\n");
  1666. return ret;
  1667. }
  1668. /*
  1669. * The thine,boot-mode property is optional and default to
  1670. * THP7312_BOOT_MODE_SPI_MASTER (1).
  1671. */
  1672. thp7312->boot_mode = THP7312_BOOT_MODE_SPI_MASTER;
  1673. ret = device_property_read_u32(dev, "thine,boot-mode",
  1674. &thp7312->boot_mode);
  1675. if (ret && ret != -EINVAL)
  1676. return dev_err_probe(dev, ret, "Property '%s' is invalid\n",
  1677. "thine,boot-mode");
  1678. if (thp7312->boot_mode != THP7312_BOOT_MODE_2WIRE_SLAVE &&
  1679. thp7312->boot_mode != THP7312_BOOT_MODE_SPI_MASTER)
  1680. return dev_err_probe(dev, -EINVAL, "Invalid '%s' value %u\n",
  1681. "thine,boot-mode", thp7312->boot_mode);
  1682. /* Sensors */
  1683. sensors = device_get_named_child_node(dev, "sensors");
  1684. if (!sensors) {
  1685. dev_err(dev, "'sensors' child node not found\n");
  1686. return -EINVAL;
  1687. }
  1688. fwnode_for_each_available_child_node(sensors, node) {
  1689. if (fwnode_name_eq(node, "sensor")) {
  1690. if (!thp7312_sensor_parse_dt(thp7312, node))
  1691. num_sensors++;
  1692. }
  1693. }
  1694. fwnode_handle_put(sensors);
  1695. if (!num_sensors) {
  1696. dev_err(dev, "No sensor found\n");
  1697. return -EINVAL;
  1698. }
  1699. return 0;
  1700. }
  1701. static int thp7312_probe(struct i2c_client *client)
  1702. {
  1703. struct device *dev = &client->dev;
  1704. struct thp7312_device *thp7312;
  1705. int ret;
  1706. thp7312 = devm_kzalloc(dev, sizeof(*thp7312), GFP_KERNEL);
  1707. if (!thp7312)
  1708. return -ENOMEM;
  1709. thp7312->dev = dev;
  1710. thp7312->regmap = devm_cci_regmap_init_i2c(client, 16);
  1711. if (IS_ERR(thp7312->regmap))
  1712. return dev_err_probe(dev, PTR_ERR(thp7312->regmap),
  1713. "Unable to initialize I2C\n");
  1714. ret = thp7312_parse_dt(thp7312);
  1715. if (ret < 0)
  1716. return ret;
  1717. ret = thp7312_get_regulators(thp7312);
  1718. if (ret)
  1719. return dev_err_probe(dev, ret, "Failed to get regulators\n");
  1720. thp7312->iclk = devm_clk_get(dev, NULL);
  1721. if (IS_ERR(thp7312->iclk))
  1722. return dev_err_probe(dev, PTR_ERR(thp7312->iclk),
  1723. "Failed to get iclk\n");
  1724. thp7312->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
  1725. if (IS_ERR(thp7312->reset_gpio))
  1726. return dev_err_probe(dev, PTR_ERR(thp7312->reset_gpio),
  1727. "Failed to get reset gpio\n");
  1728. if (thp7312->boot_mode == THP7312_BOOT_MODE_2WIRE_SLAVE)
  1729. return thp7312_register_flash_mode(thp7312);
  1730. v4l2_i2c_subdev_init(&thp7312->sd, client, &thp7312_subdev_ops);
  1731. thp7312->sd.internal_ops = &thp7312_internal_ops;
  1732. thp7312->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS;
  1733. thp7312->pad.flags = MEDIA_PAD_FL_SOURCE;
  1734. thp7312->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
  1735. ret = media_entity_pads_init(&thp7312->sd.entity, 1, &thp7312->pad);
  1736. if (ret)
  1737. return ret;
  1738. /*
  1739. * Enable power management. The driver supports runtime PM, but needs to
  1740. * work when runtime PM is disabled in the kernel. To that end, power
  1741. * the device manually here.
  1742. */
  1743. ret = thp7312_power_on(thp7312);
  1744. if (ret)
  1745. goto err_entity_cleanup;
  1746. ret = thp7312_read_firmware_version(thp7312);
  1747. if (ret < 0) {
  1748. dev_err(dev, "Camera is not found\n");
  1749. goto err_power_off;
  1750. }
  1751. ret = thp7312_init_controls(thp7312);
  1752. if (ret) {
  1753. dev_err(dev, "Failed to initialize controls\n");
  1754. goto err_power_off;
  1755. }
  1756. thp7312->sd.ctrl_handler = &thp7312->ctrl_handler;
  1757. thp7312->sd.state_lock = thp7312->ctrl_handler.lock;
  1758. ret = v4l2_subdev_init_finalize(&thp7312->sd);
  1759. if (ret < 0) {
  1760. dev_err(dev, "Subdev active state initialization failed\n");
  1761. goto err_free_ctrls;
  1762. }
  1763. /*
  1764. * Enable runtime PM with autosuspend. As the device has been powered
  1765. * manually, mark it as active, and increase the usage count without
  1766. * resuming the device.
  1767. */
  1768. pm_runtime_set_active(dev);
  1769. pm_runtime_get_noresume(dev);
  1770. pm_runtime_enable(dev);
  1771. pm_runtime_set_autosuspend_delay(dev, 1000);
  1772. pm_runtime_use_autosuspend(dev);
  1773. ret = v4l2_async_register_subdev(&thp7312->sd);
  1774. if (ret < 0) {
  1775. dev_err(dev, "Subdev registration failed\n");
  1776. goto err_pm;
  1777. }
  1778. /*
  1779. * Decrease the PM usage count. The device will get suspended after the
  1780. * autosuspend delay, turning the power off.
  1781. */
  1782. pm_runtime_mark_last_busy(dev);
  1783. pm_runtime_put_autosuspend(dev);
  1784. dev_info(dev, "THP7312 firmware version %02u.%02u\n",
  1785. THP7312_FW_VERSION_MAJOR(thp7312->fw_version),
  1786. THP7312_FW_VERSION_MINOR(thp7312->fw_version));
  1787. return 0;
  1788. err_pm:
  1789. pm_runtime_disable(dev);
  1790. pm_runtime_put_noidle(dev);
  1791. v4l2_subdev_cleanup(&thp7312->sd);
  1792. err_free_ctrls:
  1793. v4l2_ctrl_handler_free(&thp7312->ctrl_handler);
  1794. err_power_off:
  1795. thp7312_power_off(thp7312);
  1796. err_entity_cleanup:
  1797. media_entity_cleanup(&thp7312->sd.entity);
  1798. return ret;
  1799. }
  1800. static void thp7312_remove(struct i2c_client *client)
  1801. {
  1802. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  1803. struct thp7312_device *thp7312 = to_thp7312_dev(sd);
  1804. if (thp7312->boot_mode == THP7312_BOOT_MODE_2WIRE_SLAVE) {
  1805. firmware_upload_unregister(thp7312->fwl);
  1806. __thp7312_power_off(thp7312);
  1807. return;
  1808. }
  1809. v4l2_async_unregister_subdev(&thp7312->sd);
  1810. v4l2_subdev_cleanup(&thp7312->sd);
  1811. media_entity_cleanup(&thp7312->sd.entity);
  1812. v4l2_ctrl_handler_free(&thp7312->ctrl_handler);
  1813. /*
  1814. * Disable runtime PM. In case runtime PM is disabled in the kernel,
  1815. * make sure to turn power off manually.
  1816. */
  1817. pm_runtime_disable(thp7312->dev);
  1818. if (!pm_runtime_status_suspended(thp7312->dev))
  1819. thp7312_power_off(thp7312);
  1820. pm_runtime_set_suspended(thp7312->dev);
  1821. }
  1822. static const struct of_device_id thp7312_dt_ids[] = {
  1823. { .compatible = "thine,thp7312" },
  1824. { /* sentinel */ }
  1825. };
  1826. MODULE_DEVICE_TABLE(of, thp7312_dt_ids);
  1827. static struct i2c_driver thp7312_i2c_driver = {
  1828. .driver = {
  1829. .name = "thp7312",
  1830. .pm = &thp7312_pm_ops,
  1831. .of_match_table = thp7312_dt_ids,
  1832. },
  1833. .probe = thp7312_probe,
  1834. .remove = thp7312_remove,
  1835. };
  1836. module_i2c_driver(thp7312_i2c_driver);
  1837. MODULE_DESCRIPTION("THP7312 MIPI Camera Subdev Driver");
  1838. MODULE_LICENSE("GPL");