as3645a.c 23 KB

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