as3645a.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  1. /*
  2. * drivers/media/i2c/as3645a.c - AS3645A and LM3555 flash controllers driver
  3. *
  4. * Copyright (C) 2008-2011 Nokia Corporation
  5. * Copyright (c) 2011, Intel Corporation.
  6. *
  7. * Contact: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * version 2 as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * TODO:
  19. * - Check hardware FSTROBE control when sensor driver add support for this
  20. *
  21. */
  22. #include <linux/delay.h>
  23. #include <linux/i2c.h>
  24. #include <linux/module.h>
  25. #include <linux/mutex.h>
  26. #include <linux/slab.h>
  27. #include <media/i2c/as3645a.h>
  28. #include <media/v4l2-ctrls.h>
  29. #include <media/v4l2-device.h>
  30. #define AS_TIMER_MS_TO_CODE(t) (((t) - 100) / 50)
  31. #define AS_TIMER_CODE_TO_MS(c) (50 * (c) + 100)
  32. /* Register definitions */
  33. /* Read-only Design info register: Reset state: xxxx 0001 */
  34. #define AS_DESIGN_INFO_REG 0x00
  35. #define AS_DESIGN_INFO_FACTORY(x) (((x) >> 4))
  36. #define AS_DESIGN_INFO_MODEL(x) ((x) & 0x0f)
  37. /* Read-only Version control register: Reset state: 0000 0000
  38. * for first engineering samples
  39. */
  40. #define AS_VERSION_CONTROL_REG 0x01
  41. #define AS_VERSION_CONTROL_RFU(x) (((x) >> 4))
  42. #define AS_VERSION_CONTROL_VERSION(x) ((x) & 0x0f)
  43. /* Read / Write (Indicator and timer register): Reset state: 0000 1111 */
  44. #define AS_INDICATOR_AND_TIMER_REG 0x02
  45. #define AS_INDICATOR_AND_TIMER_TIMEOUT_SHIFT 0
  46. #define AS_INDICATOR_AND_TIMER_VREF_SHIFT 4
  47. #define AS_INDICATOR_AND_TIMER_INDICATOR_SHIFT 6
  48. /* Read / Write (Current set register): Reset state: 0110 1001 */
  49. #define AS_CURRENT_SET_REG 0x03
  50. #define AS_CURRENT_ASSIST_LIGHT_SHIFT 0
  51. #define AS_CURRENT_LED_DET_ON (1 << 3)
  52. #define AS_CURRENT_FLASH_CURRENT_SHIFT 4
  53. /* Read / Write (Control register): Reset state: 1011 0100 */
  54. #define AS_CONTROL_REG 0x04
  55. #define AS_CONTROL_MODE_SETTING_SHIFT 0
  56. #define AS_CONTROL_STROBE_ON (1 << 2)
  57. #define AS_CONTROL_OUT_ON (1 << 3)
  58. #define AS_CONTROL_EXT_TORCH_ON (1 << 4)
  59. #define AS_CONTROL_STROBE_TYPE_EDGE (0 << 5)
  60. #define AS_CONTROL_STROBE_TYPE_LEVEL (1 << 5)
  61. #define AS_CONTROL_COIL_PEAK_SHIFT 6
  62. /* Read only (D3 is read / write) (Fault and info): Reset state: 0000 x000 */
  63. #define AS_FAULT_INFO_REG 0x05
  64. #define AS_FAULT_INFO_INDUCTOR_PEAK_LIMIT (1 << 1)
  65. #define AS_FAULT_INFO_INDICATOR_LED (1 << 2)
  66. #define AS_FAULT_INFO_LED_AMOUNT (1 << 3)
  67. #define AS_FAULT_INFO_TIMEOUT (1 << 4)
  68. #define AS_FAULT_INFO_OVER_TEMPERATURE (1 << 5)
  69. #define AS_FAULT_INFO_SHORT_CIRCUIT (1 << 6)
  70. #define AS_FAULT_INFO_OVER_VOLTAGE (1 << 7)
  71. /* Boost register */
  72. #define AS_BOOST_REG 0x0d
  73. #define AS_BOOST_CURRENT_DISABLE (0 << 0)
  74. #define AS_BOOST_CURRENT_ENABLE (1 << 0)
  75. /* Password register is used to unlock boost register writing */
  76. #define AS_PASSWORD_REG 0x0f
  77. #define AS_PASSWORD_UNLOCK_VALUE 0x55
  78. enum as_mode {
  79. AS_MODE_EXT_TORCH = 0 << AS_CONTROL_MODE_SETTING_SHIFT,
  80. AS_MODE_INDICATOR = 1 << AS_CONTROL_MODE_SETTING_SHIFT,
  81. AS_MODE_ASSIST = 2 << AS_CONTROL_MODE_SETTING_SHIFT,
  82. AS_MODE_FLASH = 3 << AS_CONTROL_MODE_SETTING_SHIFT,
  83. };
  84. /*
  85. * struct as3645a
  86. *
  87. * @subdev: V4L2 subdev
  88. * @pdata: Flash platform data
  89. * @power_lock: Protects power_count
  90. * @power_count: Power reference count
  91. * @led_mode: V4L2 flash LED mode
  92. * @timeout: Flash timeout in microseconds
  93. * @flash_current: Flash current (0=200mA ... 15=500mA). Maximum
  94. * values are 400mA for two LEDs and 500mA for one LED.
  95. * @assist_current: Torch/Assist light current (0=20mA, 1=40mA ... 7=160mA)
  96. * @indicator_current: Indicator LED current (0=0mA, 1=2.5mA ... 4=10mA)
  97. * @strobe_source: Flash strobe source (software or external)
  98. */
  99. struct as3645a {
  100. struct v4l2_subdev subdev;
  101. const struct as3645a_platform_data *pdata;
  102. struct mutex power_lock;
  103. int power_count;
  104. /* Controls */
  105. struct v4l2_ctrl_handler ctrls;
  106. enum v4l2_flash_led_mode led_mode;
  107. unsigned int timeout;
  108. u8 flash_current;
  109. u8 assist_current;
  110. u8 indicator_current;
  111. enum v4l2_flash_strobe_source strobe_source;
  112. };
  113. #define to_as3645a(sd) container_of(sd, struct as3645a, subdev)
  114. /* Return negative errno else zero on success */
  115. static int as3645a_write(struct as3645a *flash, u8 addr, u8 val)
  116. {
  117. struct i2c_client *client = v4l2_get_subdevdata(&flash->subdev);
  118. int rval;
  119. rval = i2c_smbus_write_byte_data(client, addr, val);
  120. dev_dbg(&client->dev, "Write Addr:%02X Val:%02X %s\n", addr, val,
  121. rval < 0 ? "fail" : "ok");
  122. return rval;
  123. }
  124. /* Return negative errno else a data byte received from the device. */
  125. static int as3645a_read(struct as3645a *flash, u8 addr)
  126. {
  127. struct i2c_client *client = v4l2_get_subdevdata(&flash->subdev);
  128. int rval;
  129. rval = i2c_smbus_read_byte_data(client, addr);
  130. dev_dbg(&client->dev, "Read Addr:%02X Val:%02X %s\n", addr, rval,
  131. rval < 0 ? "fail" : "ok");
  132. return rval;
  133. }
  134. /* -----------------------------------------------------------------------------
  135. * Hardware configuration and trigger
  136. */
  137. /*
  138. * as3645a_set_config - Set flash configuration registers
  139. * @flash: The flash
  140. *
  141. * Configure the hardware with flash, assist and indicator currents, as well as
  142. * flash timeout.
  143. *
  144. * Return 0 on success, or a negative error code if an I2C communication error
  145. * occurred.
  146. */
  147. static int as3645a_set_config(struct as3645a *flash)
  148. {
  149. int ret;
  150. u8 val;
  151. val = (flash->flash_current << AS_CURRENT_FLASH_CURRENT_SHIFT)
  152. | (flash->assist_current << AS_CURRENT_ASSIST_LIGHT_SHIFT)
  153. | AS_CURRENT_LED_DET_ON;
  154. ret = as3645a_write(flash, AS_CURRENT_SET_REG, val);
  155. if (ret < 0)
  156. return ret;
  157. val = AS_TIMER_MS_TO_CODE(flash->timeout / 1000)
  158. << AS_INDICATOR_AND_TIMER_TIMEOUT_SHIFT;
  159. val |= (flash->pdata->vref << AS_INDICATOR_AND_TIMER_VREF_SHIFT)
  160. | ((flash->indicator_current ? flash->indicator_current - 1 : 0)
  161. << AS_INDICATOR_AND_TIMER_INDICATOR_SHIFT);
  162. return as3645a_write(flash, AS_INDICATOR_AND_TIMER_REG, val);
  163. }
  164. /*
  165. * as3645a_set_control - Set flash control register
  166. * @flash: The flash
  167. * @mode: Desired output mode
  168. * @on: Desired output state
  169. *
  170. * Configure the hardware with output mode and state.
  171. *
  172. * Return 0 on success, or a negative error code if an I2C communication error
  173. * occurred.
  174. */
  175. static int
  176. as3645a_set_control(struct as3645a *flash, enum as_mode mode, bool on)
  177. {
  178. u8 reg;
  179. /* Configure output parameters and operation mode. */
  180. reg = (flash->pdata->peak << AS_CONTROL_COIL_PEAK_SHIFT)
  181. | (on ? AS_CONTROL_OUT_ON : 0)
  182. | mode;
  183. if (flash->led_mode == V4L2_FLASH_LED_MODE_FLASH &&
  184. flash->strobe_source == V4L2_FLASH_STROBE_SOURCE_EXTERNAL) {
  185. reg |= AS_CONTROL_STROBE_TYPE_LEVEL
  186. | AS_CONTROL_STROBE_ON;
  187. }
  188. return as3645a_write(flash, AS_CONTROL_REG, reg);
  189. }
  190. /*
  191. * as3645a_set_output - Configure output and operation mode
  192. * @flash: Flash controller
  193. * @strobe: Strobe the flash (only valid in flash mode)
  194. *
  195. * Turn the LEDs output on/off and set the operation mode based on the current
  196. * parameters.
  197. *
  198. * The AS3645A can't control the indicator LED independently of the flash/torch
  199. * LED. If the flash controller is in V4L2_FLASH_LED_MODE_NONE mode, set the
  200. * chip to indicator mode. Otherwise set it to assist light (torch) or flash
  201. * mode.
  202. *
  203. * In indicator and assist modes, turn the output on/off based on the indicator
  204. * and torch currents. In software strobe flash mode, turn the output on/off
  205. * based on the strobe parameter.
  206. */
  207. static int as3645a_set_output(struct as3645a *flash, bool strobe)
  208. {
  209. enum as_mode mode;
  210. bool on;
  211. switch (flash->led_mode) {
  212. case V4L2_FLASH_LED_MODE_NONE:
  213. on = flash->indicator_current != 0;
  214. mode = AS_MODE_INDICATOR;
  215. break;
  216. case V4L2_FLASH_LED_MODE_TORCH:
  217. on = true;
  218. mode = AS_MODE_ASSIST;
  219. break;
  220. case V4L2_FLASH_LED_MODE_FLASH:
  221. on = strobe;
  222. mode = AS_MODE_FLASH;
  223. break;
  224. default:
  225. BUG();
  226. }
  227. /* Configure output parameters and operation mode. */
  228. return as3645a_set_control(flash, mode, on);
  229. }
  230. /* -----------------------------------------------------------------------------
  231. * V4L2 controls
  232. */
  233. static int as3645a_is_active(struct as3645a *flash)
  234. {
  235. int ret;
  236. ret = as3645a_read(flash, AS_CONTROL_REG);
  237. return ret < 0 ? ret : !!(ret & AS_CONTROL_OUT_ON);
  238. }
  239. static int as3645a_read_fault(struct as3645a *flash)
  240. {
  241. struct i2c_client *client = v4l2_get_subdevdata(&flash->subdev);
  242. int rval;
  243. /* NOTE: reading register clear fault status */
  244. rval = as3645a_read(flash, AS_FAULT_INFO_REG);
  245. if (rval < 0)
  246. return rval;
  247. if (rval & AS_FAULT_INFO_INDUCTOR_PEAK_LIMIT)
  248. dev_dbg(&client->dev, "Inductor Peak limit fault\n");
  249. if (rval & AS_FAULT_INFO_INDICATOR_LED)
  250. dev_dbg(&client->dev, "Indicator LED fault: "
  251. "Short circuit or open loop\n");
  252. dev_dbg(&client->dev, "%u connected LEDs\n",
  253. rval & AS_FAULT_INFO_LED_AMOUNT ? 2 : 1);
  254. if (rval & AS_FAULT_INFO_TIMEOUT)
  255. dev_dbg(&client->dev, "Timeout fault\n");
  256. if (rval & AS_FAULT_INFO_OVER_TEMPERATURE)
  257. dev_dbg(&client->dev, "Over temperature fault\n");
  258. if (rval & AS_FAULT_INFO_SHORT_CIRCUIT)
  259. dev_dbg(&client->dev, "Short circuit fault\n");
  260. if (rval & AS_FAULT_INFO_OVER_VOLTAGE)
  261. dev_dbg(&client->dev, "Over voltage fault: "
  262. "Indicates missing capacitor or open connection\n");
  263. return rval;
  264. }
  265. static int as3645a_get_ctrl(struct v4l2_ctrl *ctrl)
  266. {
  267. struct as3645a *flash =
  268. container_of(ctrl->handler, struct as3645a, ctrls);
  269. struct i2c_client *client = v4l2_get_subdevdata(&flash->subdev);
  270. int value;
  271. switch (ctrl->id) {
  272. case V4L2_CID_FLASH_FAULT:
  273. value = as3645a_read_fault(flash);
  274. if (value < 0)
  275. return value;
  276. ctrl->cur.val = 0;
  277. if (value & AS_FAULT_INFO_SHORT_CIRCUIT)
  278. ctrl->cur.val |= V4L2_FLASH_FAULT_SHORT_CIRCUIT;
  279. if (value & AS_FAULT_INFO_OVER_TEMPERATURE)
  280. ctrl->cur.val |= V4L2_FLASH_FAULT_OVER_TEMPERATURE;
  281. if (value & AS_FAULT_INFO_TIMEOUT)
  282. ctrl->cur.val |= V4L2_FLASH_FAULT_TIMEOUT;
  283. if (value & AS_FAULT_INFO_OVER_VOLTAGE)
  284. ctrl->cur.val |= V4L2_FLASH_FAULT_OVER_VOLTAGE;
  285. if (value & AS_FAULT_INFO_INDUCTOR_PEAK_LIMIT)
  286. ctrl->cur.val |= V4L2_FLASH_FAULT_OVER_CURRENT;
  287. if (value & AS_FAULT_INFO_INDICATOR_LED)
  288. ctrl->cur.val |= V4L2_FLASH_FAULT_INDICATOR;
  289. break;
  290. case V4L2_CID_FLASH_STROBE_STATUS:
  291. if (flash->led_mode != V4L2_FLASH_LED_MODE_FLASH) {
  292. ctrl->cur.val = 0;
  293. break;
  294. }
  295. value = as3645a_is_active(flash);
  296. if (value < 0)
  297. return value;
  298. ctrl->cur.val = value;
  299. break;
  300. }
  301. dev_dbg(&client->dev, "G_CTRL %08x:%d\n", ctrl->id, ctrl->cur.val);
  302. return 0;
  303. }
  304. static int as3645a_set_ctrl(struct v4l2_ctrl *ctrl)
  305. {
  306. struct as3645a *flash =
  307. container_of(ctrl->handler, struct as3645a, ctrls);
  308. struct i2c_client *client = v4l2_get_subdevdata(&flash->subdev);
  309. int ret;
  310. dev_dbg(&client->dev, "S_CTRL %08x:%d\n", ctrl->id, ctrl->val);
  311. /* If a control that doesn't apply to the current mode is modified,
  312. * we store the value and return immediately. The setting will be
  313. * applied when the LED mode is changed. Otherwise we apply the setting
  314. * immediately.
  315. */
  316. switch (ctrl->id) {
  317. case V4L2_CID_FLASH_LED_MODE:
  318. if (flash->indicator_current)
  319. return -EBUSY;
  320. ret = as3645a_set_config(flash);
  321. if (ret < 0)
  322. return ret;
  323. flash->led_mode = ctrl->val;
  324. return as3645a_set_output(flash, false);
  325. case V4L2_CID_FLASH_STROBE_SOURCE:
  326. flash->strobe_source = ctrl->val;
  327. /* Applies to flash mode only. */
  328. if (flash->led_mode != V4L2_FLASH_LED_MODE_FLASH)
  329. break;
  330. return as3645a_set_output(flash, false);
  331. case V4L2_CID_FLASH_STROBE:
  332. if (flash->led_mode != V4L2_FLASH_LED_MODE_FLASH)
  333. return -EBUSY;
  334. return as3645a_set_output(flash, true);
  335. case V4L2_CID_FLASH_STROBE_STOP:
  336. if (flash->led_mode != V4L2_FLASH_LED_MODE_FLASH)
  337. return -EBUSY;
  338. return as3645a_set_output(flash, false);
  339. case V4L2_CID_FLASH_TIMEOUT:
  340. flash->timeout = ctrl->val;
  341. /* Applies to flash mode only. */
  342. if (flash->led_mode != V4L2_FLASH_LED_MODE_FLASH)
  343. break;
  344. return as3645a_set_config(flash);
  345. case V4L2_CID_FLASH_INTENSITY:
  346. flash->flash_current = (ctrl->val - AS3645A_FLASH_INTENSITY_MIN)
  347. / AS3645A_FLASH_INTENSITY_STEP;
  348. /* Applies to flash mode only. */
  349. if (flash->led_mode != V4L2_FLASH_LED_MODE_FLASH)
  350. break;
  351. return as3645a_set_config(flash);
  352. case V4L2_CID_FLASH_TORCH_INTENSITY:
  353. flash->assist_current =
  354. (ctrl->val - AS3645A_TORCH_INTENSITY_MIN)
  355. / AS3645A_TORCH_INTENSITY_STEP;
  356. /* Applies to torch mode only. */
  357. if (flash->led_mode != V4L2_FLASH_LED_MODE_TORCH)
  358. break;
  359. return as3645a_set_config(flash);
  360. case V4L2_CID_FLASH_INDICATOR_INTENSITY:
  361. if (flash->led_mode != V4L2_FLASH_LED_MODE_NONE)
  362. return -EBUSY;
  363. flash->indicator_current =
  364. (ctrl->val - AS3645A_INDICATOR_INTENSITY_MIN)
  365. / AS3645A_INDICATOR_INTENSITY_STEP;
  366. ret = as3645a_set_config(flash);
  367. if (ret < 0)
  368. return ret;
  369. if ((ctrl->val == 0) == (ctrl->cur.val == 0))
  370. break;
  371. return as3645a_set_output(flash, false);
  372. }
  373. return 0;
  374. }
  375. static const struct v4l2_ctrl_ops as3645a_ctrl_ops = {
  376. .g_volatile_ctrl = as3645a_get_ctrl,
  377. .s_ctrl = as3645a_set_ctrl,
  378. };
  379. /* -----------------------------------------------------------------------------
  380. * V4L2 subdev core operations
  381. */
  382. /* Put device into know state. */
  383. static int as3645a_setup(struct as3645a *flash)
  384. {
  385. struct i2c_client *client = v4l2_get_subdevdata(&flash->subdev);
  386. int ret;
  387. /* clear errors */
  388. ret = as3645a_read(flash, AS_FAULT_INFO_REG);
  389. if (ret < 0)
  390. return ret;
  391. dev_dbg(&client->dev, "Fault info: %02x\n", ret);
  392. ret = as3645a_set_config(flash);
  393. if (ret < 0)
  394. return ret;
  395. ret = as3645a_set_output(flash, false);
  396. if (ret < 0)
  397. return ret;
  398. /* read status */
  399. ret = as3645a_read_fault(flash);
  400. if (ret < 0)
  401. return ret;
  402. dev_dbg(&client->dev, "AS_INDICATOR_AND_TIMER_REG: %02x\n",
  403. as3645a_read(flash, AS_INDICATOR_AND_TIMER_REG));
  404. dev_dbg(&client->dev, "AS_CURRENT_SET_REG: %02x\n",
  405. as3645a_read(flash, AS_CURRENT_SET_REG));
  406. dev_dbg(&client->dev, "AS_CONTROL_REG: %02x\n",
  407. as3645a_read(flash, AS_CONTROL_REG));
  408. return ret & ~AS_FAULT_INFO_LED_AMOUNT ? -EIO : 0;
  409. }
  410. static int __as3645a_set_power(struct as3645a *flash, int on)
  411. {
  412. int ret;
  413. if (!on)
  414. as3645a_set_control(flash, AS_MODE_EXT_TORCH, false);
  415. if (flash->pdata->set_power) {
  416. ret = flash->pdata->set_power(&flash->subdev, on);
  417. if (ret < 0)
  418. return ret;
  419. }
  420. if (!on)
  421. return 0;
  422. ret = as3645a_setup(flash);
  423. if (ret < 0) {
  424. if (flash->pdata->set_power)
  425. flash->pdata->set_power(&flash->subdev, 0);
  426. }
  427. return ret;
  428. }
  429. static int as3645a_set_power(struct v4l2_subdev *sd, int on)
  430. {
  431. struct as3645a *flash = to_as3645a(sd);
  432. int ret = 0;
  433. mutex_lock(&flash->power_lock);
  434. if (flash->power_count == !on) {
  435. ret = __as3645a_set_power(flash, !!on);
  436. if (ret < 0)
  437. goto done;
  438. }
  439. flash->power_count += on ? 1 : -1;
  440. WARN_ON(flash->power_count < 0);
  441. done:
  442. mutex_unlock(&flash->power_lock);
  443. return ret;
  444. }
  445. static int as3645a_registered(struct v4l2_subdev *sd)
  446. {
  447. struct as3645a *flash = to_as3645a(sd);
  448. struct i2c_client *client = v4l2_get_subdevdata(sd);
  449. int rval, man, model, rfu, version;
  450. const char *vendor;
  451. /* Power up the flash driver and read manufacturer ID, model ID, RFU
  452. * and version.
  453. */
  454. rval = as3645a_set_power(&flash->subdev, 1);
  455. if (rval < 0)
  456. return rval;
  457. rval = as3645a_read(flash, AS_DESIGN_INFO_REG);
  458. if (rval < 0)
  459. goto power_off;
  460. man = AS_DESIGN_INFO_FACTORY(rval);
  461. model = AS_DESIGN_INFO_MODEL(rval);
  462. rval = as3645a_read(flash, AS_VERSION_CONTROL_REG);
  463. if (rval < 0)
  464. goto power_off;
  465. rfu = AS_VERSION_CONTROL_RFU(rval);
  466. version = AS_VERSION_CONTROL_VERSION(rval);
  467. /* Verify the chip model and version. */
  468. if (model != 0x01 || rfu != 0x00) {
  469. dev_err(&client->dev, "AS3645A not detected "
  470. "(model %d rfu %d)\n", model, rfu);
  471. rval = -ENODEV;
  472. goto power_off;
  473. }
  474. switch (man) {
  475. case 1:
  476. vendor = "AMS, Austria Micro Systems";
  477. break;
  478. case 2:
  479. vendor = "ADI, Analog Devices Inc.";
  480. break;
  481. case 3:
  482. vendor = "NSC, National Semiconductor";
  483. break;
  484. case 4:
  485. vendor = "NXP";
  486. break;
  487. case 5:
  488. vendor = "TI, Texas Instrument";
  489. break;
  490. default:
  491. vendor = "Unknown";
  492. }
  493. dev_info(&client->dev, "Chip vendor: %s (%d) Version: %d\n", vendor,
  494. man, version);
  495. rval = as3645a_write(flash, AS_PASSWORD_REG, AS_PASSWORD_UNLOCK_VALUE);
  496. if (rval < 0)
  497. goto power_off;
  498. rval = as3645a_write(flash, AS_BOOST_REG, AS_BOOST_CURRENT_DISABLE);
  499. if (rval < 0)
  500. goto power_off;
  501. /* Setup default values. This makes sure that the chip is in a known
  502. * state, in case the power rail can't be controlled.
  503. */
  504. rval = as3645a_setup(flash);
  505. power_off:
  506. as3645a_set_power(&flash->subdev, 0);
  507. return rval;
  508. }
  509. static int as3645a_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
  510. {
  511. return as3645a_set_power(sd, 1);
  512. }
  513. static int as3645a_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
  514. {
  515. return as3645a_set_power(sd, 0);
  516. }
  517. static const struct v4l2_subdev_core_ops as3645a_core_ops = {
  518. .s_power = as3645a_set_power,
  519. };
  520. static const struct v4l2_subdev_ops as3645a_ops = {
  521. .core = &as3645a_core_ops,
  522. };
  523. static const struct v4l2_subdev_internal_ops as3645a_internal_ops = {
  524. .registered = as3645a_registered,
  525. .open = as3645a_open,
  526. .close = as3645a_close,
  527. };
  528. /* -----------------------------------------------------------------------------
  529. * I2C driver
  530. */
  531. #ifdef CONFIG_PM
  532. static int as3645a_suspend(struct device *dev)
  533. {
  534. struct i2c_client *client = to_i2c_client(dev);
  535. struct v4l2_subdev *subdev = i2c_get_clientdata(client);
  536. struct as3645a *flash = to_as3645a(subdev);
  537. int rval;
  538. if (flash->power_count == 0)
  539. return 0;
  540. rval = __as3645a_set_power(flash, 0);
  541. dev_dbg(&client->dev, "Suspend %s\n", rval < 0 ? "failed" : "ok");
  542. return rval;
  543. }
  544. static int as3645a_resume(struct device *dev)
  545. {
  546. struct i2c_client *client = to_i2c_client(dev);
  547. struct v4l2_subdev *subdev = i2c_get_clientdata(client);
  548. struct as3645a *flash = to_as3645a(subdev);
  549. int rval;
  550. if (flash->power_count == 0)
  551. return 0;
  552. rval = __as3645a_set_power(flash, 1);
  553. dev_dbg(&client->dev, "Resume %s\n", rval < 0 ? "fail" : "ok");
  554. return rval;
  555. }
  556. #else
  557. #define as3645a_suspend NULL
  558. #define as3645a_resume NULL
  559. #endif /* CONFIG_PM */
  560. /*
  561. * as3645a_init_controls - Create controls
  562. * @flash: The flash
  563. *
  564. * The number of LEDs reported in platform data is used to compute default
  565. * limits. Parameters passed through platform data can override those limits.
  566. */
  567. static int as3645a_init_controls(struct as3645a *flash)
  568. {
  569. const struct as3645a_platform_data *pdata = flash->pdata;
  570. struct v4l2_ctrl *ctrl;
  571. int maximum;
  572. v4l2_ctrl_handler_init(&flash->ctrls, 10);
  573. /* V4L2_CID_FLASH_LED_MODE */
  574. v4l2_ctrl_new_std_menu(&flash->ctrls, &as3645a_ctrl_ops,
  575. V4L2_CID_FLASH_LED_MODE, 2, ~7,
  576. V4L2_FLASH_LED_MODE_NONE);
  577. /* V4L2_CID_FLASH_STROBE_SOURCE */
  578. v4l2_ctrl_new_std_menu(&flash->ctrls, &as3645a_ctrl_ops,
  579. V4L2_CID_FLASH_STROBE_SOURCE,
  580. pdata->ext_strobe ? 1 : 0,
  581. pdata->ext_strobe ? ~3 : ~1,
  582. V4L2_FLASH_STROBE_SOURCE_SOFTWARE);
  583. flash->strobe_source = V4L2_FLASH_STROBE_SOURCE_SOFTWARE;
  584. /* V4L2_CID_FLASH_STROBE */
  585. v4l2_ctrl_new_std(&flash->ctrls, &as3645a_ctrl_ops,
  586. V4L2_CID_FLASH_STROBE, 0, 0, 0, 0);
  587. /* V4L2_CID_FLASH_STROBE_STOP */
  588. v4l2_ctrl_new_std(&flash->ctrls, &as3645a_ctrl_ops,
  589. V4L2_CID_FLASH_STROBE_STOP, 0, 0, 0, 0);
  590. /* V4L2_CID_FLASH_STROBE_STATUS */
  591. ctrl = v4l2_ctrl_new_std(&flash->ctrls, &as3645a_ctrl_ops,
  592. V4L2_CID_FLASH_STROBE_STATUS, 0, 1, 1, 1);
  593. if (ctrl != NULL)
  594. ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
  595. /* V4L2_CID_FLASH_TIMEOUT */
  596. maximum = pdata->timeout_max;
  597. v4l2_ctrl_new_std(&flash->ctrls, &as3645a_ctrl_ops,
  598. V4L2_CID_FLASH_TIMEOUT, AS3645A_FLASH_TIMEOUT_MIN,
  599. maximum, AS3645A_FLASH_TIMEOUT_STEP, maximum);
  600. flash->timeout = maximum;
  601. /* V4L2_CID_FLASH_INTENSITY */
  602. maximum = pdata->flash_max_current;
  603. v4l2_ctrl_new_std(&flash->ctrls, &as3645a_ctrl_ops,
  604. V4L2_CID_FLASH_INTENSITY, AS3645A_FLASH_INTENSITY_MIN,
  605. maximum, AS3645A_FLASH_INTENSITY_STEP, maximum);
  606. flash->flash_current = (maximum - AS3645A_FLASH_INTENSITY_MIN)
  607. / AS3645A_FLASH_INTENSITY_STEP;
  608. /* V4L2_CID_FLASH_TORCH_INTENSITY */
  609. maximum = pdata->torch_max_current;
  610. v4l2_ctrl_new_std(&flash->ctrls, &as3645a_ctrl_ops,
  611. V4L2_CID_FLASH_TORCH_INTENSITY,
  612. AS3645A_TORCH_INTENSITY_MIN, maximum,
  613. AS3645A_TORCH_INTENSITY_STEP,
  614. AS3645A_TORCH_INTENSITY_MIN);
  615. flash->assist_current = 0;
  616. /* V4L2_CID_FLASH_INDICATOR_INTENSITY */
  617. v4l2_ctrl_new_std(&flash->ctrls, &as3645a_ctrl_ops,
  618. V4L2_CID_FLASH_INDICATOR_INTENSITY,
  619. AS3645A_INDICATOR_INTENSITY_MIN,
  620. AS3645A_INDICATOR_INTENSITY_MAX,
  621. AS3645A_INDICATOR_INTENSITY_STEP,
  622. AS3645A_INDICATOR_INTENSITY_MIN);
  623. flash->indicator_current = 0;
  624. /* V4L2_CID_FLASH_FAULT */
  625. ctrl = v4l2_ctrl_new_std(&flash->ctrls, &as3645a_ctrl_ops,
  626. V4L2_CID_FLASH_FAULT, 0,
  627. V4L2_FLASH_FAULT_OVER_VOLTAGE |
  628. V4L2_FLASH_FAULT_TIMEOUT |
  629. V4L2_FLASH_FAULT_OVER_TEMPERATURE |
  630. V4L2_FLASH_FAULT_SHORT_CIRCUIT, 0, 0);
  631. if (ctrl != NULL)
  632. ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
  633. flash->subdev.ctrl_handler = &flash->ctrls;
  634. return flash->ctrls.error;
  635. }
  636. static int as3645a_probe(struct i2c_client *client,
  637. const struct i2c_device_id *devid)
  638. {
  639. struct as3645a *flash;
  640. int ret;
  641. if (client->dev.platform_data == NULL)
  642. return -ENODEV;
  643. flash = devm_kzalloc(&client->dev, sizeof(*flash), GFP_KERNEL);
  644. if (flash == NULL)
  645. return -ENOMEM;
  646. flash->pdata = client->dev.platform_data;
  647. v4l2_i2c_subdev_init(&flash->subdev, client, &as3645a_ops);
  648. flash->subdev.internal_ops = &as3645a_internal_ops;
  649. flash->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  650. ret = as3645a_init_controls(flash);
  651. if (ret < 0)
  652. goto done;
  653. ret = media_entity_pads_init(&flash->subdev.entity, 0, NULL);
  654. if (ret < 0)
  655. goto done;
  656. flash->subdev.entity.function = MEDIA_ENT_F_FLASH;
  657. mutex_init(&flash->power_lock);
  658. flash->led_mode = V4L2_FLASH_LED_MODE_NONE;
  659. done:
  660. if (ret < 0)
  661. v4l2_ctrl_handler_free(&flash->ctrls);
  662. return ret;
  663. }
  664. static int as3645a_remove(struct i2c_client *client)
  665. {
  666. struct v4l2_subdev *subdev = i2c_get_clientdata(client);
  667. struct as3645a *flash = to_as3645a(subdev);
  668. v4l2_device_unregister_subdev(subdev);
  669. v4l2_ctrl_handler_free(&flash->ctrls);
  670. media_entity_cleanup(&flash->subdev.entity);
  671. mutex_destroy(&flash->power_lock);
  672. return 0;
  673. }
  674. static const struct i2c_device_id as3645a_id_table[] = {
  675. { AS3645A_NAME, 0 },
  676. { },
  677. };
  678. MODULE_DEVICE_TABLE(i2c, as3645a_id_table);
  679. static const struct dev_pm_ops as3645a_pm_ops = {
  680. .suspend = as3645a_suspend,
  681. .resume = as3645a_resume,
  682. };
  683. static struct i2c_driver as3645a_i2c_driver = {
  684. .driver = {
  685. .name = AS3645A_NAME,
  686. .pm = &as3645a_pm_ops,
  687. },
  688. .probe = as3645a_probe,
  689. .remove = as3645a_remove,
  690. .id_table = as3645a_id_table,
  691. };
  692. module_i2c_driver(as3645a_i2c_driver);
  693. MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
  694. MODULE_DESCRIPTION("LED flash driver for AS3645A, LM3555 and their clones");
  695. MODULE_LICENSE("GPL");