thp7312.c 59 KB

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