mpu3050.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996
  1. /*
  2. * MPU3050 Tri-axis gyroscope driver
  3. *
  4. * Copyright (C) 2011 Wistron Co.Ltd
  5. * Joseph Lai <joseph_lai@wistron.com>
  6. *
  7. * Trimmed down by Alan Cox <alan@linux.intel.com> to produce this version
  8. *
  9. * This is a 'lite' version of the driver, while we consider the right way
  10. * to present the other features to user space. In particular it requires the
  11. * device has an IRQ, and it only provides an input interface, so is not much
  12. * use for device orientation. A fuller version is available from the Meego
  13. * tree.
  14. *
  15. * This program is based on bma023.c.
  16. *
  17. * This program is free software; you can redistribute it and/or modify
  18. * it under the terms of the GNU General Public License as published by
  19. * the Free Software Foundation; version 2 of the License.
  20. *
  21. * This program is distributed in the hope that it will be useful, but
  22. * WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  24. * General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License along
  27. * with this program; if not, write to the Free Software Foundation, Inc.,
  28. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  29. *
  30. */
  31. #include <linux/module.h>
  32. #include <linux/init.h>
  33. #include <linux/interrupt.h>
  34. #include <linux/platform_device.h>
  35. #include <linux/mutex.h>
  36. #include <linux/err.h>
  37. #include <linux/i2c.h>
  38. #include <linux/input.h>
  39. #include <linux/sensors.h>
  40. #include <linux/delay.h>
  41. #include <linux/slab.h>
  42. #include <linux/pm_runtime.h>
  43. #include <linux/gpio.h>
  44. #include <linux/input/mpu3050.h>
  45. #include <linux/regulator/consumer.h>
  46. #include <linux/of_gpio.h>
  47. #include <mach/gpiomux.h>
  48. #define MPU3050_AUTO_DELAY 1000
  49. #define MPU3050_MIN_VALUE -32768
  50. #define MPU3050_MAX_VALUE 32767
  51. #define MPU3050_MIN_POLL_INTERVAL 1
  52. #define MPU3050_MAX_POLL_INTERVAL 250
  53. #define MPU3050_DEFAULT_POLL_INTERVAL 200
  54. #define MPU3050_DEFAULT_FS_RANGE 3
  55. /* Register map */
  56. #define MPU3050_CHIP_ID_REG 0x00
  57. #define MPU3050_SMPLRT_DIV 0x15
  58. #define MPU3050_DLPF_FS_SYNC 0x16
  59. #define MPU3050_INT_CFG 0x17
  60. #define MPU3050_XOUT_H 0x1D
  61. #define MPU3050_PWR_MGM 0x3E
  62. #define MPU3050_PWR_MGM_POS 6
  63. /* Register bits */
  64. /* DLPF_FS_SYNC */
  65. #define MPU3050_EXT_SYNC_NONE 0x00
  66. #define MPU3050_EXT_SYNC_TEMP 0x20
  67. #define MPU3050_EXT_SYNC_GYROX 0x40
  68. #define MPU3050_EXT_SYNC_GYROY 0x60
  69. #define MPU3050_EXT_SYNC_GYROZ 0x80
  70. #define MPU3050_EXT_SYNC_ACCELX 0xA0
  71. #define MPU3050_EXT_SYNC_ACCELY 0xC0
  72. #define MPU3050_EXT_SYNC_ACCELZ 0xE0
  73. #define MPU3050_EXT_SYNC_MASK 0xE0
  74. #define MPU3050_FS_250DPS 0x00
  75. #define MPU3050_FS_500DPS 0x08
  76. #define MPU3050_FS_1000DPS 0x10
  77. #define MPU3050_FS_2000DPS 0x18
  78. #define MPU3050_FS_MASK 0x18
  79. #define MPU3050_DLPF_CFG_256HZ_NOLPF2 0x00
  80. #define MPU3050_DLPF_CFG_188HZ 0x01
  81. #define MPU3050_DLPF_CFG_98HZ 0x02
  82. #define MPU3050_DLPF_CFG_42HZ 0x03
  83. #define MPU3050_DLPF_CFG_20HZ 0x04
  84. #define MPU3050_DLPF_CFG_10HZ 0x05
  85. #define MPU3050_DLPF_CFG_5HZ 0x06
  86. #define MPU3050_DLPF_CFG_2100HZ_NOLPF 0x07
  87. #define MPU3050_DLPF_CFG_MASK 0x07
  88. /* INT_CFG */
  89. #define MPU3050_RAW_RDY_EN 0x01
  90. #define MPU3050_MPU_RDY_EN 0x04
  91. #define MPU3050_LATCH_INT_EN 0x20
  92. #define MPU3050_OPEN_DRAIN 0x40
  93. #define MPU3050_ACTIVE_LOW 0x80
  94. /* PWR_MGM */
  95. #define MPU3050_PWR_MGM_PLL_X 0x01
  96. #define MPU3050_PWR_MGM_PLL_Y 0x02
  97. #define MPU3050_PWR_MGM_PLL_Z 0x03
  98. #define MPU3050_PWR_MGM_CLKSEL 0x07
  99. #define MPU3050_PWR_MGM_STBY_ZG 0x08
  100. #define MPU3050_PWR_MGM_STBY_YG 0x10
  101. #define MPU3050_PWR_MGM_STBY_XG 0x20
  102. #define MPU3050_PWR_MGM_SLEEP 0x40
  103. #define MPU3050_PWR_MGM_RESET 0x80
  104. #define MPU3050_PWR_MGM_MASK 0x40
  105. struct axis_data {
  106. s16 x;
  107. s16 y;
  108. s16 z;
  109. };
  110. struct mpu3050_sensor {
  111. struct i2c_client *client;
  112. struct device *dev;
  113. struct input_dev *idev;
  114. struct mpu3050_gyro_platform_data *platform_data;
  115. struct delayed_work input_work;
  116. struct sensors_classdev cdev;
  117. u32 use_poll;
  118. u32 poll_interval;
  119. u32 dlpf_index;
  120. u32 enable_gpio;
  121. u32 enable;
  122. };
  123. static struct sensors_classdev sensors_cdev = {
  124. .name = "mpu3050-gyro",
  125. .vendor = "Invensense",
  126. .version = 1,
  127. .handle = SENSORS_GYROSCOPE_HANDLE,
  128. .type = SENSOR_TYPE_GYROSCOPE,
  129. .max_range = "35.0",
  130. .resolution = "0.06",
  131. .sensor_power = "0.2",
  132. .min_delay = 2000,
  133. .fifo_reserved_event_count = 0,
  134. .fifo_max_event_count = 0,
  135. .enabled = 0,
  136. .delay_msec = MPU3050_DEFAULT_POLL_INTERVAL,
  137. .sensors_enable = NULL,
  138. .sensors_poll_delay = NULL,
  139. };
  140. struct sensor_regulator {
  141. struct regulator *vreg;
  142. const char *name;
  143. u32 min_uV;
  144. u32 max_uV;
  145. };
  146. struct sensor_regulator mpu_vreg[] = {
  147. {NULL, "vdd", 2100000, 3600000},
  148. {NULL, "vlogic", 1800000, 1800000},
  149. };
  150. static const int mpu3050_chip_ids[] = {
  151. 0x68,
  152. 0x69,
  153. };
  154. struct dlpf_cfg_tb {
  155. u8 cfg; /* cfg index */
  156. u32 lpf_bw; /* low pass filter bandwidth in Hz */
  157. u32 sample_rate; /* analog sample rate in Khz, 1 or 8 */
  158. };
  159. static struct dlpf_cfg_tb dlpf_table[] = {
  160. {6, 5, 1},
  161. {5, 10, 1},
  162. {4, 20, 1},
  163. {3, 42, 1},
  164. {2, 98, 1},
  165. {1, 188, 1},
  166. {0, 256, 8},
  167. };
  168. static u8 interval_to_dlpf_cfg(u32 interval)
  169. {
  170. u32 sample_rate = 1000 / interval;
  171. u32 i;
  172. /* the filter bandwidth needs to be greater or
  173. * equal to half of the sample rate
  174. */
  175. for (i = 0; i < sizeof(dlpf_table)/sizeof(dlpf_table[0]); i++) {
  176. if (dlpf_table[i].lpf_bw * 2 >= sample_rate)
  177. return i;
  178. }
  179. /* return the maximum possible */
  180. return --i;
  181. }
  182. static int mpu3050_config_regulator(struct i2c_client *client, bool on)
  183. {
  184. int rc = 0, i;
  185. int num_reg = sizeof(mpu_vreg) / sizeof(struct sensor_regulator);
  186. if (on) {
  187. for (i = 0; i < num_reg; i++) {
  188. mpu_vreg[i].vreg = regulator_get(&client->dev,
  189. mpu_vreg[i].name);
  190. if (IS_ERR(mpu_vreg[i].vreg)) {
  191. rc = PTR_ERR(mpu_vreg[i].vreg);
  192. pr_err("%s:regulator get failed rc=%d\n",
  193. __func__, rc);
  194. mpu_vreg[i].vreg = NULL;
  195. goto error_vdd;
  196. }
  197. if (regulator_count_voltages(mpu_vreg[i].vreg) > 0) {
  198. rc = regulator_set_voltage(mpu_vreg[i].vreg,
  199. mpu_vreg[i].min_uV, mpu_vreg[i].max_uV);
  200. if (rc) {
  201. pr_err("%s:set_voltage failed rc=%d\n",
  202. __func__, rc);
  203. regulator_put(mpu_vreg[i].vreg);
  204. mpu_vreg[i].vreg = NULL;
  205. goto error_vdd;
  206. }
  207. }
  208. rc = regulator_enable(mpu_vreg[i].vreg);
  209. if (rc) {
  210. pr_err("%s: regulator_enable failed rc =%d\n",
  211. __func__,
  212. rc);
  213. if (regulator_count_voltages(
  214. mpu_vreg[i].vreg) > 0) {
  215. regulator_set_voltage(mpu_vreg[i].vreg,
  216. 0, mpu_vreg[i].max_uV);
  217. }
  218. regulator_put(mpu_vreg[i].vreg);
  219. mpu_vreg[i].vreg = NULL;
  220. goto error_vdd;
  221. }
  222. }
  223. return rc;
  224. } else {
  225. i = num_reg;
  226. }
  227. error_vdd:
  228. while (--i >= 0) {
  229. if (!IS_ERR_OR_NULL(mpu_vreg[i].vreg)) {
  230. if (regulator_count_voltages(
  231. mpu_vreg[i].vreg) > 0) {
  232. regulator_set_voltage(mpu_vreg[i].vreg, 0,
  233. mpu_vreg[i].max_uV);
  234. }
  235. regulator_disable(mpu_vreg[i].vreg);
  236. regulator_put(mpu_vreg[i].vreg);
  237. mpu_vreg[i].vreg = NULL;
  238. }
  239. }
  240. return rc;
  241. }
  242. static int mpu3050_poll_delay_set(struct sensors_classdev *sensors_cdev,
  243. unsigned int delay_msec)
  244. {
  245. struct mpu3050_sensor *sensor = container_of(sensors_cdev,
  246. struct mpu3050_sensor, cdev);
  247. unsigned int dlpf_index;
  248. u8 divider, reg;
  249. int ret;
  250. dlpf_index = interval_to_dlpf_cfg(delay_msec);
  251. divider = delay_msec * dlpf_table[dlpf_index].sample_rate - 1;
  252. if (sensor->dlpf_index != dlpf_index) {
  253. /* Set low pass filter and full scale */
  254. reg = dlpf_table[dlpf_index].cfg;
  255. reg |= MPU3050_DEFAULT_FS_RANGE << 3;
  256. reg |= MPU3050_EXT_SYNC_NONE << 5;
  257. ret = i2c_smbus_write_byte_data(sensor->client,
  258. MPU3050_DLPF_FS_SYNC, reg);
  259. if (!ret)
  260. sensor->dlpf_index = dlpf_index;
  261. }
  262. if (sensor->poll_interval != delay_msec) {
  263. /* Output frequency divider. The poll interval */
  264. ret = i2c_smbus_write_byte_data(sensor->client,
  265. MPU3050_SMPLRT_DIV, divider);
  266. if (!ret)
  267. sensor->poll_interval = delay_msec;
  268. }
  269. return 0;
  270. }
  271. /**
  272. * mpu3050_attr_get_polling_rate - get the sampling rate
  273. */
  274. static ssize_t mpu3050_attr_get_polling_rate(struct device *dev,
  275. struct device_attribute *attr,
  276. char *buf)
  277. {
  278. int val;
  279. struct mpu3050_sensor *sensor = dev_get_drvdata(dev);
  280. val = sensor ? sensor->poll_interval : 0;
  281. return snprintf(buf, 8, "%d\n", val);
  282. }
  283. /**
  284. * mpu3050_attr_set_polling_rate - set the sampling rate
  285. */
  286. static ssize_t mpu3050_attr_set_polling_rate(struct device *dev,
  287. struct device_attribute *attr,
  288. const char *buf, size_t size)
  289. {
  290. struct mpu3050_sensor *sensor = dev_get_drvdata(dev);
  291. unsigned long interval_ms;
  292. int ret;
  293. if (kstrtoul(buf, 10, &interval_ms))
  294. return -EINVAL;
  295. if ((interval_ms < MPU3050_MIN_POLL_INTERVAL) ||
  296. (interval_ms > MPU3050_MAX_POLL_INTERVAL))
  297. return -EINVAL;
  298. ret = mpu3050_poll_delay_set(&sensor->cdev, interval_ms);
  299. return ret < 0 ? ret : size;
  300. }
  301. static int mpu3050_enable_set(struct sensors_classdev *sensors_cdev,
  302. unsigned int enabled)
  303. {
  304. struct mpu3050_sensor *sensor = container_of(sensors_cdev,
  305. struct mpu3050_sensor, cdev);
  306. if (enabled && (!sensor->enable)) {
  307. sensor->enable = enabled;
  308. pm_runtime_get_sync(sensor->dev);
  309. if (sensor->use_poll)
  310. schedule_delayed_work(&sensor->input_work,
  311. msecs_to_jiffies(sensor->poll_interval));
  312. else
  313. enable_irq(sensor->client->irq);
  314. } else if (!enabled && sensor->enable) {
  315. if (sensor->use_poll)
  316. cancel_delayed_work_sync(&sensor->input_work);
  317. else
  318. disable_irq(sensor->client->irq);
  319. pm_runtime_put_sync(sensor->dev);
  320. sensor->enable = enabled;
  321. } else {
  322. dev_warn(&sensor->client->dev,
  323. "ignore enable state change from %d to %d\n",
  324. sensor->enable, enabled);
  325. }
  326. return 0;
  327. }
  328. /**
  329. * Set/get enable function is just needed by sensor HAL.
  330. */
  331. static ssize_t mpu3050_attr_set_enable(struct device *dev,
  332. struct device_attribute *attr,
  333. const char *buf, size_t count)
  334. {
  335. struct mpu3050_sensor *sensor = dev_get_drvdata(dev);
  336. unsigned long val;
  337. int err;
  338. if (kstrtoul(buf, 10, &val))
  339. return -EINVAL;
  340. err = mpu3050_enable_set(&sensor->cdev, val);
  341. if (err < 0)
  342. return err;
  343. return count;
  344. }
  345. static ssize_t mpu3050_attr_get_enable(struct device *dev,
  346. struct device_attribute *attr, char *buf)
  347. {
  348. struct mpu3050_sensor *sensor = dev_get_drvdata(dev);
  349. return snprintf(buf, 4, "%d\n", sensor->enable);
  350. }
  351. static struct device_attribute attributes[] = {
  352. __ATTR(pollrate_ms, 0664,
  353. mpu3050_attr_get_polling_rate,
  354. mpu3050_attr_set_polling_rate),
  355. __ATTR(enable, 0644,
  356. mpu3050_attr_get_enable,
  357. mpu3050_attr_set_enable),
  358. };
  359. static int create_sysfs_interfaces(struct device *dev)
  360. {
  361. int i;
  362. int err;
  363. for (i = 0; i < ARRAY_SIZE(attributes); i++) {
  364. err = device_create_file(dev, attributes + i);
  365. if (err)
  366. goto error;
  367. }
  368. return 0;
  369. error:
  370. for ( ; i >= 0; i--)
  371. device_remove_file(dev, attributes + i);
  372. dev_err(dev, "%s:Unable to create interface\n", __func__);
  373. return err;
  374. }
  375. static int remove_sysfs_interfaces(struct device *dev)
  376. {
  377. int i;
  378. for (i = 0; i < ARRAY_SIZE(attributes); i++)
  379. device_remove_file(dev, attributes + i);
  380. return 0;
  381. }
  382. /**
  383. * mpu3050_xyz_read_reg - read the axes values
  384. * @buffer: provide register addr and get register
  385. * @length: length of register
  386. *
  387. * Reads the register values in one transaction or returns a negative
  388. * error code on failure.
  389. */
  390. static int mpu3050_xyz_read_reg(struct i2c_client *client,
  391. u8 *buffer, int length)
  392. {
  393. /*
  394. * Annoying we can't make this const because the i2c layer doesn't
  395. * declare input buffers const.
  396. */
  397. char cmd = MPU3050_XOUT_H;
  398. struct i2c_msg msg[] = {
  399. {
  400. .addr = client->addr,
  401. .flags = 0,
  402. .len = 1,
  403. .buf = &cmd,
  404. },
  405. {
  406. .addr = client->addr,
  407. .flags = I2C_M_RD,
  408. .len = length,
  409. .buf = buffer,
  410. },
  411. };
  412. return i2c_transfer(client->adapter, msg, 2);
  413. }
  414. /**
  415. * mpu3050_read_xyz - get co-ordinates from device
  416. * @client: i2c address of sensor
  417. * @coords: co-ordinates to update
  418. *
  419. * Return the converted X Y and Z co-ordinates from the sensor device
  420. */
  421. static void mpu3050_read_xyz(struct i2c_client *client,
  422. struct axis_data *coords)
  423. {
  424. u16 buffer[3];
  425. mpu3050_xyz_read_reg(client, (u8 *)buffer, 6);
  426. coords->x = be16_to_cpu(buffer[0]);
  427. coords->y = be16_to_cpu(buffer[1]);
  428. coords->z = be16_to_cpu(buffer[2]);
  429. dev_dbg(&client->dev, "%s: x %d, y %d, z %d\n", __func__,
  430. coords->x, coords->y, coords->z);
  431. }
  432. /**
  433. * mpu3050_set_power_mode - set the power mode
  434. * @client: i2c client for the sensor
  435. * @val: value to switch on/off of power, 1: normal power, 0: low power
  436. *
  437. * Put device to normal-power mode or low-power mode.
  438. */
  439. static void mpu3050_set_power_mode(struct i2c_client *client, u8 val)
  440. {
  441. u8 value;
  442. struct mpu3050_sensor *sensor = i2c_get_clientdata(client);
  443. if (val) {
  444. mpu3050_config_regulator(client, 1);
  445. udelay(10);
  446. gpio_set_value(sensor->enable_gpio, 1);
  447. msleep(60);
  448. }
  449. value = i2c_smbus_read_byte_data(client, MPU3050_PWR_MGM);
  450. value = (value & ~MPU3050_PWR_MGM_MASK) |
  451. (((val << MPU3050_PWR_MGM_POS) & MPU3050_PWR_MGM_MASK) ^
  452. MPU3050_PWR_MGM_MASK);
  453. i2c_smbus_write_byte_data(client, MPU3050_PWR_MGM, value);
  454. if (!val) {
  455. udelay(10);
  456. gpio_set_value(sensor->enable_gpio, 0);
  457. udelay(10);
  458. mpu3050_config_regulator(client, 0);
  459. }
  460. }
  461. /**
  462. * mpu3050_interrupt_thread - handle an IRQ
  463. * @irq: interrupt numner
  464. * @data: the sensor
  465. *
  466. * Called by the kernel single threaded after an interrupt occurs. Read
  467. * the sensor data and generate an input event for it.
  468. */
  469. static irqreturn_t mpu3050_interrupt_thread(int irq, void *data)
  470. {
  471. struct mpu3050_sensor *sensor = data;
  472. struct axis_data axis;
  473. mpu3050_read_xyz(sensor->client, &axis);
  474. input_report_abs(sensor->idev, ABS_X, axis.x);
  475. input_report_abs(sensor->idev, ABS_Y, axis.y);
  476. input_report_abs(sensor->idev, ABS_Z, axis.z);
  477. input_sync(sensor->idev);
  478. return IRQ_HANDLED;
  479. }
  480. /**
  481. * mpu3050_input_work_fn - polling work
  482. * @work: the work struct
  483. *
  484. * Called by the work queue; read sensor data and generate an input
  485. * event
  486. */
  487. static void mpu3050_input_work_fn(struct work_struct *work)
  488. {
  489. struct mpu3050_sensor *sensor;
  490. struct axis_data axis;
  491. sensor = container_of((struct delayed_work *)work,
  492. struct mpu3050_sensor, input_work);
  493. mpu3050_read_xyz(sensor->client, &axis);
  494. input_report_abs(sensor->idev, ABS_X, axis.x);
  495. input_report_abs(sensor->idev, ABS_Y, axis.y);
  496. input_report_abs(sensor->idev, ABS_Z, axis.z);
  497. input_sync(sensor->idev);
  498. if (sensor->use_poll)
  499. schedule_delayed_work(&sensor->input_work,
  500. msecs_to_jiffies(sensor->poll_interval));
  501. }
  502. /**
  503. * mpu3050_hw_init - initialize hardware
  504. * @sensor: the sensor
  505. *
  506. * Called during device probe; configures the sampling method.
  507. */
  508. static int mpu3050_hw_init(struct mpu3050_sensor *sensor)
  509. {
  510. struct i2c_client *client = sensor->client;
  511. int ret;
  512. u8 reg;
  513. ret = i2c_smbus_read_byte_data(client, MPU3050_PWR_MGM);
  514. if (ret < 0)
  515. return ret;
  516. ret &= ~MPU3050_PWR_MGM_CLKSEL;
  517. ret |= MPU3050_PWR_MGM_PLL_Z;
  518. ret = i2c_smbus_write_byte_data(client, MPU3050_PWR_MGM, ret);
  519. if (ret < 0)
  520. return ret;
  521. /* Output frequency divider. The poll interval */
  522. ret = i2c_smbus_write_byte_data(client, MPU3050_SMPLRT_DIV,
  523. sensor->poll_interval - 1);
  524. if (ret < 0)
  525. return ret;
  526. /* Set low pass filter and full scale */
  527. reg = MPU3050_DLPF_CFG_42HZ;
  528. reg |= MPU3050_DEFAULT_FS_RANGE << 3;
  529. reg |= MPU3050_EXT_SYNC_NONE << 5;
  530. ret = i2c_smbus_write_byte_data(client, MPU3050_DLPF_FS_SYNC, reg);
  531. if (ret < 0)
  532. return ret;
  533. /* Enable interrupts */
  534. if (!sensor->use_poll) {
  535. reg = MPU3050_ACTIVE_LOW;
  536. reg |= MPU3050_OPEN_DRAIN;
  537. reg |= MPU3050_RAW_RDY_EN;
  538. ret = i2c_smbus_write_byte_data(client, MPU3050_INT_CFG, reg);
  539. if (ret < 0)
  540. return ret;
  541. }
  542. return 0;
  543. }
  544. #ifdef CONFIG_OF
  545. static int mpu3050_parse_dt(struct device *dev,
  546. struct mpu3050_gyro_platform_data *pdata)
  547. {
  548. int rc = 0;
  549. rc = of_property_read_u32(dev->of_node, "invn,poll-interval",
  550. &pdata->poll_interval);
  551. if (rc) {
  552. dev_err(dev, "Failed to read poll-interval\n");
  553. return rc;
  554. }
  555. /* check gpio_int later, if it is invalid, just use poll */
  556. pdata->gpio_int = of_get_named_gpio_flags(dev->of_node,
  557. "invn,gpio-int", 0, NULL);
  558. pdata->gpio_en = of_get_named_gpio_flags(dev->of_node,
  559. "invn,gpio-en", 0, NULL);
  560. if (!gpio_is_valid(pdata->gpio_en))
  561. return -EINVAL;
  562. return 0;
  563. }
  564. #else
  565. static int mpu3050_parse_dt(struct device *dev,
  566. struct mpu3050_gyro_platform_data *pdata)
  567. {
  568. return -EINVAL;
  569. }
  570. #endif
  571. /**
  572. * mpu3050_probe - device detection callback
  573. * @client: i2c client of found device
  574. * @id: id match information
  575. *
  576. * The I2C layer calls us when it believes a sensor is present at this
  577. * address. Probe to see if this is correct and to validate the device.
  578. *
  579. * If present install the relevant sysfs interfaces and input device.
  580. */
  581. static int __devinit mpu3050_probe(struct i2c_client *client,
  582. const struct i2c_device_id *id)
  583. {
  584. struct mpu3050_sensor *sensor;
  585. struct input_dev *idev;
  586. struct mpu3050_gyro_platform_data *pdata;
  587. int ret;
  588. int error;
  589. u32 i;
  590. sensor = kzalloc(sizeof(struct mpu3050_sensor), GFP_KERNEL);
  591. idev = input_allocate_device();
  592. if (!sensor || !idev) {
  593. dev_err(&client->dev, "failed to allocate driver data\n");
  594. error = -ENOMEM;
  595. goto err_free_mem;
  596. }
  597. sensor->client = client;
  598. sensor->dev = &client->dev;
  599. sensor->idev = idev;
  600. i2c_set_clientdata(client, sensor);
  601. if (client->dev.of_node) {
  602. pdata = devm_kzalloc(&client->dev,
  603. sizeof(struct mpu3050_gyro_platform_data), GFP_KERNEL);
  604. if (!pdata) {
  605. dev_err(&client->dev, "Failed to allcated memory\n");
  606. error = -ENOMEM;
  607. goto err_free_mem;
  608. }
  609. ret = mpu3050_parse_dt(&client->dev, pdata);
  610. if (ret) {
  611. dev_err(&client->dev, "Failed to parse device tree\n");
  612. error = ret;
  613. goto err_free_mem;
  614. }
  615. } else
  616. pdata = client->dev.platform_data;
  617. sensor->platform_data = pdata;
  618. if (sensor->platform_data) {
  619. u32 interval = sensor->platform_data->poll_interval;
  620. sensor->enable_gpio = sensor->platform_data->gpio_en;
  621. if ((interval < MPU3050_MIN_POLL_INTERVAL) ||
  622. (interval > MPU3050_MAX_POLL_INTERVAL))
  623. sensor->poll_interval = MPU3050_DEFAULT_POLL_INTERVAL;
  624. else
  625. sensor->poll_interval = interval;
  626. } else {
  627. sensor->poll_interval = MPU3050_DEFAULT_POLL_INTERVAL;
  628. sensor->enable_gpio = -EINVAL;
  629. }
  630. sensor->cdev = sensors_cdev;
  631. sensor->cdev.min_delay = MPU3050_MIN_POLL_INTERVAL * 1000;
  632. sensor->cdev.delay_msec = sensor->poll_interval;
  633. sensor->cdev.sensors_enable = mpu3050_enable_set;
  634. sensor->cdev.sensors_poll_delay = mpu3050_poll_delay_set;
  635. ret = sensors_classdev_register(&client->dev, &sensor->cdev);
  636. if (ret) {
  637. dev_err(&client->dev, "class device create failed: %d\n", ret);
  638. error = -EINVAL;
  639. goto err_free_mem;
  640. }
  641. if (gpio_is_valid(sensor->enable_gpio)) {
  642. ret = gpio_request(sensor->enable_gpio, "GYRO_EN_PM");
  643. gpio_direction_output(sensor->enable_gpio, 1);
  644. }
  645. mpu3050_set_power_mode(client, 1);
  646. ret = i2c_smbus_read_byte_data(client, MPU3050_CHIP_ID_REG);
  647. if (ret < 0) {
  648. dev_err(&client->dev, "failed to detect device\n");
  649. error = -ENXIO;
  650. goto err_class_sysfs;
  651. }
  652. for (i = 0; i < ARRAY_SIZE(mpu3050_chip_ids); i++)
  653. if (ret == mpu3050_chip_ids[i])
  654. break;
  655. if (i == ARRAY_SIZE(mpu3050_chip_ids)) {
  656. dev_err(&client->dev, "unsupported chip id\n");
  657. error = -ENXIO;
  658. goto err_class_sysfs;
  659. }
  660. idev->name = "MPU3050";
  661. idev->id.bustype = BUS_I2C;
  662. input_set_capability(idev, EV_ABS, ABS_MISC);
  663. input_set_abs_params(idev, ABS_X,
  664. MPU3050_MIN_VALUE, MPU3050_MAX_VALUE, 0, 0);
  665. input_set_abs_params(idev, ABS_Y,
  666. MPU3050_MIN_VALUE, MPU3050_MAX_VALUE, 0, 0);
  667. input_set_abs_params(idev, ABS_Z,
  668. MPU3050_MIN_VALUE, MPU3050_MAX_VALUE, 0, 0);
  669. input_set_drvdata(idev, sensor);
  670. pm_runtime_set_active(&client->dev);
  671. error = mpu3050_hw_init(sensor);
  672. if (error)
  673. goto err_pm_set_suspended;
  674. if (client->irq == 0) {
  675. sensor->use_poll = 1;
  676. INIT_DELAYED_WORK(&sensor->input_work, mpu3050_input_work_fn);
  677. } else {
  678. sensor->use_poll = 0;
  679. if (gpio_is_valid(sensor->platform_data->gpio_int)) {
  680. /* configure interrupt gpio */
  681. ret = gpio_request(sensor->platform_data->gpio_int,
  682. "gyro_gpio_int");
  683. if (ret) {
  684. pr_err("%s: unable to request interrupt gpio %d\n",
  685. __func__,
  686. sensor->platform_data->gpio_int);
  687. goto err_pm_set_suspended;
  688. }
  689. ret = gpio_direction_input(
  690. sensor->platform_data->gpio_int);
  691. if (ret) {
  692. pr_err("%s: unable to set direction for gpio %d\n",
  693. __func__, sensor->platform_data->gpio_int);
  694. goto err_free_gpio;
  695. }
  696. client->irq = gpio_to_irq(
  697. sensor->platform_data->gpio_int);
  698. } else {
  699. ret = -EINVAL;
  700. goto err_pm_set_suspended;
  701. }
  702. error = request_threaded_irq(client->irq,
  703. NULL, mpu3050_interrupt_thread,
  704. IRQF_TRIGGER_FALLING,
  705. "mpu3050", sensor);
  706. if (error) {
  707. dev_err(&client->dev,
  708. "can't get IRQ %d, error %d\n",
  709. client->irq, error);
  710. goto err_pm_set_suspended;
  711. }
  712. disable_irq(client->irq);
  713. }
  714. sensor->enable = 0;
  715. mpu3050_set_power_mode(client, 0);
  716. error = input_register_device(idev);
  717. if (error) {
  718. dev_err(&client->dev, "failed to register input device\n");
  719. goto err_free_irq;
  720. }
  721. error = create_sysfs_interfaces(&idev->dev);
  722. if (error < 0) {
  723. dev_err(&client->dev, "failed to create sysfs\n");
  724. goto err_input_cleanup;
  725. }
  726. pm_runtime_enable(&client->dev);
  727. pm_runtime_set_autosuspend_delay(&client->dev, MPU3050_AUTO_DELAY);
  728. return 0;
  729. err_input_cleanup:
  730. input_unregister_device(idev);
  731. err_free_irq:
  732. if (client->irq > 0)
  733. free_irq(client->irq, sensor);
  734. err_free_gpio:
  735. if ((client->irq > 0) &&
  736. (gpio_is_valid(sensor->platform_data->gpio_int)))
  737. gpio_free(sensor->platform_data->gpio_int);
  738. err_pm_set_suspended:
  739. pm_runtime_set_suspended(&client->dev);
  740. err_class_sysfs:
  741. sensors_classdev_unregister(&sensor->cdev);
  742. err_free_mem:
  743. input_free_device(idev);
  744. kfree(sensor);
  745. return error;
  746. }
  747. /**
  748. * mpu3050_remove - remove a sensor
  749. * @client: i2c client of sensor being removed
  750. *
  751. * Our sensor is going away, clean up the resources.
  752. */
  753. static int __devexit mpu3050_remove(struct i2c_client *client)
  754. {
  755. struct mpu3050_sensor *sensor = i2c_get_clientdata(client);
  756. pm_runtime_disable(&client->dev);
  757. pm_runtime_set_suspended(&client->dev);
  758. if (client->irq)
  759. free_irq(client->irq, sensor);
  760. remove_sysfs_interfaces(&client->dev);
  761. if (gpio_is_valid(sensor->enable_gpio))
  762. gpio_free(sensor->enable_gpio);
  763. input_unregister_device(sensor->idev);
  764. kfree(sensor);
  765. return 0;
  766. }
  767. #ifdef CONFIG_PM
  768. /**
  769. * mpu3050_suspend - called on device suspend
  770. * @dev: device being suspended
  771. *
  772. * Put the device into sleep mode before we suspend the machine.
  773. */
  774. static int mpu3050_suspend(struct device *dev)
  775. {
  776. struct i2c_client *client = to_i2c_client(dev);
  777. struct mpu3050_sensor *sensor = i2c_get_clientdata(client);
  778. if (sensor->enable) {
  779. if (!sensor->use_poll)
  780. disable_irq(client->irq);
  781. mpu3050_set_power_mode(client, 0);
  782. }
  783. return 0;
  784. }
  785. /**
  786. * mpu3050_resume - called on device resume
  787. * @dev: device being resumed
  788. *
  789. * Put the device into powered mode on resume.
  790. */
  791. static int mpu3050_resume(struct device *dev)
  792. {
  793. struct i2c_client *client = to_i2c_client(dev);
  794. struct mpu3050_sensor *sensor = i2c_get_clientdata(client);
  795. if (sensor->enable) {
  796. mpu3050_set_power_mode(client, 1);
  797. mpu3050_hw_init(sensor);
  798. if (!sensor->use_poll)
  799. enable_irq(client->irq);
  800. }
  801. return 0;
  802. }
  803. /**
  804. * mpu3050_runtime_suspend - called on device enters runtime suspend
  805. * @dev: device being suspended
  806. *
  807. * Put the device into sleep mode.
  808. */
  809. static int mpu3050_runtime_suspend(struct device *dev)
  810. {
  811. struct i2c_client *client = to_i2c_client(dev);
  812. struct mpu3050_sensor *sensor = i2c_get_clientdata(client);
  813. if (sensor->enable)
  814. mpu3050_set_power_mode(client, 0);
  815. return 0;
  816. }
  817. /**
  818. * mpu3050_runtime_resume - called on device enters runtime resume
  819. * @dev: device being resumed
  820. *
  821. * Put the device into powered mode.
  822. */
  823. static int mpu3050_runtime_resume(struct device *dev)
  824. {
  825. struct i2c_client *client = to_i2c_client(dev);
  826. struct mpu3050_sensor *sensor = i2c_get_clientdata(client);
  827. if (sensor->enable) {
  828. mpu3050_set_power_mode(client, 1);
  829. mpu3050_hw_init(sensor);
  830. }
  831. return 0;
  832. }
  833. #endif
  834. static const struct dev_pm_ops mpu3050_pm = {
  835. .runtime_suspend = mpu3050_runtime_suspend,
  836. .runtime_resume = mpu3050_runtime_resume,
  837. .runtime_idle = NULL,
  838. .suspend = mpu3050_suspend,
  839. .resume = mpu3050_resume,
  840. .freeze = mpu3050_suspend,
  841. .thaw = mpu3050_resume,
  842. .poweroff = mpu3050_suspend,
  843. .restore = mpu3050_resume,
  844. };
  845. static const struct i2c_device_id mpu3050_ids[] = {
  846. { "mpu3050", 0 },
  847. { }
  848. };
  849. MODULE_DEVICE_TABLE(i2c, mpu3050_ids);
  850. static const struct of_device_id mpu3050_of_match[] = {
  851. { .compatible = "invn,mpu3050", },
  852. { },
  853. };
  854. MODULE_DEVICE_TABLE(of, mpu3050_of_match);
  855. static struct i2c_driver mpu3050_i2c_driver = {
  856. .driver = {
  857. .name = "mpu3050",
  858. .owner = THIS_MODULE,
  859. .pm = &mpu3050_pm,
  860. .of_match_table = mpu3050_of_match,
  861. },
  862. .probe = mpu3050_probe,
  863. .remove = __devexit_p(mpu3050_remove),
  864. .id_table = mpu3050_ids,
  865. };
  866. module_i2c_driver(mpu3050_i2c_driver);
  867. MODULE_AUTHOR("Wistron Corp.");
  868. MODULE_DESCRIPTION("MPU3050 Tri-axis gyroscope driver");
  869. MODULE_LICENSE("GPL");