gc2145.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * A V4L2 driver for Galaxycore GC2145 camera.
  4. * Copyright (C) 2023, STMicroelectronics SA
  5. *
  6. * Inspired by the imx219.c driver
  7. *
  8. * Datasheet v1.0 available at http://files.pine64.org/doc/datasheet/PinebookPro/GC2145%20CSP%20DataSheet%20release%20V1.0_20131201.pdf
  9. */
  10. #include <linux/clk.h>
  11. #include <linux/delay.h>
  12. #include <linux/gpio/consumer.h>
  13. #include <linux/i2c.h>
  14. #include <linux/module.h>
  15. #include <linux/pm_runtime.h>
  16. #include <linux/regulator/consumer.h>
  17. #include <linux/units.h>
  18. #include <media/mipi-csi2.h>
  19. #include <media/v4l2-cci.h>
  20. #include <media/v4l2-ctrls.h>
  21. #include <media/v4l2-device.h>
  22. #include <media/v4l2-fwnode.h>
  23. #include <media/v4l2-mediabus.h>
  24. /* Chip ID */
  25. #define GC2145_CHIP_ID 0x2145
  26. /* Page 0 */
  27. #define GC2145_REG_EXPOSURE CCI_REG16(0x03)
  28. #define GC2145_REG_HBLANK CCI_REG16(0x05)
  29. #define GC2145_REG_VBLANK CCI_REG16(0x07)
  30. #define GC2145_REG_ROW_START CCI_REG16(0x09)
  31. #define GC2145_REG_COL_START CCI_REG16(0x0b)
  32. #define GC2145_REG_WIN_HEIGHT CCI_REG16(0x0d)
  33. #define GC2145_REG_WIN_WIDTH CCI_REG16(0x0f)
  34. #define GC2145_REG_ANALOG_MODE1 CCI_REG8(0x17)
  35. #define GC2145_REG_OUTPUT_FMT CCI_REG8(0x84)
  36. #define GC2145_REG_SYNC_MODE CCI_REG8(0x86)
  37. #define GC2145_SYNC_MODE_COL_SWITCH BIT(4)
  38. #define GC2145_SYNC_MODE_ROW_SWITCH BIT(5)
  39. #define GC2145_REG_BYPASS_MODE CCI_REG8(0x89)
  40. #define GC2145_BYPASS_MODE_SWITCH BIT(5)
  41. #define GC2145_REG_DEBUG_MODE2 CCI_REG8(0x8c)
  42. #define GC2145_REG_DEBUG_MODE3 CCI_REG8(0x8d)
  43. #define GC2145_REG_CROP_ENABLE CCI_REG8(0x90)
  44. #define GC2145_REG_CROP_Y CCI_REG16(0x91)
  45. #define GC2145_REG_CROP_X CCI_REG16(0x93)
  46. #define GC2145_REG_CROP_HEIGHT CCI_REG16(0x95)
  47. #define GC2145_REG_CROP_WIDTH CCI_REG16(0x97)
  48. #define GC2145_REG_GLOBAL_GAIN CCI_REG8(0xb0)
  49. #define GC2145_REG_CHIP_ID CCI_REG16(0xf0)
  50. #define GC2145_REG_PAD_IO CCI_REG8(0xf2)
  51. #define GC2145_REG_PAGE_SELECT CCI_REG8(0xfe)
  52. /* Page 3 */
  53. #define GC2145_REG_DPHY_ANALOG_MODE1 CCI_REG8(0x01)
  54. #define GC2145_DPHY_MODE_PHY_CLK_EN BIT(0)
  55. #define GC2145_DPHY_MODE_PHY_LANE0_EN BIT(1)
  56. #define GC2145_DPHY_MODE_PHY_LANE1_EN BIT(2)
  57. #define GC2145_DPHY_MODE_PHY_CLK_LANE_P2S_SEL BIT(7)
  58. #define GC2145_REG_DPHY_ANALOG_MODE2 CCI_REG8(0x02)
  59. #define GC2145_DPHY_CLK_DIFF(a) ((a) & 0x07)
  60. #define GC2145_DPHY_LANE0_DIFF(a) (((a) & 0x07) << 4)
  61. #define GC2145_REG_DPHY_ANALOG_MODE3 CCI_REG8(0x03)
  62. #define GC2145_DPHY_LANE1_DIFF(a) ((a) & 0x07)
  63. #define GC2145_DPHY_CLK_DELAY BIT(4)
  64. #define GC2145_DPHY_LANE0_DELAY BIT(5)
  65. #define GC2145_DPHY_LANE1_DELAY BIT(6)
  66. #define GC2145_REG_FIFO_FULL_LVL CCI_REG16_LE(0x04)
  67. #define GC2145_REG_FIFO_MODE CCI_REG8(0x06)
  68. #define GC2145_FIFO_MODE_READ_GATE BIT(3)
  69. #define GC2145_FIFO_MODE_MIPI_CLK_MODULE BIT(7)
  70. #define GC2145_REG_BUF_CSI2_MODE CCI_REG8(0x10)
  71. #define GC2145_CSI2_MODE_DOUBLE BIT(0)
  72. #define GC2145_CSI2_MODE_RAW8 BIT(2)
  73. #define GC2145_CSI2_MODE_MIPI_EN BIT(4)
  74. #define GC2145_CSI2_MODE_EN BIT(7)
  75. #define GC2145_REG_MIPI_DT CCI_REG8(0x11)
  76. #define GC2145_REG_LWC CCI_REG16_LE(0x12)
  77. #define GC2145_REG_DPHY_MODE CCI_REG8(0x15)
  78. #define GC2145_DPHY_MODE_TRIGGER_PROG BIT(4)
  79. #define GC2145_REG_FIFO_GATE_MODE CCI_REG8(0x17)
  80. #define GC2145_REG_T_LPX CCI_REG8(0x21)
  81. #define GC2145_REG_T_CLK_HS_PREPARE CCI_REG8(0x22)
  82. #define GC2145_REG_T_CLK_ZERO CCI_REG8(0x23)
  83. #define GC2145_REG_T_CLK_PRE CCI_REG8(0x24)
  84. #define GC2145_REG_T_CLK_POST CCI_REG8(0x25)
  85. #define GC2145_REG_T_CLK_TRAIL CCI_REG8(0x26)
  86. #define GC2145_REG_T_HS_EXIT CCI_REG8(0x27)
  87. #define GC2145_REG_T_WAKEUP CCI_REG8(0x28)
  88. #define GC2145_REG_T_HS_PREPARE CCI_REG8(0x29)
  89. #define GC2145_REG_T_HS_ZERO CCI_REG8(0x2a)
  90. #define GC2145_REG_T_HS_TRAIL CCI_REG8(0x2b)
  91. /* External clock frequency is 24.0MHz */
  92. #define GC2145_XCLK_FREQ (24 * HZ_PER_MHZ)
  93. #define GC2145_NATIVE_WIDTH 1616U
  94. #define GC2145_NATIVE_HEIGHT 1232U
  95. /**
  96. * struct gc2145_mode - GC2145 mode description
  97. * @width: frame width (in pixels)
  98. * @height: frame height (in pixels)
  99. * @reg_seq: registers config sequence to enter into the mode
  100. * @reg_seq_size: size of the sequence
  101. * @pixel_rate: pixel rate associated with the mode
  102. * @crop: window area captured
  103. * @hblank: default horizontal blanking
  104. * @vblank: default vertical blanking
  105. * @link_freq_index: index within the link frequency menu
  106. */
  107. struct gc2145_mode {
  108. unsigned int width;
  109. unsigned int height;
  110. const struct cci_reg_sequence *reg_seq;
  111. size_t reg_seq_size;
  112. unsigned long pixel_rate;
  113. struct v4l2_rect crop;
  114. unsigned int hblank;
  115. unsigned int vblank;
  116. unsigned int link_freq_index;
  117. };
  118. #define GC2145_DEFAULT_EXPOSURE 0x04e2
  119. #define GC2145_DEFAULT_GLOBAL_GAIN 0x55
  120. static const struct cci_reg_sequence gc2145_common_regs[] = {
  121. {GC2145_REG_PAGE_SELECT, 0x00},
  122. /* SH Delay */
  123. {CCI_REG8(0x12), 0x2e},
  124. /* Flip */
  125. {GC2145_REG_ANALOG_MODE1, 0x14},
  126. /* Analog Conf */
  127. {CCI_REG8(0x18), 0x22}, {CCI_REG8(0x19), 0x0e}, {CCI_REG8(0x1a), 0x01},
  128. {CCI_REG8(0x1b), 0x4b}, {CCI_REG8(0x1c), 0x07}, {CCI_REG8(0x1d), 0x10},
  129. {CCI_REG8(0x1e), 0x88}, {CCI_REG8(0x1f), 0x78}, {CCI_REG8(0x20), 0x03},
  130. {CCI_REG8(0x21), 0x40}, {CCI_REG8(0x22), 0xa0}, {CCI_REG8(0x24), 0x16},
  131. {CCI_REG8(0x25), 0x01}, {CCI_REG8(0x26), 0x10}, {CCI_REG8(0x2d), 0x60},
  132. {CCI_REG8(0x30), 0x01}, {CCI_REG8(0x31), 0x90}, {CCI_REG8(0x33), 0x06},
  133. {CCI_REG8(0x34), 0x01},
  134. /* ISP related */
  135. {CCI_REG8(0x80), 0x7f}, {CCI_REG8(0x81), 0x26}, {CCI_REG8(0x82), 0xfa},
  136. {CCI_REG8(0x83), 0x00}, {CCI_REG8(0x84), 0x02}, {CCI_REG8(0x86), 0x02},
  137. {CCI_REG8(0x88), 0x03},
  138. {GC2145_REG_BYPASS_MODE, 0x03},
  139. {CCI_REG8(0x85), 0x08}, {CCI_REG8(0x8a), 0x00}, {CCI_REG8(0x8b), 0x00},
  140. {GC2145_REG_GLOBAL_GAIN, GC2145_DEFAULT_GLOBAL_GAIN},
  141. {CCI_REG8(0xc3), 0x00}, {CCI_REG8(0xc4), 0x80}, {CCI_REG8(0xc5), 0x90},
  142. {CCI_REG8(0xc6), 0x3b}, {CCI_REG8(0xc7), 0x46},
  143. /* BLK */
  144. {GC2145_REG_PAGE_SELECT, 0x00},
  145. {CCI_REG8(0x40), 0x42}, {CCI_REG8(0x41), 0x00}, {CCI_REG8(0x43), 0x5b},
  146. {CCI_REG8(0x5e), 0x00}, {CCI_REG8(0x5f), 0x00}, {CCI_REG8(0x60), 0x00},
  147. {CCI_REG8(0x61), 0x00}, {CCI_REG8(0x62), 0x00}, {CCI_REG8(0x63), 0x00},
  148. {CCI_REG8(0x64), 0x00}, {CCI_REG8(0x65), 0x00}, {CCI_REG8(0x66), 0x20},
  149. {CCI_REG8(0x67), 0x20}, {CCI_REG8(0x68), 0x20}, {CCI_REG8(0x69), 0x20},
  150. {CCI_REG8(0x76), 0x00}, {CCI_REG8(0x6a), 0x08}, {CCI_REG8(0x6b), 0x08},
  151. {CCI_REG8(0x6c), 0x08}, {CCI_REG8(0x6d), 0x08}, {CCI_REG8(0x6e), 0x08},
  152. {CCI_REG8(0x6f), 0x08}, {CCI_REG8(0x70), 0x08}, {CCI_REG8(0x71), 0x08},
  153. {CCI_REG8(0x76), 0x00}, {CCI_REG8(0x72), 0xf0}, {CCI_REG8(0x7e), 0x3c},
  154. {CCI_REG8(0x7f), 0x00},
  155. {GC2145_REG_PAGE_SELECT, 0x02},
  156. {CCI_REG8(0x48), 0x15}, {CCI_REG8(0x49), 0x00}, {CCI_REG8(0x4b), 0x0b},
  157. /* AEC */
  158. {GC2145_REG_PAGE_SELECT, 0x00},
  159. {GC2145_REG_EXPOSURE, GC2145_DEFAULT_EXPOSURE},
  160. {GC2145_REG_PAGE_SELECT, 0x01},
  161. {CCI_REG8(0x01), 0x04}, {CCI_REG8(0x02), 0xc0}, {CCI_REG8(0x03), 0x04},
  162. {CCI_REG8(0x04), 0x90}, {CCI_REG8(0x05), 0x30}, {CCI_REG8(0x06), 0x90},
  163. {CCI_REG8(0x07), 0x30}, {CCI_REG8(0x08), 0x80}, {CCI_REG8(0x09), 0x00},
  164. {CCI_REG8(0x0a), 0x82}, {CCI_REG8(0x0b), 0x11}, {CCI_REG8(0x0c), 0x10},
  165. {CCI_REG8(0x11), 0x10}, {CCI_REG8(0x13), 0x7b}, {CCI_REG8(0x17), 0x00},
  166. {CCI_REG8(0x1c), 0x11}, {CCI_REG8(0x1e), 0x61}, {CCI_REG8(0x1f), 0x35},
  167. {CCI_REG8(0x20), 0x40}, {CCI_REG8(0x22), 0x40}, {CCI_REG8(0x23), 0x20},
  168. {GC2145_REG_PAGE_SELECT, 0x02},
  169. {CCI_REG8(0x0f), 0x04},
  170. {GC2145_REG_PAGE_SELECT, 0x01},
  171. {CCI_REG8(0x12), 0x35}, {CCI_REG8(0x15), 0xb0}, {CCI_REG8(0x10), 0x31},
  172. {CCI_REG8(0x3e), 0x28}, {CCI_REG8(0x3f), 0xb0}, {CCI_REG8(0x40), 0x90},
  173. {CCI_REG8(0x41), 0x0f},
  174. /* INTPEE */
  175. {GC2145_REG_PAGE_SELECT, 0x02},
  176. {CCI_REG8(0x90), 0x6c}, {CCI_REG8(0x91), 0x03}, {CCI_REG8(0x92), 0xcb},
  177. {CCI_REG8(0x94), 0x33}, {CCI_REG8(0x95), 0x84}, {CCI_REG8(0x97), 0x65},
  178. {CCI_REG8(0xa2), 0x11},
  179. /* DNDD */
  180. {GC2145_REG_PAGE_SELECT, 0x02},
  181. {CCI_REG8(0x80), 0xc1}, {CCI_REG8(0x81), 0x08}, {CCI_REG8(0x82), 0x05},
  182. {CCI_REG8(0x83), 0x08}, {CCI_REG8(0x84), 0x0a}, {CCI_REG8(0x86), 0xf0},
  183. {CCI_REG8(0x87), 0x50}, {CCI_REG8(0x88), 0x15}, {CCI_REG8(0x89), 0xb0},
  184. {CCI_REG8(0x8a), 0x30}, {CCI_REG8(0x8b), 0x10},
  185. /* ASDE */
  186. {GC2145_REG_PAGE_SELECT, 0x01},
  187. {CCI_REG8(0x21), 0x04},
  188. {GC2145_REG_PAGE_SELECT, 0x02},
  189. {CCI_REG8(0xa3), 0x50}, {CCI_REG8(0xa4), 0x20}, {CCI_REG8(0xa5), 0x40},
  190. {CCI_REG8(0xa6), 0x80}, {CCI_REG8(0xab), 0x40}, {CCI_REG8(0xae), 0x0c},
  191. {CCI_REG8(0xb3), 0x46}, {CCI_REG8(0xb4), 0x64}, {CCI_REG8(0xb6), 0x38},
  192. {CCI_REG8(0xb7), 0x01}, {CCI_REG8(0xb9), 0x2b}, {CCI_REG8(0x3c), 0x04},
  193. {CCI_REG8(0x3d), 0x15}, {CCI_REG8(0x4b), 0x06}, {CCI_REG8(0x4c), 0x20},
  194. /* Gamma */
  195. {GC2145_REG_PAGE_SELECT, 0x02},
  196. {CCI_REG8(0x10), 0x09}, {CCI_REG8(0x11), 0x0d}, {CCI_REG8(0x12), 0x13},
  197. {CCI_REG8(0x13), 0x19}, {CCI_REG8(0x14), 0x27}, {CCI_REG8(0x15), 0x37},
  198. {CCI_REG8(0x16), 0x45}, {CCI_REG8(0x17), 0x53}, {CCI_REG8(0x18), 0x69},
  199. {CCI_REG8(0x19), 0x7d}, {CCI_REG8(0x1a), 0x8f}, {CCI_REG8(0x1b), 0x9d},
  200. {CCI_REG8(0x1c), 0xa9}, {CCI_REG8(0x1d), 0xbd}, {CCI_REG8(0x1e), 0xcd},
  201. {CCI_REG8(0x1f), 0xd9}, {CCI_REG8(0x20), 0xe3}, {CCI_REG8(0x21), 0xea},
  202. {CCI_REG8(0x22), 0xef}, {CCI_REG8(0x23), 0xf5}, {CCI_REG8(0x24), 0xf9},
  203. {CCI_REG8(0x25), 0xff},
  204. {GC2145_REG_PAGE_SELECT, 0x00},
  205. {CCI_REG8(0xc6), 0x20}, {CCI_REG8(0xc7), 0x2b},
  206. /* Gamma 2 */
  207. {GC2145_REG_PAGE_SELECT, 0x02},
  208. {CCI_REG8(0x26), 0x0f}, {CCI_REG8(0x27), 0x14}, {CCI_REG8(0x28), 0x19},
  209. {CCI_REG8(0x29), 0x1e}, {CCI_REG8(0x2a), 0x27}, {CCI_REG8(0x2b), 0x33},
  210. {CCI_REG8(0x2c), 0x3b}, {CCI_REG8(0x2d), 0x45}, {CCI_REG8(0x2e), 0x59},
  211. {CCI_REG8(0x2f), 0x69}, {CCI_REG8(0x30), 0x7c}, {CCI_REG8(0x31), 0x89},
  212. {CCI_REG8(0x32), 0x98}, {CCI_REG8(0x33), 0xae}, {CCI_REG8(0x34), 0xc0},
  213. {CCI_REG8(0x35), 0xcf}, {CCI_REG8(0x36), 0xda}, {CCI_REG8(0x37), 0xe2},
  214. {CCI_REG8(0x38), 0xe9}, {CCI_REG8(0x39), 0xf3}, {CCI_REG8(0x3a), 0xf9},
  215. {CCI_REG8(0x3b), 0xff},
  216. /* YCP */
  217. {GC2145_REG_PAGE_SELECT, 0x02},
  218. {CCI_REG8(0xd1), 0x32}, {CCI_REG8(0xd2), 0x32}, {CCI_REG8(0xd3), 0x40},
  219. {CCI_REG8(0xd6), 0xf0}, {CCI_REG8(0xd7), 0x10}, {CCI_REG8(0xd8), 0xda},
  220. {CCI_REG8(0xdd), 0x14}, {CCI_REG8(0xde), 0x86}, {CCI_REG8(0xed), 0x80},
  221. {CCI_REG8(0xee), 0x00}, {CCI_REG8(0xef), 0x3f}, {CCI_REG8(0xd8), 0xd8},
  222. /* ABS */
  223. {GC2145_REG_PAGE_SELECT, 0x01},
  224. {CCI_REG8(0x9f), 0x40},
  225. /* LSC */
  226. {GC2145_REG_PAGE_SELECT, 0x01},
  227. {CCI_REG8(0xc2), 0x14}, {CCI_REG8(0xc3), 0x0d}, {CCI_REG8(0xc4), 0x0c},
  228. {CCI_REG8(0xc8), 0x15}, {CCI_REG8(0xc9), 0x0d}, {CCI_REG8(0xca), 0x0a},
  229. {CCI_REG8(0xbc), 0x24}, {CCI_REG8(0xbd), 0x10}, {CCI_REG8(0xbe), 0x0b},
  230. {CCI_REG8(0xb6), 0x25}, {CCI_REG8(0xb7), 0x16}, {CCI_REG8(0xb8), 0x15},
  231. {CCI_REG8(0xc5), 0x00}, {CCI_REG8(0xc6), 0x00}, {CCI_REG8(0xc7), 0x00},
  232. {CCI_REG8(0xcb), 0x00}, {CCI_REG8(0xcc), 0x00}, {CCI_REG8(0xcd), 0x00},
  233. {CCI_REG8(0xbf), 0x07}, {CCI_REG8(0xc0), 0x00}, {CCI_REG8(0xc1), 0x00},
  234. {CCI_REG8(0xb9), 0x00}, {CCI_REG8(0xba), 0x00}, {CCI_REG8(0xbb), 0x00},
  235. {CCI_REG8(0xaa), 0x01}, {CCI_REG8(0xab), 0x01}, {CCI_REG8(0xac), 0x00},
  236. {CCI_REG8(0xad), 0x05}, {CCI_REG8(0xae), 0x06}, {CCI_REG8(0xaf), 0x0e},
  237. {CCI_REG8(0xb0), 0x0b}, {CCI_REG8(0xb1), 0x07}, {CCI_REG8(0xb2), 0x06},
  238. {CCI_REG8(0xb3), 0x17}, {CCI_REG8(0xb4), 0x0e}, {CCI_REG8(0xb5), 0x0e},
  239. {CCI_REG8(0xd0), 0x09}, {CCI_REG8(0xd1), 0x00}, {CCI_REG8(0xd2), 0x00},
  240. {CCI_REG8(0xd6), 0x08}, {CCI_REG8(0xd7), 0x00}, {CCI_REG8(0xd8), 0x00},
  241. {CCI_REG8(0xd9), 0x00}, {CCI_REG8(0xda), 0x00}, {CCI_REG8(0xdb), 0x00},
  242. {CCI_REG8(0xd3), 0x0a}, {CCI_REG8(0xd4), 0x00}, {CCI_REG8(0xd5), 0x00},
  243. {CCI_REG8(0xa4), 0x00}, {CCI_REG8(0xa5), 0x00}, {CCI_REG8(0xa6), 0x77},
  244. {CCI_REG8(0xa7), 0x77}, {CCI_REG8(0xa8), 0x77}, {CCI_REG8(0xa9), 0x77},
  245. {CCI_REG8(0xa1), 0x80}, {CCI_REG8(0xa2), 0x80},
  246. {GC2145_REG_PAGE_SELECT, 0x01},
  247. {CCI_REG8(0xdf), 0x0d}, {CCI_REG8(0xdc), 0x25}, {CCI_REG8(0xdd), 0x30},
  248. {CCI_REG8(0xe0), 0x77}, {CCI_REG8(0xe1), 0x80}, {CCI_REG8(0xe2), 0x77},
  249. {CCI_REG8(0xe3), 0x90}, {CCI_REG8(0xe6), 0x90}, {CCI_REG8(0xe7), 0xa0},
  250. {CCI_REG8(0xe8), 0x90}, {CCI_REG8(0xe9), 0xa0},
  251. /* AWB */
  252. /* measure window */
  253. {GC2145_REG_PAGE_SELECT, 0x00},
  254. {CCI_REG8(0xec), 0x06}, {CCI_REG8(0xed), 0x04}, {CCI_REG8(0xee), 0x60},
  255. {CCI_REG8(0xef), 0x90}, {CCI_REG8(0xb6), 0x01},
  256. {GC2145_REG_PAGE_SELECT, 0x01},
  257. {CCI_REG8(0x4f), 0x00}, {CCI_REG8(0x4f), 0x00}, {CCI_REG8(0x4b), 0x01},
  258. {CCI_REG8(0x4f), 0x00},
  259. {CCI_REG8(0x4c), 0x01}, {CCI_REG8(0x4d), 0x71}, {CCI_REG8(0x4e), 0x01},
  260. {CCI_REG8(0x4c), 0x01}, {CCI_REG8(0x4d), 0x91}, {CCI_REG8(0x4e), 0x01},
  261. {CCI_REG8(0x4c), 0x01}, {CCI_REG8(0x4d), 0x70}, {CCI_REG8(0x4e), 0x01},
  262. {CCI_REG8(0x4c), 0x01}, {CCI_REG8(0x4d), 0x90}, {CCI_REG8(0x4e), 0x02},
  263. {CCI_REG8(0x4c), 0x01}, {CCI_REG8(0x4d), 0xb0}, {CCI_REG8(0x4e), 0x02},
  264. {CCI_REG8(0x4c), 0x01}, {CCI_REG8(0x4d), 0x8f}, {CCI_REG8(0x4e), 0x02},
  265. {CCI_REG8(0x4c), 0x01}, {CCI_REG8(0x4d), 0x6f}, {CCI_REG8(0x4e), 0x02},
  266. {CCI_REG8(0x4c), 0x01}, {CCI_REG8(0x4d), 0xaf}, {CCI_REG8(0x4e), 0x02},
  267. {CCI_REG8(0x4c), 0x01}, {CCI_REG8(0x4d), 0xd0}, {CCI_REG8(0x4e), 0x02},
  268. {CCI_REG8(0x4c), 0x01}, {CCI_REG8(0x4d), 0xf0}, {CCI_REG8(0x4e), 0x02},
  269. {CCI_REG8(0x4c), 0x01}, {CCI_REG8(0x4d), 0xcf}, {CCI_REG8(0x4e), 0x02},
  270. {CCI_REG8(0x4c), 0x01}, {CCI_REG8(0x4d), 0xef}, {CCI_REG8(0x4e), 0x02},
  271. {CCI_REG8(0x4c), 0x01}, {CCI_REG8(0x4d), 0x6e}, {CCI_REG8(0x4e), 0x03},
  272. {CCI_REG8(0x4c), 0x01}, {CCI_REG8(0x4d), 0x8e}, {CCI_REG8(0x4e), 0x03},
  273. {CCI_REG8(0x4c), 0x01}, {CCI_REG8(0x4d), 0xae}, {CCI_REG8(0x4e), 0x03},
  274. {CCI_REG8(0x4c), 0x01}, {CCI_REG8(0x4d), 0xce}, {CCI_REG8(0x4e), 0x03},
  275. {CCI_REG8(0x4c), 0x01}, {CCI_REG8(0x4d), 0x4d}, {CCI_REG8(0x4e), 0x03},
  276. {CCI_REG8(0x4c), 0x01}, {CCI_REG8(0x4d), 0x6d}, {CCI_REG8(0x4e), 0x03},
  277. {CCI_REG8(0x4c), 0x01}, {CCI_REG8(0x4d), 0x8d}, {CCI_REG8(0x4e), 0x03},
  278. {CCI_REG8(0x4c), 0x01}, {CCI_REG8(0x4d), 0xad}, {CCI_REG8(0x4e), 0x03},
  279. {CCI_REG8(0x4c), 0x01}, {CCI_REG8(0x4d), 0xcd}, {CCI_REG8(0x4e), 0x03},
  280. {CCI_REG8(0x4c), 0x01}, {CCI_REG8(0x4d), 0x4c}, {CCI_REG8(0x4e), 0x03},
  281. {CCI_REG8(0x4c), 0x01}, {CCI_REG8(0x4d), 0x6c}, {CCI_REG8(0x4e), 0x03},
  282. {CCI_REG8(0x4c), 0x01}, {CCI_REG8(0x4d), 0x8c}, {CCI_REG8(0x4e), 0x03},
  283. {CCI_REG8(0x4c), 0x01}, {CCI_REG8(0x4d), 0xac}, {CCI_REG8(0x4e), 0x03},
  284. {CCI_REG8(0x4c), 0x01}, {CCI_REG8(0x4d), 0xcc}, {CCI_REG8(0x4e), 0x03},
  285. {CCI_REG8(0x4c), 0x01}, {CCI_REG8(0x4d), 0xcb}, {CCI_REG8(0x4e), 0x03},
  286. {CCI_REG8(0x4c), 0x01}, {CCI_REG8(0x4d), 0x4b}, {CCI_REG8(0x4e), 0x03},
  287. {CCI_REG8(0x4c), 0x01}, {CCI_REG8(0x4d), 0x6b}, {CCI_REG8(0x4e), 0x03},
  288. {CCI_REG8(0x4c), 0x01}, {CCI_REG8(0x4d), 0x8b}, {CCI_REG8(0x4e), 0x03},
  289. {CCI_REG8(0x4c), 0x01}, {CCI_REG8(0x4d), 0xab}, {CCI_REG8(0x4e), 0x03},
  290. {CCI_REG8(0x4c), 0x01}, {CCI_REG8(0x4d), 0x8a}, {CCI_REG8(0x4e), 0x04},
  291. {CCI_REG8(0x4c), 0x01}, {CCI_REG8(0x4d), 0xaa}, {CCI_REG8(0x4e), 0x04},
  292. {CCI_REG8(0x4c), 0x01}, {CCI_REG8(0x4d), 0xca}, {CCI_REG8(0x4e), 0x04},
  293. {CCI_REG8(0x4c), 0x01}, {CCI_REG8(0x4d), 0xca}, {CCI_REG8(0x4e), 0x04},
  294. {CCI_REG8(0x4c), 0x01}, {CCI_REG8(0x4d), 0xc9}, {CCI_REG8(0x4e), 0x04},
  295. {CCI_REG8(0x4c), 0x01}, {CCI_REG8(0x4d), 0x8a}, {CCI_REG8(0x4e), 0x04},
  296. {CCI_REG8(0x4c), 0x01}, {CCI_REG8(0x4d), 0x89}, {CCI_REG8(0x4e), 0x04},
  297. {CCI_REG8(0x4c), 0x01}, {CCI_REG8(0x4d), 0xa9}, {CCI_REG8(0x4e), 0x04},
  298. {CCI_REG8(0x4c), 0x02}, {CCI_REG8(0x4d), 0x0b}, {CCI_REG8(0x4e), 0x05},
  299. {CCI_REG8(0x4c), 0x02}, {CCI_REG8(0x4d), 0x0a}, {CCI_REG8(0x4e), 0x05},
  300. {CCI_REG8(0x4c), 0x01}, {CCI_REG8(0x4d), 0xeb}, {CCI_REG8(0x4e), 0x05},
  301. {CCI_REG8(0x4c), 0x01}, {CCI_REG8(0x4d), 0xea}, {CCI_REG8(0x4e), 0x05},
  302. {CCI_REG8(0x4c), 0x02}, {CCI_REG8(0x4d), 0x09}, {CCI_REG8(0x4e), 0x05},
  303. {CCI_REG8(0x4c), 0x02}, {CCI_REG8(0x4d), 0x29}, {CCI_REG8(0x4e), 0x05},
  304. {CCI_REG8(0x4c), 0x02}, {CCI_REG8(0x4d), 0x2a}, {CCI_REG8(0x4e), 0x05},
  305. {CCI_REG8(0x4c), 0x02}, {CCI_REG8(0x4d), 0x4a}, {CCI_REG8(0x4e), 0x05},
  306. {CCI_REG8(0x4c), 0x02}, {CCI_REG8(0x4d), 0x8a}, {CCI_REG8(0x4e), 0x06},
  307. {CCI_REG8(0x4c), 0x02}, {CCI_REG8(0x4d), 0x49}, {CCI_REG8(0x4e), 0x06},
  308. {CCI_REG8(0x4c), 0x02}, {CCI_REG8(0x4d), 0x69}, {CCI_REG8(0x4e), 0x06},
  309. {CCI_REG8(0x4c), 0x02}, {CCI_REG8(0x4d), 0x89}, {CCI_REG8(0x4e), 0x06},
  310. {CCI_REG8(0x4c), 0x02}, {CCI_REG8(0x4d), 0xa9}, {CCI_REG8(0x4e), 0x06},
  311. {CCI_REG8(0x4c), 0x02}, {CCI_REG8(0x4d), 0x48}, {CCI_REG8(0x4e), 0x06},
  312. {CCI_REG8(0x4c), 0x02}, {CCI_REG8(0x4d), 0x68}, {CCI_REG8(0x4e), 0x06},
  313. {CCI_REG8(0x4c), 0x02}, {CCI_REG8(0x4d), 0x69}, {CCI_REG8(0x4e), 0x06},
  314. {CCI_REG8(0x4c), 0x02}, {CCI_REG8(0x4d), 0xca}, {CCI_REG8(0x4e), 0x07},
  315. {CCI_REG8(0x4c), 0x02}, {CCI_REG8(0x4d), 0xc9}, {CCI_REG8(0x4e), 0x07},
  316. {CCI_REG8(0x4c), 0x02}, {CCI_REG8(0x4d), 0xe9}, {CCI_REG8(0x4e), 0x07},
  317. {CCI_REG8(0x4c), 0x03}, {CCI_REG8(0x4d), 0x09}, {CCI_REG8(0x4e), 0x07},
  318. {CCI_REG8(0x4c), 0x02}, {CCI_REG8(0x4d), 0xc8}, {CCI_REG8(0x4e), 0x07},
  319. {CCI_REG8(0x4c), 0x02}, {CCI_REG8(0x4d), 0xe8}, {CCI_REG8(0x4e), 0x07},
  320. {CCI_REG8(0x4c), 0x02}, {CCI_REG8(0x4d), 0xa7}, {CCI_REG8(0x4e), 0x07},
  321. {CCI_REG8(0x4c), 0x02}, {CCI_REG8(0x4d), 0xc7}, {CCI_REG8(0x4e), 0x07},
  322. {CCI_REG8(0x4c), 0x02}, {CCI_REG8(0x4d), 0xe7}, {CCI_REG8(0x4e), 0x07},
  323. {CCI_REG8(0x4c), 0x03}, {CCI_REG8(0x4d), 0x07}, {CCI_REG8(0x4e), 0x07},
  324. {CCI_REG8(0x4f), 0x01},
  325. {CCI_REG8(0x50), 0x80}, {CCI_REG8(0x51), 0xa8}, {CCI_REG8(0x52), 0x47},
  326. {CCI_REG8(0x53), 0x38}, {CCI_REG8(0x54), 0xc7}, {CCI_REG8(0x56), 0x0e},
  327. {CCI_REG8(0x58), 0x08}, {CCI_REG8(0x5b), 0x00}, {CCI_REG8(0x5c), 0x74},
  328. {CCI_REG8(0x5d), 0x8b}, {CCI_REG8(0x61), 0xdb}, {CCI_REG8(0x62), 0xb8},
  329. {CCI_REG8(0x63), 0x86}, {CCI_REG8(0x64), 0xc0}, {CCI_REG8(0x65), 0x04},
  330. {CCI_REG8(0x67), 0xa8}, {CCI_REG8(0x68), 0xb0}, {CCI_REG8(0x69), 0x00},
  331. {CCI_REG8(0x6a), 0xa8}, {CCI_REG8(0x6b), 0xb0}, {CCI_REG8(0x6c), 0xaf},
  332. {CCI_REG8(0x6d), 0x8b}, {CCI_REG8(0x6e), 0x50}, {CCI_REG8(0x6f), 0x18},
  333. {CCI_REG8(0x73), 0xf0}, {CCI_REG8(0x70), 0x0d}, {CCI_REG8(0x71), 0x60},
  334. {CCI_REG8(0x72), 0x80}, {CCI_REG8(0x74), 0x01}, {CCI_REG8(0x75), 0x01},
  335. {CCI_REG8(0x7f), 0x0c}, {CCI_REG8(0x76), 0x70}, {CCI_REG8(0x77), 0x58},
  336. {CCI_REG8(0x78), 0xa0}, {CCI_REG8(0x79), 0x5e}, {CCI_REG8(0x7a), 0x54},
  337. {CCI_REG8(0x7b), 0x58},
  338. /* CC */
  339. {GC2145_REG_PAGE_SELECT, 0x02},
  340. {CCI_REG8(0xc0), 0x01}, {CCI_REG8(0xc1), 0x44}, {CCI_REG8(0xc2), 0xfd},
  341. {CCI_REG8(0xc3), 0x04}, {CCI_REG8(0xc4), 0xf0}, {CCI_REG8(0xc5), 0x48},
  342. {CCI_REG8(0xc6), 0xfd}, {CCI_REG8(0xc7), 0x46}, {CCI_REG8(0xc8), 0xfd},
  343. {CCI_REG8(0xc9), 0x02}, {CCI_REG8(0xca), 0xe0}, {CCI_REG8(0xcb), 0x45},
  344. {CCI_REG8(0xcc), 0xec}, {CCI_REG8(0xcd), 0x48}, {CCI_REG8(0xce), 0xf0},
  345. {CCI_REG8(0xcf), 0xf0}, {CCI_REG8(0xe3), 0x0c}, {CCI_REG8(0xe4), 0x4b},
  346. {CCI_REG8(0xe5), 0xe0},
  347. /* ABS */
  348. {GC2145_REG_PAGE_SELECT, 0x01},
  349. {CCI_REG8(0x9f), 0x40},
  350. /* Dark sun */
  351. {GC2145_REG_PAGE_SELECT, 0x02},
  352. {CCI_REG8(0x40), 0xbf}, {CCI_REG8(0x46), 0xcf},
  353. };
  354. #define GC2145_640_480_PIXELRATE 30000000
  355. #define GC2145_640_480_LINKFREQ 120000000
  356. #define GC2145_640_480_HBLANK 0x0130
  357. #define GC2145_640_480_VBLANK 0x000c
  358. static const struct cci_reg_sequence gc2145_mode_640_480_regs[] = {
  359. {GC2145_REG_PAGE_SELECT, 0xf0}, {GC2145_REG_PAGE_SELECT, 0xf0},
  360. {GC2145_REG_PAGE_SELECT, 0xf0}, {CCI_REG8(0xfc), 0x06},
  361. {CCI_REG8(0xf6), 0x00}, {CCI_REG8(0xf7), 0x1d}, {CCI_REG8(0xf8), 0x86},
  362. {CCI_REG8(0xfa), 0x00}, {CCI_REG8(0xf9), 0x8e},
  363. /* Disable PAD IO */
  364. {GC2145_REG_PAD_IO, 0x00},
  365. {GC2145_REG_PAGE_SELECT, 0x00},
  366. /* Row/Col start - 0/0 */
  367. {GC2145_REG_ROW_START, 0x0000},
  368. {GC2145_REG_COL_START, 0x0000},
  369. /* Window size 1216/1618 */
  370. {GC2145_REG_WIN_HEIGHT, 0x04c0},
  371. {GC2145_REG_WIN_WIDTH, 0x0652},
  372. /* Scalar more */
  373. {CCI_REG8(0xfd), 0x01}, {CCI_REG8(0xfa), 0x00},
  374. /* Crop 640-480@0-0 */
  375. {GC2145_REG_CROP_ENABLE, 0x01},
  376. {GC2145_REG_CROP_Y, 0x0000},
  377. {GC2145_REG_CROP_X, 0x0000},
  378. {GC2145_REG_CROP_HEIGHT, 0x01e0},
  379. {GC2145_REG_CROP_WIDTH, 0x0280},
  380. /* Subsampling configuration */
  381. {CCI_REG8(0x99), 0x55}, {CCI_REG8(0x9a), 0x06}, {CCI_REG8(0x9b), 0x01},
  382. {CCI_REG8(0x9c), 0x23}, {CCI_REG8(0x9d), 0x00}, {CCI_REG8(0x9e), 0x00},
  383. {CCI_REG8(0x9f), 0x01}, {CCI_REG8(0xa0), 0x23}, {CCI_REG8(0xa1), 0x00},
  384. {CCI_REG8(0xa2), 0x00},
  385. {GC2145_REG_PAGE_SELECT, 0x01},
  386. /* AEC anti-flicker */
  387. {CCI_REG16(0x25), 0x0175},
  388. /* AEC exposure level 1-5 */
  389. {CCI_REG16(0x27), 0x045f}, {CCI_REG16(0x29), 0x045f},
  390. {CCI_REG16(0x2b), 0x045f}, {CCI_REG16(0x2d), 0x045f},
  391. };
  392. #define GC2145_1280_720_PIXELRATE 48000000
  393. #define GC2145_1280_720_LINKFREQ 192000000
  394. #define GC2145_1280_720_HBLANK 0x0156
  395. #define GC2145_1280_720_VBLANK 0x0011
  396. static const struct cci_reg_sequence gc2145_mode_1280_720_regs[] = {
  397. {GC2145_REG_PAGE_SELECT, 0xf0}, {GC2145_REG_PAGE_SELECT, 0xf0},
  398. {GC2145_REG_PAGE_SELECT, 0xf0}, {CCI_REG8(0xfc), 0x06},
  399. {CCI_REG8(0xf6), 0x00}, {CCI_REG8(0xf7), 0x1d}, {CCI_REG8(0xf8), 0x83},
  400. {CCI_REG8(0xfa), 0x00}, {CCI_REG8(0xf9), 0x8e},
  401. /* Disable PAD IO */
  402. {GC2145_REG_PAD_IO, 0x00},
  403. {GC2145_REG_PAGE_SELECT, 0x00},
  404. /* Row/Col start - 240/160 */
  405. {GC2145_REG_ROW_START, 0x00f0},
  406. {GC2145_REG_COL_START, 0x00a0},
  407. /* Window size 736/1296 */
  408. {GC2145_REG_WIN_HEIGHT, 0x02e0},
  409. {GC2145_REG_WIN_WIDTH, 0x0510},
  410. /* Crop 1280-720@0-0 */
  411. {GC2145_REG_CROP_ENABLE, 0x01},
  412. {GC2145_REG_CROP_Y, 0x0000},
  413. {GC2145_REG_CROP_X, 0x0000},
  414. {GC2145_REG_CROP_HEIGHT, 0x02d0},
  415. {GC2145_REG_CROP_WIDTH, 0x0500},
  416. {GC2145_REG_PAGE_SELECT, 0x01},
  417. /* AEC anti-flicker */
  418. {CCI_REG16(0x25), 0x00e6},
  419. /* AEC exposure level 1-5 */
  420. {CCI_REG16(0x27), 0x02b2}, {CCI_REG16(0x29), 0x02b2},
  421. {CCI_REG16(0x2b), 0x02b2}, {CCI_REG16(0x2d), 0x02b2},
  422. };
  423. #define GC2145_1600_1200_PIXELRATE 60000000
  424. #define GC2145_1600_1200_LINKFREQ 240000000
  425. #define GC2145_1600_1200_HBLANK 0x0156
  426. #define GC2145_1600_1200_VBLANK 0x0010
  427. static const struct cci_reg_sequence gc2145_mode_1600_1200_regs[] = {
  428. {GC2145_REG_PAGE_SELECT, 0xf0}, {GC2145_REG_PAGE_SELECT, 0xf0},
  429. {GC2145_REG_PAGE_SELECT, 0xf0}, {CCI_REG8(0xfc), 0x06},
  430. {CCI_REG8(0xf6), 0x00}, {CCI_REG8(0xf7), 0x1d}, {CCI_REG8(0xf8), 0x84},
  431. {CCI_REG8(0xfa), 0x00}, {CCI_REG8(0xf9), 0x8e},
  432. /* Disable PAD IO */
  433. {GC2145_REG_PAD_IO, 0x00},
  434. {GC2145_REG_PAGE_SELECT, 0x00},
  435. /* Row/Col start - 0/0 */
  436. {GC2145_REG_ROW_START, 0x0000},
  437. {GC2145_REG_COL_START, 0x0000},
  438. /* Window size: 1216/1618 */
  439. {GC2145_REG_WIN_HEIGHT, 0x04c0},
  440. {GC2145_REG_WIN_WIDTH, 0x0652},
  441. /* Crop 1600-1200@0-0 */
  442. {GC2145_REG_CROP_ENABLE, 0x01},
  443. {GC2145_REG_CROP_Y, 0x0000},
  444. {GC2145_REG_CROP_X, 0x0000},
  445. {GC2145_REG_CROP_HEIGHT, 0x04b0},
  446. {GC2145_REG_CROP_WIDTH, 0x0640},
  447. {GC2145_REG_PAGE_SELECT, 0x01},
  448. /* AEC anti-flicker */
  449. {CCI_REG16(0x25), 0x00fa},
  450. /* AEC exposure level 1-5 */
  451. {CCI_REG16(0x27), 0x04e2}, {CCI_REG16(0x29), 0x04e2},
  452. {CCI_REG16(0x2b), 0x04e2}, {CCI_REG16(0x2d), 0x04e2},
  453. };
  454. static const s64 gc2145_link_freq_menu[] = {
  455. GC2145_640_480_LINKFREQ,
  456. GC2145_1280_720_LINKFREQ,
  457. GC2145_1600_1200_LINKFREQ,
  458. };
  459. /* Regulators supplies */
  460. static const char * const gc2145_supply_name[] = {
  461. "iovdd", /* Digital I/O (1.7-3V) suppply */
  462. "avdd", /* Analog (2.7-3V) supply */
  463. "dvdd", /* Digital Core (1.7-1.9V) supply */
  464. };
  465. #define GC2145_NUM_SUPPLIES ARRAY_SIZE(gc2145_supply_name)
  466. /* Mode configs */
  467. #define GC2145_MODE_640X480 0
  468. #define GC2145_MODE_1280X720 1
  469. #define GC2145_MODE_1600X1200 2
  470. static const struct gc2145_mode supported_modes[] = {
  471. {
  472. /* 640x480 30fps mode */
  473. .width = 640,
  474. .height = 480,
  475. .reg_seq = gc2145_mode_640_480_regs,
  476. .reg_seq_size = ARRAY_SIZE(gc2145_mode_640_480_regs),
  477. .pixel_rate = GC2145_640_480_PIXELRATE,
  478. .crop = {
  479. .top = 0,
  480. .left = 0,
  481. .width = 640,
  482. .height = 480,
  483. },
  484. .hblank = GC2145_640_480_HBLANK,
  485. .vblank = GC2145_640_480_VBLANK,
  486. .link_freq_index = GC2145_MODE_640X480,
  487. },
  488. {
  489. /* 1280x720 30fps mode */
  490. .width = 1280,
  491. .height = 720,
  492. .reg_seq = gc2145_mode_1280_720_regs,
  493. .reg_seq_size = ARRAY_SIZE(gc2145_mode_1280_720_regs),
  494. .pixel_rate = GC2145_1280_720_PIXELRATE,
  495. .crop = {
  496. .top = 160,
  497. .left = 240,
  498. .width = 1280,
  499. .height = 720,
  500. },
  501. .hblank = GC2145_1280_720_HBLANK,
  502. .vblank = GC2145_1280_720_VBLANK,
  503. .link_freq_index = GC2145_MODE_1280X720,
  504. },
  505. {
  506. /* 1600x1200 20fps mode */
  507. .width = 1600,
  508. .height = 1200,
  509. .reg_seq = gc2145_mode_1600_1200_regs,
  510. .reg_seq_size = ARRAY_SIZE(gc2145_mode_1600_1200_regs),
  511. .pixel_rate = GC2145_1600_1200_PIXELRATE,
  512. .crop = {
  513. .top = 0,
  514. .left = 0,
  515. .width = 1600,
  516. .height = 1200,
  517. },
  518. .hblank = GC2145_1600_1200_HBLANK,
  519. .vblank = GC2145_1600_1200_VBLANK,
  520. .link_freq_index = GC2145_MODE_1600X1200,
  521. },
  522. };
  523. /**
  524. * struct gc2145_format - GC2145 pixel format description
  525. * @code: media bus (MBUS) associated code
  526. * @colorspace: V4L2 colorspace
  527. * @datatype: MIPI CSI2 data type
  528. * @output_fmt: GC2145 output format
  529. * @switch_bit: GC2145 first/second switch
  530. * @row_col_switch: GC2145 switch row and/or column
  531. */
  532. struct gc2145_format {
  533. unsigned int code;
  534. unsigned int colorspace;
  535. unsigned char datatype;
  536. unsigned char output_fmt;
  537. bool switch_bit;
  538. unsigned char row_col_switch;
  539. };
  540. /* All supported formats */
  541. static const struct gc2145_format supported_formats[] = {
  542. {
  543. .code = MEDIA_BUS_FMT_UYVY8_1X16,
  544. .colorspace = V4L2_COLORSPACE_SRGB,
  545. .datatype = MIPI_CSI2_DT_YUV422_8B,
  546. .output_fmt = 0x00,
  547. },
  548. {
  549. .code = MEDIA_BUS_FMT_VYUY8_1X16,
  550. .colorspace = V4L2_COLORSPACE_SRGB,
  551. .datatype = MIPI_CSI2_DT_YUV422_8B,
  552. .output_fmt = 0x01,
  553. },
  554. {
  555. .code = MEDIA_BUS_FMT_YUYV8_1X16,
  556. .colorspace = V4L2_COLORSPACE_SRGB,
  557. .datatype = MIPI_CSI2_DT_YUV422_8B,
  558. .output_fmt = 0x02,
  559. },
  560. {
  561. .code = MEDIA_BUS_FMT_YVYU8_1X16,
  562. .colorspace = V4L2_COLORSPACE_SRGB,
  563. .datatype = MIPI_CSI2_DT_YUV422_8B,
  564. .output_fmt = 0x03,
  565. },
  566. {
  567. .code = MEDIA_BUS_FMT_RGB565_1X16,
  568. .colorspace = V4L2_COLORSPACE_SRGB,
  569. .datatype = MIPI_CSI2_DT_RGB565,
  570. .output_fmt = 0x06,
  571. .switch_bit = true,
  572. },
  573. {
  574. .code = MEDIA_BUS_FMT_SGRBG8_1X8,
  575. .colorspace = V4L2_COLORSPACE_RAW,
  576. .datatype = MIPI_CSI2_DT_RAW8,
  577. .output_fmt = 0x17,
  578. .row_col_switch = GC2145_SYNC_MODE_COL_SWITCH,
  579. },
  580. {
  581. .code = MEDIA_BUS_FMT_SRGGB8_1X8,
  582. .colorspace = V4L2_COLORSPACE_RAW,
  583. .datatype = MIPI_CSI2_DT_RAW8,
  584. .output_fmt = 0x17,
  585. .row_col_switch = GC2145_SYNC_MODE_COL_SWITCH | GC2145_SYNC_MODE_ROW_SWITCH,
  586. },
  587. {
  588. .code = MEDIA_BUS_FMT_SBGGR8_1X8,
  589. .colorspace = V4L2_COLORSPACE_RAW,
  590. .datatype = MIPI_CSI2_DT_RAW8,
  591. .output_fmt = 0x17,
  592. .row_col_switch = 0,
  593. },
  594. {
  595. .code = MEDIA_BUS_FMT_SGBRG8_1X8,
  596. .colorspace = V4L2_COLORSPACE_RAW,
  597. .datatype = MIPI_CSI2_DT_RAW8,
  598. .output_fmt = 0x17,
  599. .row_col_switch = GC2145_SYNC_MODE_ROW_SWITCH,
  600. },
  601. };
  602. struct gc2145_ctrls {
  603. struct v4l2_ctrl_handler handler;
  604. struct v4l2_ctrl *pixel_rate;
  605. struct v4l2_ctrl *link_freq;
  606. struct v4l2_ctrl *test_pattern;
  607. struct v4l2_ctrl *hflip;
  608. struct v4l2_ctrl *vflip;
  609. struct v4l2_ctrl *hblank;
  610. struct v4l2_ctrl *vblank;
  611. };
  612. struct gc2145 {
  613. struct v4l2_subdev sd;
  614. struct media_pad pad;
  615. struct regmap *regmap;
  616. struct clk *xclk;
  617. struct gpio_desc *reset_gpio;
  618. struct gpio_desc *powerdown_gpio;
  619. struct regulator_bulk_data supplies[GC2145_NUM_SUPPLIES];
  620. /* V4L2 controls */
  621. struct gc2145_ctrls ctrls;
  622. /* Current mode */
  623. const struct gc2145_mode *mode;
  624. };
  625. static inline struct gc2145 *to_gc2145(struct v4l2_subdev *_sd)
  626. {
  627. return container_of(_sd, struct gc2145, sd);
  628. }
  629. static inline struct v4l2_subdev *gc2145_ctrl_to_sd(struct v4l2_ctrl *ctrl)
  630. {
  631. return &container_of(ctrl->handler, struct gc2145,
  632. ctrls.handler)->sd;
  633. }
  634. static const struct gc2145_format *
  635. gc2145_get_format_code(struct gc2145 *gc2145, u32 code)
  636. {
  637. unsigned int i;
  638. for (i = 0; i < ARRAY_SIZE(supported_formats); i++) {
  639. if (supported_formats[i].code == code)
  640. break;
  641. }
  642. if (i >= ARRAY_SIZE(supported_formats))
  643. i = 0;
  644. return &supported_formats[i];
  645. }
  646. static void gc2145_update_pad_format(struct gc2145 *gc2145,
  647. const struct gc2145_mode *mode,
  648. struct v4l2_mbus_framefmt *fmt, u32 code,
  649. u32 colorspace)
  650. {
  651. fmt->code = code;
  652. fmt->width = mode->width;
  653. fmt->height = mode->height;
  654. fmt->field = V4L2_FIELD_NONE;
  655. fmt->colorspace = V4L2_COLORSPACE_SRGB;
  656. fmt->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
  657. fmt->quantization = V4L2_QUANTIZATION_DEFAULT;
  658. fmt->xfer_func = V4L2_XFER_FUNC_DEFAULT;
  659. }
  660. static int gc2145_init_state(struct v4l2_subdev *sd,
  661. struct v4l2_subdev_state *state)
  662. {
  663. struct gc2145 *gc2145 = to_gc2145(sd);
  664. struct v4l2_mbus_framefmt *format;
  665. struct v4l2_rect *crop;
  666. /* Initialize pad format */
  667. format = v4l2_subdev_state_get_format(state, 0);
  668. gc2145_update_pad_format(gc2145, &supported_modes[0], format,
  669. MEDIA_BUS_FMT_RGB565_1X16,
  670. V4L2_COLORSPACE_SRGB);
  671. /* Initialize crop rectangle. */
  672. crop = v4l2_subdev_state_get_crop(state, 0);
  673. *crop = supported_modes[0].crop;
  674. return 0;
  675. }
  676. static int gc2145_get_selection(struct v4l2_subdev *sd,
  677. struct v4l2_subdev_state *sd_state,
  678. struct v4l2_subdev_selection *sel)
  679. {
  680. switch (sel->target) {
  681. case V4L2_SEL_TGT_CROP:
  682. sel->r = *v4l2_subdev_state_get_crop(sd_state, 0);
  683. return 0;
  684. case V4L2_SEL_TGT_NATIVE_SIZE:
  685. sel->r.top = 0;
  686. sel->r.left = 0;
  687. sel->r.width = GC2145_NATIVE_WIDTH;
  688. sel->r.height = GC2145_NATIVE_HEIGHT;
  689. return 0;
  690. case V4L2_SEL_TGT_CROP_DEFAULT:
  691. case V4L2_SEL_TGT_CROP_BOUNDS:
  692. sel->r.top = 0;
  693. sel->r.left = 0;
  694. sel->r.width = 1600;
  695. sel->r.height = 1200;
  696. return 0;
  697. }
  698. return -EINVAL;
  699. }
  700. static int gc2145_enum_mbus_code(struct v4l2_subdev *sd,
  701. struct v4l2_subdev_state *sd_state,
  702. struct v4l2_subdev_mbus_code_enum *code)
  703. {
  704. if (code->index >= ARRAY_SIZE(supported_formats))
  705. return -EINVAL;
  706. code->code = supported_formats[code->index].code;
  707. return 0;
  708. }
  709. static int gc2145_enum_frame_size(struct v4l2_subdev *sd,
  710. struct v4l2_subdev_state *sd_state,
  711. struct v4l2_subdev_frame_size_enum *fse)
  712. {
  713. struct gc2145 *gc2145 = to_gc2145(sd);
  714. const struct gc2145_format *gc2145_format;
  715. u32 code;
  716. if (fse->index >= ARRAY_SIZE(supported_modes))
  717. return -EINVAL;
  718. gc2145_format = gc2145_get_format_code(gc2145, fse->code);
  719. code = gc2145_format->code;
  720. if (fse->code != code)
  721. return -EINVAL;
  722. fse->min_width = supported_modes[fse->index].width;
  723. fse->max_width = fse->min_width;
  724. fse->min_height = supported_modes[fse->index].height;
  725. fse->max_height = fse->min_height;
  726. return 0;
  727. }
  728. static int gc2145_set_pad_format(struct v4l2_subdev *sd,
  729. struct v4l2_subdev_state *sd_state,
  730. struct v4l2_subdev_format *fmt)
  731. {
  732. struct gc2145 *gc2145 = to_gc2145(sd);
  733. const struct gc2145_mode *mode;
  734. const struct gc2145_format *gc2145_fmt;
  735. struct v4l2_mbus_framefmt *framefmt;
  736. struct gc2145_ctrls *ctrls = &gc2145->ctrls;
  737. struct v4l2_rect *crop;
  738. gc2145_fmt = gc2145_get_format_code(gc2145, fmt->format.code);
  739. mode = v4l2_find_nearest_size(supported_modes,
  740. ARRAY_SIZE(supported_modes),
  741. width, height,
  742. fmt->format.width, fmt->format.height);
  743. /* In RAW mode, VGA is not possible so use 720p instead */
  744. if (gc2145_fmt->colorspace == V4L2_COLORSPACE_RAW &&
  745. mode == &supported_modes[GC2145_MODE_640X480])
  746. mode = &supported_modes[GC2145_MODE_1280X720];
  747. gc2145_update_pad_format(gc2145, mode, &fmt->format, gc2145_fmt->code,
  748. gc2145_fmt->colorspace);
  749. framefmt = v4l2_subdev_state_get_format(sd_state, fmt->pad);
  750. if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
  751. gc2145->mode = mode;
  752. /* Update pixel_rate based on the mode */
  753. __v4l2_ctrl_s_ctrl_int64(ctrls->pixel_rate, mode->pixel_rate);
  754. /* Update link_freq based on the mode */
  755. __v4l2_ctrl_s_ctrl(ctrls->link_freq, mode->link_freq_index);
  756. /* Update hblank/vblank based on the mode */
  757. __v4l2_ctrl_s_ctrl(ctrls->hblank, mode->hblank);
  758. __v4l2_ctrl_s_ctrl(ctrls->vblank, mode->vblank);
  759. }
  760. *framefmt = fmt->format;
  761. crop = v4l2_subdev_state_get_crop(sd_state, fmt->pad);
  762. *crop = mode->crop;
  763. return 0;
  764. }
  765. static const struct cci_reg_sequence gc2145_common_mipi_regs[] = {
  766. {GC2145_REG_PAGE_SELECT, 0x03},
  767. {GC2145_REG_DPHY_ANALOG_MODE1, GC2145_DPHY_MODE_PHY_CLK_EN |
  768. GC2145_DPHY_MODE_PHY_LANE0_EN |
  769. GC2145_DPHY_MODE_PHY_LANE1_EN |
  770. GC2145_DPHY_MODE_PHY_CLK_LANE_P2S_SEL},
  771. {GC2145_REG_DPHY_ANALOG_MODE2, GC2145_DPHY_CLK_DIFF(2) |
  772. GC2145_DPHY_LANE0_DIFF(2)},
  773. {GC2145_REG_DPHY_ANALOG_MODE3, GC2145_DPHY_LANE1_DIFF(0) |
  774. GC2145_DPHY_CLK_DELAY},
  775. {GC2145_REG_FIFO_MODE, GC2145_FIFO_MODE_READ_GATE |
  776. GC2145_FIFO_MODE_MIPI_CLK_MODULE},
  777. {GC2145_REG_DPHY_MODE, GC2145_DPHY_MODE_TRIGGER_PROG},
  778. /* Clock & Data lanes timing */
  779. {GC2145_REG_T_LPX, 0x10},
  780. {GC2145_REG_T_CLK_HS_PREPARE, 0x04}, {GC2145_REG_T_CLK_ZERO, 0x10},
  781. {GC2145_REG_T_CLK_PRE, 0x10}, {GC2145_REG_T_CLK_POST, 0x10},
  782. {GC2145_REG_T_CLK_TRAIL, 0x05},
  783. {GC2145_REG_T_HS_PREPARE, 0x03}, {GC2145_REG_T_HS_ZERO, 0x0a},
  784. {GC2145_REG_T_HS_TRAIL, 0x06},
  785. };
  786. static int gc2145_config_mipi_mode(struct gc2145 *gc2145,
  787. const struct gc2145_format *gc2145_format)
  788. {
  789. u16 lwc, fifo_full_lvl;
  790. int ret = 0;
  791. /* Common MIPI settings */
  792. cci_multi_reg_write(gc2145->regmap, gc2145_common_mipi_regs,
  793. ARRAY_SIZE(gc2145_common_mipi_regs), &ret);
  794. /*
  795. * Adjust the MIPI buffer settings.
  796. * For YUV/RGB, LWC = image width * 2
  797. * For RAW8, LWC = image width
  798. * For RAW10, LWC = image width * 1.25
  799. */
  800. if (gc2145_format->colorspace != V4L2_COLORSPACE_RAW)
  801. lwc = gc2145->mode->width * 2;
  802. else
  803. lwc = gc2145->mode->width;
  804. cci_write(gc2145->regmap, GC2145_REG_LWC, lwc, &ret);
  805. /*
  806. * Adjust the MIPI FIFO Full Level
  807. * 640x480 RGB: 0x0190
  808. * 1280x720 / 1600x1200 (aka no scaler) non RAW: 0x0001
  809. * 1600x1200 RAW: 0x0190
  810. */
  811. if (gc2145_format->colorspace != V4L2_COLORSPACE_RAW) {
  812. if (gc2145->mode->width == 1280 || gc2145->mode->width == 1600)
  813. fifo_full_lvl = 0x0001;
  814. else
  815. fifo_full_lvl = 0x0190;
  816. } else {
  817. fifo_full_lvl = 0x0190;
  818. }
  819. cci_write(gc2145->regmap, GC2145_REG_FIFO_FULL_LVL,
  820. fifo_full_lvl, &ret);
  821. /*
  822. * Set the FIFO gate mode / MIPI wdiv set:
  823. * 0xf1 in case of RAW mode and 0xf0 otherwise
  824. */
  825. cci_write(gc2145->regmap, GC2145_REG_FIFO_GATE_MODE,
  826. gc2145_format->colorspace == V4L2_COLORSPACE_RAW ?
  827. 0xf1 : 0xf0, &ret);
  828. /* Set the MIPI data type */
  829. cci_write(gc2145->regmap, GC2145_REG_MIPI_DT,
  830. gc2145_format->datatype, &ret);
  831. /* Configure mode and enable CSI */
  832. cci_write(gc2145->regmap, GC2145_REG_BUF_CSI2_MODE,
  833. GC2145_CSI2_MODE_RAW8 | GC2145_CSI2_MODE_DOUBLE |
  834. GC2145_CSI2_MODE_EN | GC2145_CSI2_MODE_MIPI_EN, &ret);
  835. return ret;
  836. }
  837. static int gc2145_enable_streams(struct v4l2_subdev *sd,
  838. struct v4l2_subdev_state *state, u32 pad,
  839. u64 streams_mask)
  840. {
  841. struct gc2145 *gc2145 = to_gc2145(sd);
  842. struct i2c_client *client = v4l2_get_subdevdata(&gc2145->sd);
  843. const struct gc2145_format *gc2145_format;
  844. struct v4l2_mbus_framefmt *fmt;
  845. int ret;
  846. ret = pm_runtime_resume_and_get(&client->dev);
  847. if (ret < 0)
  848. return ret;
  849. /* Apply default values of current mode */
  850. cci_multi_reg_write(gc2145->regmap, gc2145->mode->reg_seq,
  851. gc2145->mode->reg_seq_size, &ret);
  852. cci_multi_reg_write(gc2145->regmap, gc2145_common_regs,
  853. ARRAY_SIZE(gc2145_common_regs), &ret);
  854. if (ret) {
  855. dev_err(&client->dev, "%s failed to write regs\n", __func__);
  856. goto err_rpm_put;
  857. }
  858. fmt = v4l2_subdev_state_get_format(state, 0);
  859. gc2145_format = gc2145_get_format_code(gc2145, fmt->code);
  860. /* Set the output format */
  861. cci_write(gc2145->regmap, GC2145_REG_PAGE_SELECT, 0x00, &ret);
  862. cci_write(gc2145->regmap, GC2145_REG_OUTPUT_FMT,
  863. gc2145_format->output_fmt, &ret);
  864. cci_update_bits(gc2145->regmap, GC2145_REG_BYPASS_MODE,
  865. GC2145_BYPASS_MODE_SWITCH,
  866. gc2145_format->switch_bit ? GC2145_BYPASS_MODE_SWITCH
  867. : 0, &ret);
  868. cci_update_bits(gc2145->regmap, GC2145_REG_SYNC_MODE,
  869. GC2145_SYNC_MODE_COL_SWITCH |
  870. GC2145_SYNC_MODE_ROW_SWITCH,
  871. gc2145_format->row_col_switch, &ret);
  872. if (ret) {
  873. dev_err(&client->dev, "%s failed to write regs\n", __func__);
  874. goto err_rpm_put;
  875. }
  876. /* Apply customized values from user */
  877. ret = __v4l2_ctrl_handler_setup(&gc2145->ctrls.handler);
  878. if (ret) {
  879. dev_err(&client->dev, "%s failed to apply ctrls\n", __func__);
  880. goto err_rpm_put;
  881. }
  882. /* Perform MIPI specific configuration */
  883. ret = gc2145_config_mipi_mode(gc2145, gc2145_format);
  884. if (ret) {
  885. dev_err(&client->dev, "%s failed to write mipi conf\n",
  886. __func__);
  887. goto err_rpm_put;
  888. }
  889. cci_write(gc2145->regmap, GC2145_REG_PAGE_SELECT, 0x00, &ret);
  890. return 0;
  891. err_rpm_put:
  892. pm_runtime_put_autosuspend(&client->dev);
  893. return ret;
  894. }
  895. static int gc2145_disable_streams(struct v4l2_subdev *sd,
  896. struct v4l2_subdev_state *state, u32 pad,
  897. u64 streams_mask)
  898. {
  899. struct gc2145 *gc2145 = to_gc2145(sd);
  900. struct i2c_client *client = v4l2_get_subdevdata(&gc2145->sd);
  901. int ret = 0;
  902. /* Disable lanes & mipi streaming */
  903. cci_write(gc2145->regmap, GC2145_REG_PAGE_SELECT, 0x03, &ret);
  904. cci_update_bits(gc2145->regmap, GC2145_REG_BUF_CSI2_MODE,
  905. GC2145_CSI2_MODE_EN | GC2145_CSI2_MODE_MIPI_EN, 0,
  906. &ret);
  907. cci_write(gc2145->regmap, GC2145_REG_PAGE_SELECT, 0x00, &ret);
  908. if (ret)
  909. dev_err(&client->dev, "%s failed to write regs\n", __func__);
  910. pm_runtime_put_autosuspend(&client->dev);
  911. return ret;
  912. }
  913. /* Power/clock management functions */
  914. static int gc2145_power_on(struct device *dev)
  915. {
  916. struct v4l2_subdev *sd = dev_get_drvdata(dev);
  917. struct gc2145 *gc2145 = to_gc2145(sd);
  918. int ret;
  919. ret = regulator_bulk_enable(GC2145_NUM_SUPPLIES, gc2145->supplies);
  920. if (ret) {
  921. dev_err(dev, "failed to enable regulators\n");
  922. return ret;
  923. }
  924. ret = clk_prepare_enable(gc2145->xclk);
  925. if (ret) {
  926. dev_err(dev, "failed to enable clock\n");
  927. goto reg_off;
  928. }
  929. gpiod_set_value_cansleep(gc2145->powerdown_gpio, 0);
  930. gpiod_set_value_cansleep(gc2145->reset_gpio, 0);
  931. /*
  932. * Datasheet doesn't mention timing between PWDN/RESETB control and
  933. * i2c access however, experimentation shows that a rather big delay is
  934. * needed.
  935. */
  936. msleep(41);
  937. return 0;
  938. reg_off:
  939. regulator_bulk_disable(GC2145_NUM_SUPPLIES, gc2145->supplies);
  940. return ret;
  941. }
  942. static int gc2145_power_off(struct device *dev)
  943. {
  944. struct v4l2_subdev *sd = dev_get_drvdata(dev);
  945. struct gc2145 *gc2145 = to_gc2145(sd);
  946. gpiod_set_value_cansleep(gc2145->powerdown_gpio, 1);
  947. gpiod_set_value_cansleep(gc2145->reset_gpio, 1);
  948. clk_disable_unprepare(gc2145->xclk);
  949. regulator_bulk_disable(GC2145_NUM_SUPPLIES, gc2145->supplies);
  950. return 0;
  951. }
  952. static int gc2145_get_regulators(struct gc2145 *gc2145)
  953. {
  954. struct i2c_client *client = v4l2_get_subdevdata(&gc2145->sd);
  955. unsigned int i;
  956. for (i = 0; i < GC2145_NUM_SUPPLIES; i++)
  957. gc2145->supplies[i].supply = gc2145_supply_name[i];
  958. return devm_regulator_bulk_get(&client->dev, GC2145_NUM_SUPPLIES,
  959. gc2145->supplies);
  960. }
  961. /* Verify chip ID */
  962. static int gc2145_identify_module(struct gc2145 *gc2145)
  963. {
  964. struct i2c_client *client = v4l2_get_subdevdata(&gc2145->sd);
  965. int ret;
  966. u64 chip_id;
  967. ret = cci_read(gc2145->regmap, GC2145_REG_CHIP_ID, &chip_id, NULL);
  968. if (ret) {
  969. dev_err(&client->dev, "failed to read chip id (%d)\n", ret);
  970. return ret;
  971. }
  972. if (chip_id != GC2145_CHIP_ID) {
  973. dev_err(&client->dev, "chip id mismatch: %x!=%llx\n",
  974. GC2145_CHIP_ID, chip_id);
  975. return -EIO;
  976. }
  977. return 0;
  978. }
  979. static const char * const test_pattern_menu[] = {
  980. "Disabled",
  981. "Colored patterns",
  982. "Uniform white",
  983. "Uniform yellow",
  984. "Uniform cyan",
  985. "Uniform green",
  986. "Uniform magenta",
  987. "Uniform red",
  988. "Uniform black",
  989. };
  990. #define GC2145_TEST_PATTERN_ENABLE BIT(0)
  991. #define GC2145_TEST_PATTERN_UXGA BIT(3)
  992. #define GC2145_TEST_UNIFORM BIT(3)
  993. #define GC2145_TEST_WHITE (4 << 4)
  994. #define GC2145_TEST_YELLOW (8 << 4)
  995. #define GC2145_TEST_CYAN (9 << 4)
  996. #define GC2145_TEST_GREEN (6 << 4)
  997. #define GC2145_TEST_MAGENTA (10 << 4)
  998. #define GC2145_TEST_RED (5 << 4)
  999. #define GC2145_TEST_BLACK (0)
  1000. static const u8 test_pattern_val[] = {
  1001. 0,
  1002. GC2145_TEST_PATTERN_ENABLE,
  1003. GC2145_TEST_UNIFORM | GC2145_TEST_WHITE,
  1004. GC2145_TEST_UNIFORM | GC2145_TEST_YELLOW,
  1005. GC2145_TEST_UNIFORM | GC2145_TEST_CYAN,
  1006. GC2145_TEST_UNIFORM | GC2145_TEST_GREEN,
  1007. GC2145_TEST_UNIFORM | GC2145_TEST_MAGENTA,
  1008. GC2145_TEST_UNIFORM | GC2145_TEST_RED,
  1009. GC2145_TEST_UNIFORM | GC2145_TEST_BLACK,
  1010. };
  1011. static const struct v4l2_subdev_video_ops gc2145_video_ops = {
  1012. .s_stream = v4l2_subdev_s_stream_helper,
  1013. };
  1014. static const struct v4l2_subdev_pad_ops gc2145_pad_ops = {
  1015. .enum_mbus_code = gc2145_enum_mbus_code,
  1016. .get_fmt = v4l2_subdev_get_fmt,
  1017. .set_fmt = gc2145_set_pad_format,
  1018. .get_selection = gc2145_get_selection,
  1019. .enum_frame_size = gc2145_enum_frame_size,
  1020. .enable_streams = gc2145_enable_streams,
  1021. .disable_streams = gc2145_disable_streams,
  1022. };
  1023. static const struct v4l2_subdev_ops gc2145_subdev_ops = {
  1024. .video = &gc2145_video_ops,
  1025. .pad = &gc2145_pad_ops,
  1026. };
  1027. static const struct v4l2_subdev_internal_ops gc2145_subdev_internal_ops = {
  1028. .init_state = gc2145_init_state,
  1029. };
  1030. static int gc2145_set_ctrl_test_pattern(struct gc2145 *gc2145, int value)
  1031. {
  1032. int ret = 0;
  1033. if (!value) {
  1034. /* Disable test pattern */
  1035. cci_write(gc2145->regmap, GC2145_REG_DEBUG_MODE2, 0, &ret);
  1036. return cci_write(gc2145->regmap, GC2145_REG_DEBUG_MODE3, 0,
  1037. &ret);
  1038. }
  1039. /* Enable test pattern, colored or uniform */
  1040. cci_write(gc2145->regmap, GC2145_REG_DEBUG_MODE2,
  1041. GC2145_TEST_PATTERN_ENABLE | GC2145_TEST_PATTERN_UXGA, &ret);
  1042. if (!(test_pattern_val[value] & GC2145_TEST_UNIFORM))
  1043. return cci_write(gc2145->regmap, GC2145_REG_DEBUG_MODE3, 0,
  1044. &ret);
  1045. /* Uniform */
  1046. return cci_write(gc2145->regmap, GC2145_REG_DEBUG_MODE3,
  1047. test_pattern_val[value], &ret);
  1048. }
  1049. static int gc2145_s_ctrl(struct v4l2_ctrl *ctrl)
  1050. {
  1051. struct v4l2_subdev *sd = gc2145_ctrl_to_sd(ctrl);
  1052. struct i2c_client *client = v4l2_get_subdevdata(sd);
  1053. struct gc2145 *gc2145 = to_gc2145(sd);
  1054. int ret;
  1055. if (pm_runtime_get_if_in_use(&client->dev) == 0)
  1056. return 0;
  1057. switch (ctrl->id) {
  1058. case V4L2_CID_HBLANK:
  1059. ret = cci_write(gc2145->regmap, GC2145_REG_HBLANK, ctrl->val,
  1060. NULL);
  1061. break;
  1062. case V4L2_CID_VBLANK:
  1063. ret = cci_write(gc2145->regmap, GC2145_REG_VBLANK, ctrl->val,
  1064. NULL);
  1065. break;
  1066. case V4L2_CID_TEST_PATTERN:
  1067. ret = gc2145_set_ctrl_test_pattern(gc2145, ctrl->val);
  1068. break;
  1069. case V4L2_CID_HFLIP:
  1070. ret = cci_update_bits(gc2145->regmap, GC2145_REG_ANALOG_MODE1,
  1071. BIT(0), (ctrl->val ? BIT(0) : 0), NULL);
  1072. break;
  1073. case V4L2_CID_VFLIP:
  1074. ret = cci_update_bits(gc2145->regmap, GC2145_REG_ANALOG_MODE1,
  1075. BIT(1), (ctrl->val ? BIT(1) : 0), NULL);
  1076. break;
  1077. default:
  1078. ret = -EINVAL;
  1079. break;
  1080. }
  1081. pm_runtime_put_autosuspend(&client->dev);
  1082. return ret;
  1083. }
  1084. static const struct v4l2_ctrl_ops gc2145_ctrl_ops = {
  1085. .s_ctrl = gc2145_s_ctrl,
  1086. };
  1087. /* Initialize control handlers */
  1088. static int gc2145_init_controls(struct gc2145 *gc2145)
  1089. {
  1090. struct i2c_client *client = v4l2_get_subdevdata(&gc2145->sd);
  1091. const struct v4l2_ctrl_ops *ops = &gc2145_ctrl_ops;
  1092. struct gc2145_ctrls *ctrls = &gc2145->ctrls;
  1093. struct v4l2_ctrl_handler *hdl = &ctrls->handler;
  1094. struct v4l2_fwnode_device_properties props;
  1095. int ret;
  1096. ret = v4l2_ctrl_handler_init(hdl, 12);
  1097. if (ret)
  1098. return ret;
  1099. ctrls->pixel_rate = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_PIXEL_RATE,
  1100. GC2145_640_480_PIXELRATE,
  1101. GC2145_1600_1200_PIXELRATE, 1,
  1102. supported_modes[0].pixel_rate);
  1103. ctrls->link_freq = v4l2_ctrl_new_int_menu(hdl, ops, V4L2_CID_LINK_FREQ,
  1104. ARRAY_SIZE(gc2145_link_freq_menu) - 1,
  1105. 0, gc2145_link_freq_menu);
  1106. if (ctrls->link_freq)
  1107. ctrls->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
  1108. ctrls->hblank = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HBLANK,
  1109. 0, 0xfff, 1, GC2145_640_480_HBLANK);
  1110. ctrls->vblank = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_VBLANK,
  1111. 0, 0x1fff, 1, GC2145_640_480_VBLANK);
  1112. ctrls->test_pattern =
  1113. v4l2_ctrl_new_std_menu_items(hdl, ops, V4L2_CID_TEST_PATTERN,
  1114. ARRAY_SIZE(test_pattern_menu) - 1,
  1115. 0, 0, test_pattern_menu);
  1116. ctrls->hflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HFLIP,
  1117. 0, 1, 1, 0);
  1118. ctrls->vflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_VFLIP,
  1119. 0, 1, 1, 0);
  1120. if (hdl->error) {
  1121. ret = hdl->error;
  1122. dev_err(&client->dev, "control init failed (%d)\n", ret);
  1123. goto error;
  1124. }
  1125. ret = v4l2_fwnode_device_parse(&client->dev, &props);
  1126. if (ret)
  1127. goto error;
  1128. ret = v4l2_ctrl_new_fwnode_properties(hdl, &gc2145_ctrl_ops,
  1129. &props);
  1130. if (ret)
  1131. goto error;
  1132. gc2145->sd.ctrl_handler = hdl;
  1133. return 0;
  1134. error:
  1135. v4l2_ctrl_handler_free(hdl);
  1136. return ret;
  1137. }
  1138. static int gc2145_check_hwcfg(struct device *dev)
  1139. {
  1140. struct fwnode_handle *endpoint;
  1141. struct v4l2_fwnode_endpoint ep_cfg = {
  1142. .bus_type = V4L2_MBUS_CSI2_DPHY
  1143. };
  1144. int ret;
  1145. endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(dev), NULL);
  1146. if (!endpoint) {
  1147. dev_err(dev, "endpoint node not found\n");
  1148. return -EINVAL;
  1149. }
  1150. ret = v4l2_fwnode_endpoint_alloc_parse(endpoint, &ep_cfg);
  1151. fwnode_handle_put(endpoint);
  1152. if (ret)
  1153. return ret;
  1154. /* Check the number of MIPI CSI2 data lanes */
  1155. if (ep_cfg.bus.mipi_csi2.num_data_lanes != 2) {
  1156. dev_err(dev, "only 2 data lanes are currently supported\n");
  1157. ret = -EINVAL;
  1158. goto out;
  1159. }
  1160. /* Check the link frequency set in device tree */
  1161. if (!ep_cfg.nr_of_link_frequencies) {
  1162. dev_err(dev, "link-frequency property not found in DT\n");
  1163. ret = -EINVAL;
  1164. goto out;
  1165. }
  1166. if (ep_cfg.nr_of_link_frequencies != 3 ||
  1167. ep_cfg.link_frequencies[0] != GC2145_640_480_LINKFREQ ||
  1168. ep_cfg.link_frequencies[1] != GC2145_1280_720_LINKFREQ ||
  1169. ep_cfg.link_frequencies[2] != GC2145_1600_1200_LINKFREQ) {
  1170. dev_err(dev, "Invalid link-frequencies provided\n");
  1171. ret = -EINVAL;
  1172. }
  1173. out:
  1174. v4l2_fwnode_endpoint_free(&ep_cfg);
  1175. return ret;
  1176. }
  1177. static int gc2145_probe(struct i2c_client *client)
  1178. {
  1179. struct device *dev = &client->dev;
  1180. unsigned int xclk_freq;
  1181. struct gc2145 *gc2145;
  1182. int ret;
  1183. gc2145 = devm_kzalloc(&client->dev, sizeof(*gc2145), GFP_KERNEL);
  1184. if (!gc2145)
  1185. return -ENOMEM;
  1186. v4l2_i2c_subdev_init(&gc2145->sd, client, &gc2145_subdev_ops);
  1187. gc2145->sd.internal_ops = &gc2145_subdev_internal_ops;
  1188. /* Check the hardware configuration in device tree */
  1189. if (gc2145_check_hwcfg(dev))
  1190. return -EINVAL;
  1191. /* Get system clock (xclk) */
  1192. gc2145->xclk = devm_v4l2_sensor_clk_get(dev, NULL);
  1193. if (IS_ERR(gc2145->xclk))
  1194. return dev_err_probe(dev, PTR_ERR(gc2145->xclk),
  1195. "failed to get xclk\n");
  1196. xclk_freq = clk_get_rate(gc2145->xclk);
  1197. if (xclk_freq != GC2145_XCLK_FREQ) {
  1198. dev_err(dev, "xclk frequency not supported: %d Hz\n",
  1199. xclk_freq);
  1200. return -EINVAL;
  1201. }
  1202. ret = gc2145_get_regulators(gc2145);
  1203. if (ret)
  1204. return dev_err_probe(dev, ret,
  1205. "failed to get regulators\n");
  1206. /* Request optional reset pin */
  1207. gc2145->reset_gpio = devm_gpiod_get_optional(dev, "reset",
  1208. GPIOD_OUT_HIGH);
  1209. if (IS_ERR(gc2145->reset_gpio))
  1210. return dev_err_probe(dev, PTR_ERR(gc2145->reset_gpio),
  1211. "failed to get reset_gpio\n");
  1212. /* Request optional powerdown pin */
  1213. gc2145->powerdown_gpio = devm_gpiod_get_optional(dev, "powerdown",
  1214. GPIOD_OUT_HIGH);
  1215. if (IS_ERR(gc2145->powerdown_gpio))
  1216. return dev_err_probe(dev, PTR_ERR(gc2145->powerdown_gpio),
  1217. "failed to get powerdown_gpio\n");
  1218. /* Initialise the regmap for further cci access */
  1219. gc2145->regmap = devm_cci_regmap_init_i2c(client, 8);
  1220. if (IS_ERR(gc2145->regmap))
  1221. return dev_err_probe(dev, PTR_ERR(gc2145->regmap),
  1222. "failed to get cci regmap\n");
  1223. /*
  1224. * The sensor must be powered for gc2145_identify_module()
  1225. * to be able to read the CHIP_ID register
  1226. */
  1227. ret = gc2145_power_on(dev);
  1228. if (ret)
  1229. return ret;
  1230. ret = gc2145_identify_module(gc2145);
  1231. if (ret)
  1232. goto error_power_off;
  1233. /* Set default mode */
  1234. gc2145->mode = &supported_modes[0];
  1235. ret = gc2145_init_controls(gc2145);
  1236. if (ret)
  1237. goto error_power_off;
  1238. /* Initialize subdev */
  1239. gc2145->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  1240. gc2145->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
  1241. /* Initialize source pad */
  1242. gc2145->pad.flags = MEDIA_PAD_FL_SOURCE;
  1243. ret = media_entity_pads_init(&gc2145->sd.entity, 1, &gc2145->pad);
  1244. if (ret) {
  1245. dev_err(dev, "failed to init entity pads: %d\n", ret);
  1246. goto error_handler_free;
  1247. }
  1248. gc2145->sd.state_lock = gc2145->ctrls.handler.lock;
  1249. ret = v4l2_subdev_init_finalize(&gc2145->sd);
  1250. if (ret < 0) {
  1251. dev_err(dev, "subdev init error: %d\n", ret);
  1252. goto error_media_entity;
  1253. }
  1254. /* Enable runtime PM and turn off the device */
  1255. pm_runtime_set_active(dev);
  1256. pm_runtime_get_noresume(&client->dev);
  1257. pm_runtime_enable(dev);
  1258. pm_runtime_set_autosuspend_delay(&client->dev, 1000);
  1259. pm_runtime_use_autosuspend(&client->dev);
  1260. pm_runtime_put_autosuspend(&client->dev);
  1261. ret = v4l2_async_register_subdev_sensor(&gc2145->sd);
  1262. if (ret < 0) {
  1263. dev_err(dev, "failed to register sensor sub-device: %d\n", ret);
  1264. goto error_subdev_cleanup;
  1265. }
  1266. return 0;
  1267. error_subdev_cleanup:
  1268. v4l2_subdev_cleanup(&gc2145->sd);
  1269. pm_runtime_disable(&client->dev);
  1270. pm_runtime_set_suspended(&client->dev);
  1271. error_media_entity:
  1272. media_entity_cleanup(&gc2145->sd.entity);
  1273. error_handler_free:
  1274. v4l2_ctrl_handler_free(&gc2145->ctrls.handler);
  1275. error_power_off:
  1276. gc2145_power_off(dev);
  1277. return ret;
  1278. }
  1279. static void gc2145_remove(struct i2c_client *client)
  1280. {
  1281. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  1282. struct gc2145 *gc2145 = to_gc2145(sd);
  1283. v4l2_subdev_cleanup(sd);
  1284. v4l2_async_unregister_subdev(sd);
  1285. media_entity_cleanup(&sd->entity);
  1286. v4l2_ctrl_handler_free(&gc2145->ctrls.handler);
  1287. pm_runtime_disable(&client->dev);
  1288. if (!pm_runtime_status_suspended(&client->dev))
  1289. gc2145_power_off(&client->dev);
  1290. pm_runtime_set_suspended(&client->dev);
  1291. }
  1292. static const struct of_device_id gc2145_dt_ids[] = {
  1293. { .compatible = "galaxycore,gc2145" },
  1294. { /* sentinel */ }
  1295. };
  1296. MODULE_DEVICE_TABLE(of, gc2145_dt_ids);
  1297. static const struct dev_pm_ops gc2145_pm_ops = {
  1298. RUNTIME_PM_OPS(gc2145_power_off, gc2145_power_on, NULL)
  1299. };
  1300. static struct i2c_driver gc2145_i2c_driver = {
  1301. .driver = {
  1302. .name = "gc2145",
  1303. .of_match_table = gc2145_dt_ids,
  1304. .pm = pm_ptr(&gc2145_pm_ops),
  1305. },
  1306. .probe = gc2145_probe,
  1307. .remove = gc2145_remove,
  1308. };
  1309. module_i2c_driver(gc2145_i2c_driver);
  1310. MODULE_AUTHOR("Alain Volmat <alain.volmat@foss.st.com>");
  1311. MODULE_DESCRIPTION("GalaxyCore GC2145 sensor driver");
  1312. MODULE_LICENSE("GPL");