2 * Common code for Freescale MMA955x Intelligent Sensor Platform drivers
3 * Copyright (c) 2014, Intel Corporation.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 #include <linux/module.h>
16 #include <linux/i2c.h>
17 #include <linux/delay.h>
18 #include <linux/iio/iio.h>
19 #include <linux/pm_runtime.h>
20 #include "mma9551_core.h"
22 /* Command masks for mailbox write command */
23 #define MMA9551_CMD_READ_VERSION_INFO 0x00
24 #define MMA9551_CMD_READ_CONFIG 0x10
25 #define MMA9551_CMD_WRITE_CONFIG 0x20
26 #define MMA9551_CMD_READ_STATUS 0x30
28 /* Mailbox read command */
29 #define MMA9551_RESPONSE_COCO BIT(7)
31 /* Error-Status codes returned in mailbox read command */
32 #define MMA9551_MCI_ERROR_NONE 0x00
33 #define MMA9551_MCI_ERROR_PARAM 0x04
34 #define MMA9551_MCI_INVALID_COUNT 0x19
35 #define MMA9551_MCI_ERROR_COMMAND 0x1C
36 #define MMA9551_MCI_ERROR_INVALID_LENGTH 0x21
37 #define MMA9551_MCI_ERROR_FIFO_BUSY 0x22
38 #define MMA9551_MCI_ERROR_FIFO_ALLOCATED 0x23
39 #define MMA9551_MCI_ERROR_FIFO_OVERSIZE 0x24
41 /* GPIO Application */
42 #define MMA9551_GPIO_POL_MSB 0x08
43 #define MMA9551_GPIO_POL_LSB 0x09
45 /* Sleep/Wake application */
46 #define MMA9551_SLEEP_CFG 0x06
47 #define MMA9551_SLEEP_CFG_SNCEN BIT(0)
48 #define MMA9551_SLEEP_CFG_FLEEN BIT(1)
49 #define MMA9551_SLEEP_CFG_SCHEN BIT(2)
52 #define MMA9551_AFE_X_ACCEL_REG 0x00
53 #define MMA9551_AFE_Y_ACCEL_REG 0x02
54 #define MMA9551_AFE_Z_ACCEL_REG 0x04
56 /* Reset/Suspend/Clear application */
57 #define MMA9551_RSC_RESET 0x00
58 #define MMA9551_RSC_OFFSET(mask) (3 - (ffs(mask) - 1) / 8)
59 #define MMA9551_RSC_VAL(mask) (mask >> (((ffs(mask) - 1) / 8) * 8))
62 * A response is composed of:
63 * - control registers: MB0-3
64 * - data registers: MB4-31
66 * A request is composed of:
67 * - mbox to write to (always 0)
68 * - control registers: MB1-4
69 * - data registers: MB5-31
71 #define MMA9551_MAILBOX_CTRL_REGS 4
72 #define MMA9551_MAX_MAILBOX_DATA_REGS 28
73 #define MMA9551_MAILBOX_REGS 32
75 #define MMA9551_I2C_READ_RETRIES 5
76 #define MMA9551_I2C_READ_DELAY 50 /* us */
78 struct mma9551_mbox_request {
79 u8 start_mbox; /* Always 0. */
82 * See Section 5.3.1 of the MMA955xL Software Reference Manual.
84 * Bit 7: reserved, always 0
86 * Bits 3-0: upper bits of register offset
91 u8 buf[MMA9551_MAX_MAILBOX_DATA_REGS - 1];
94 struct mma9551_mbox_response {
97 * See Section 5.3.3 of the MMA955xL Software Reference Manual.
100 * Bits 6-0: Error code.
105 u8 buf[MMA9551_MAX_MAILBOX_DATA_REGS];
108 struct mma9551_version_info {
116 static int mma9551_transfer(struct i2c_client *client,
117 u8 app_id, u8 command, u16 offset,
118 u8 *inbytes, int num_inbytes,
119 u8 *outbytes, int num_outbytes)
121 struct mma9551_mbox_request req;
122 struct mma9551_mbox_response rsp;
123 struct i2c_msg in, out;
124 u8 req_len, err_code;
127 if (offset >= 1 << 12) {
128 dev_err(&client->dev, "register offset too large\n");
132 req_len = 1 + MMA9551_MAILBOX_CTRL_REGS + num_inbytes;
135 req.cmd_off = command | (offset >> 8);
136 req.lower_off = offset;
138 if (command == MMA9551_CMD_WRITE_CONFIG)
139 req.nbytes = num_inbytes;
141 req.nbytes = num_outbytes;
143 memcpy(req.buf, inbytes, num_inbytes);
145 out.addr = client->addr;
148 out.buf = (u8 *)&req;
150 ret = i2c_transfer(client->adapter, &out, 1);
152 dev_err(&client->dev, "i2c write failed\n");
156 retries = MMA9551_I2C_READ_RETRIES;
158 udelay(MMA9551_I2C_READ_DELAY);
160 in.addr = client->addr;
162 in.len = sizeof(rsp);
165 ret = i2c_transfer(client->adapter, &in, 1);
167 dev_err(&client->dev, "i2c read failed\n");
171 if (rsp.coco_err & MMA9551_RESPONSE_COCO)
173 } while (--retries > 0);
176 dev_err(&client->dev,
177 "timed out while waiting for command response\n");
181 if (rsp.app_id != app_id) {
182 dev_err(&client->dev,
183 "app_id mismatch in response got %02x expected %02x\n",
188 err_code = rsp.coco_err & ~MMA9551_RESPONSE_COCO;
189 if (err_code != MMA9551_MCI_ERROR_NONE) {
190 dev_err(&client->dev, "read returned error %x\n", err_code);
194 if (rsp.nbytes != rsp.req_bytes) {
195 dev_err(&client->dev,
196 "output length mismatch got %d expected %d\n",
197 rsp.nbytes, rsp.req_bytes);
202 memcpy(outbytes, rsp.buf, num_outbytes);
208 * mma9551_read_config_byte() - read 1 configuration byte
209 * @client: I2C client
210 * @app_id: Application ID
211 * @reg: Application register
212 * @val: Pointer to store value read
214 * Read one configuration byte from the device using MMA955xL command format.
215 * Commands to the MMA955xL platform consist of a write followed
216 * by one or more reads.
218 * Locking note: This function must be called with the device lock held.
219 * Locking is not handled inside the function. Callers should ensure they
220 * serialize access to the HW.
222 * Returns: 0 on success, negative value on failure.
224 int mma9551_read_config_byte(struct i2c_client *client, u8 app_id,
227 return mma9551_transfer(client, app_id, MMA9551_CMD_READ_CONFIG,
228 reg, NULL, 0, val, 1);
230 EXPORT_SYMBOL(mma9551_read_config_byte);
233 * mma9551_write_config_byte() - write 1 configuration byte
234 * @client: I2C client
235 * @app_id: Application ID
236 * @reg: Application register
237 * @val: Value to write
239 * Write one configuration byte from the device using MMA955xL command format.
240 * Commands to the MMA955xL platform consist of a write followed by one or
243 * Locking note: This function must be called with the device lock held.
244 * Locking is not handled inside the function. Callers should ensure they
245 * serialize access to the HW.
247 * Returns: 0 on success, negative value on failure.
249 int mma9551_write_config_byte(struct i2c_client *client, u8 app_id,
252 return mma9551_transfer(client, app_id, MMA9551_CMD_WRITE_CONFIG, reg,
255 EXPORT_SYMBOL(mma9551_write_config_byte);
258 * mma9551_read_status_byte() - read 1 status byte
259 * @client: I2C client
260 * @app_id: Application ID
261 * @reg: Application register
262 * @val: Pointer to store value read
264 * Read one status byte from the device using MMA955xL command format.
265 * Commands to the MMA955xL platform consist of a write followed by one or
268 * Locking note: This function must be called with the device lock held.
269 * Locking is not handled inside the function. Callers should ensure they
270 * serialize access to the HW.
272 * Returns: 0 on success, negative value on failure.
274 int mma9551_read_status_byte(struct i2c_client *client, u8 app_id,
277 return mma9551_transfer(client, app_id, MMA9551_CMD_READ_STATUS,
278 reg, NULL, 0, val, 1);
280 EXPORT_SYMBOL(mma9551_read_status_byte);
283 * mma9551_read_config_word() - read 1 config word
284 * @client: I2C client
285 * @app_id: Application ID
286 * @reg: Application register
287 * @val: Pointer to store value read
289 * Read one configuration word from the device using MMA955xL command format.
290 * Commands to the MMA955xL platform consist of a write followed by one or
293 * Locking note: This function must be called with the device lock held.
294 * Locking is not handled inside the function. Callers should ensure they
295 * serialize access to the HW.
297 * Returns: 0 on success, negative value on failure.
299 int mma9551_read_config_word(struct i2c_client *client, u8 app_id,
305 ret = mma9551_transfer(client, app_id, MMA9551_CMD_READ_CONFIG,
306 reg, NULL, 0, (u8 *)&v, 2);
310 *val = be16_to_cpu(v);
314 EXPORT_SYMBOL(mma9551_read_config_word);
317 * mma9551_write_config_word() - write 1 config word
318 * @client: I2C client
319 * @app_id: Application ID
320 * @reg: Application register
321 * @val: Value to write
323 * Write one configuration word from the device using MMA955xL command format.
324 * Commands to the MMA955xL platform consist of a write followed by one or
327 * Locking note: This function must be called with the device lock held.
328 * Locking is not handled inside the function. Callers should ensure they
329 * serialize access to the HW.
331 * Returns: 0 on success, negative value on failure.
333 int mma9551_write_config_word(struct i2c_client *client, u8 app_id,
336 __be16 v = cpu_to_be16(val);
338 return mma9551_transfer(client, app_id, MMA9551_CMD_WRITE_CONFIG, reg,
339 (u8 *)&v, 2, NULL, 0);
341 EXPORT_SYMBOL(mma9551_write_config_word);
344 * mma9551_read_status_word() - read 1 status word
345 * @client: I2C client
346 * @app_id: Application ID
347 * @reg: Application register
348 * @val: Pointer to store value read
350 * Read one status word from the device using MMA955xL command format.
351 * Commands to the MMA955xL platform consist of a write followed by one or
354 * Locking note: This function must be called with the device lock held.
355 * Locking is not handled inside the function. Callers should ensure they
356 * serialize access to the HW.
358 * Returns: 0 on success, negative value on failure.
360 int mma9551_read_status_word(struct i2c_client *client, u8 app_id,
366 ret = mma9551_transfer(client, app_id, MMA9551_CMD_READ_STATUS,
367 reg, NULL, 0, (u8 *)&v, 2);
371 *val = be16_to_cpu(v);
375 EXPORT_SYMBOL(mma9551_read_status_word);
378 * mma9551_read_config_words() - read multiple config words
379 * @client: I2C client
380 * @app_id: Application ID
381 * @reg: Application register
382 * @len: Length of array to read (in words)
383 * @buf: Array of words to read
385 * Read multiple configuration registers (word-sized registers).
387 * Locking note: This function must be called with the device lock held.
388 * Locking is not handled inside the function. Callers should ensure they
389 * serialize access to the HW.
391 * Returns: 0 on success, negative value on failure.
393 int mma9551_read_config_words(struct i2c_client *client, u8 app_id,
394 u16 reg, u8 len, u16 *buf)
397 __be16 be_buf[MMA9551_MAX_MAILBOX_DATA_REGS / 2];
399 if (len > ARRAY_SIZE(be_buf)) {
400 dev_err(&client->dev, "Invalid buffer size %d\n", len);
404 ret = mma9551_transfer(client, app_id, MMA9551_CMD_READ_CONFIG,
405 reg, NULL, 0, (u8 *)be_buf, len * sizeof(u16));
409 for (i = 0; i < len; i++)
410 buf[i] = be16_to_cpu(be_buf[i]);
414 EXPORT_SYMBOL(mma9551_read_config_words);
417 * mma9551_read_status_words() - read multiple status words
418 * @client: I2C client
419 * @app_id: Application ID
420 * @reg: Application register
421 * @len: Length of array to read (in words)
422 * @buf: Array of words to read
424 * Read multiple status registers (word-sized registers).
426 * Locking note: This function must be called with the device lock held.
427 * Locking is not handled inside the function. Callers should ensure they
428 * serialize access to the HW.
430 * Returns: 0 on success, negative value on failure.
432 int mma9551_read_status_words(struct i2c_client *client, u8 app_id,
433 u16 reg, u8 len, u16 *buf)
436 __be16 be_buf[MMA9551_MAX_MAILBOX_DATA_REGS / 2];
438 if (len > ARRAY_SIZE(be_buf)) {
439 dev_err(&client->dev, "Invalid buffer size %d\n", len);
443 ret = mma9551_transfer(client, app_id, MMA9551_CMD_READ_STATUS,
444 reg, NULL, 0, (u8 *)be_buf, len * sizeof(u16));
448 for (i = 0; i < len; i++)
449 buf[i] = be16_to_cpu(be_buf[i]);
453 EXPORT_SYMBOL(mma9551_read_status_words);
456 * mma9551_write_config_words() - write multiple config words
457 * @client: I2C client
458 * @app_id: Application ID
459 * @reg: Application register
460 * @len: Length of array to write (in words)
461 * @buf: Array of words to write
463 * Write multiple configuration registers (word-sized registers).
465 * Locking note: This function must be called with the device lock held.
466 * Locking is not handled inside the function. Callers should ensure they
467 * serialize access to the HW.
469 * Returns: 0 on success, negative value on failure.
471 int mma9551_write_config_words(struct i2c_client *client, u8 app_id,
472 u16 reg, u8 len, u16 *buf)
475 __be16 be_buf[(MMA9551_MAX_MAILBOX_DATA_REGS - 1) / 2];
477 if (len > ARRAY_SIZE(be_buf)) {
478 dev_err(&client->dev, "Invalid buffer size %d\n", len);
482 for (i = 0; i < len; i++)
483 be_buf[i] = cpu_to_be16(buf[i]);
485 return mma9551_transfer(client, app_id, MMA9551_CMD_WRITE_CONFIG,
486 reg, (u8 *)be_buf, len * sizeof(u16), NULL, 0);
488 EXPORT_SYMBOL(mma9551_write_config_words);
491 * mma9551_update_config_bits() - update bits in register
492 * @client: I2C client
493 * @app_id: Application ID
494 * @reg: Application register
495 * @mask: Mask for the bits to update
496 * @val: Value of the bits to update
498 * Update bits in the given register using a bit mask.
500 * Locking note: This function must be called with the device lock held.
501 * Locking is not handled inside the function. Callers should ensure they
502 * serialize access to the HW.
504 * Returns: 0 on success, negative value on failure.
506 int mma9551_update_config_bits(struct i2c_client *client, u8 app_id,
507 u16 reg, u8 mask, u8 val)
512 ret = mma9551_read_config_byte(client, app_id, reg, &orig);
522 return mma9551_write_config_byte(client, app_id, reg, tmp);
524 EXPORT_SYMBOL(mma9551_update_config_bits);
527 * mma9551_gpio_config() - configure gpio
528 * @client: I2C client
529 * @pin: GPIO pin to configure
530 * @app_id: Application ID
531 * @bitnum: Bit number of status register being assigned to the GPIO pin.
532 * @polarity: The polarity parameter is described in section 6.2.2, page 66,
533 * of the Software Reference Manual. Basically, polarity=0 means
534 * the interrupt line has the same value as the selected bit,
535 * while polarity=1 means the line is inverted.
537 * Assign a bit from an application’s status register to a specific GPIO pin.
539 * Locking note: This function must be called with the device lock held.
540 * Locking is not handled inside the function. Callers should ensure they
541 * serialize access to the HW.
543 * Returns: 0 on success, negative value on failure.
545 int mma9551_gpio_config(struct i2c_client *client, enum mma9551_gpio_pin pin,
546 u8 app_id, u8 bitnum, int polarity)
548 u8 reg, pol_mask, pol_val;
551 if (pin > mma9551_gpio_max) {
552 dev_err(&client->dev, "bad GPIO pin\n");
557 * Pin 6 is configured by regs 0x00 and 0x01, pin 7 by 0x02 and
562 ret = mma9551_write_config_byte(client, MMA9551_APPID_GPIO,
565 dev_err(&client->dev, "error setting GPIO app_id\n");
569 ret = mma9551_write_config_byte(client, MMA9551_APPID_GPIO,
572 dev_err(&client->dev, "error setting GPIO bit number\n");
578 reg = MMA9551_GPIO_POL_LSB;
582 reg = MMA9551_GPIO_POL_LSB;
586 reg = MMA9551_GPIO_POL_MSB;
590 reg = MMA9551_GPIO_POL_MSB;
594 pol_val = polarity ? pol_mask : 0;
596 ret = mma9551_update_config_bits(client, MMA9551_APPID_GPIO, reg,
599 dev_err(&client->dev, "error setting GPIO polarity\n");
603 EXPORT_SYMBOL(mma9551_gpio_config);
606 * mma9551_read_version() - read device version information
607 * @client: I2C client
609 * Read version information and print device id and firmware version.
611 * Locking note: This function must be called with the device lock held.
612 * Locking is not handled inside the function. Callers should ensure they
613 * serialize access to the HW.
615 * Returns: 0 on success, negative value on failure.
617 int mma9551_read_version(struct i2c_client *client)
619 struct mma9551_version_info info;
622 ret = mma9551_transfer(client, MMA9551_APPID_VERSION, 0x00, 0x00,
623 NULL, 0, (u8 *)&info, sizeof(info));
627 dev_info(&client->dev, "device ID 0x%x, firmware version %02x.%02x\n",
628 be32_to_cpu(info.device_id), info.fw_version[0],
633 EXPORT_SYMBOL(mma9551_read_version);
636 * mma9551_set_device_state() - sets HW power mode
637 * @client: I2C client
638 * @enable: Use true to power on device, false to cause the device
641 * Set power on/off for device using the Sleep/Wake Application.
642 * When enable is true, power on chip and enable doze mode.
643 * When enable is false, enter sleep mode (device remains in the
644 * lowest-power mode).
646 * Locking note: This function must be called with the device lock held.
647 * Locking is not handled inside the function. Callers should ensure they
648 * serialize access to the HW.
650 * Returns: 0 on success, negative value on failure.
652 int mma9551_set_device_state(struct i2c_client *client, bool enable)
654 return mma9551_update_config_bits(client, MMA9551_APPID_SLEEP_WAKE,
656 MMA9551_SLEEP_CFG_SNCEN |
657 MMA9551_SLEEP_CFG_FLEEN |
658 MMA9551_SLEEP_CFG_SCHEN,
659 enable ? MMA9551_SLEEP_CFG_SCHEN |
660 MMA9551_SLEEP_CFG_FLEEN :
661 MMA9551_SLEEP_CFG_SNCEN);
663 EXPORT_SYMBOL(mma9551_set_device_state);
666 * mma9551_set_power_state() - sets runtime PM state
667 * @client: I2C client
668 * @on: Use true to power on device, false to power off
670 * Resume or suspend the device using Runtime PM.
671 * The device will suspend after the autosuspend delay.
673 * Returns: 0 on success, negative value on failure.
675 int mma9551_set_power_state(struct i2c_client *client, bool on)
681 ret = pm_runtime_get_sync(&client->dev);
683 pm_runtime_mark_last_busy(&client->dev);
684 ret = pm_runtime_put_autosuspend(&client->dev);
688 dev_err(&client->dev,
689 "failed to change power state to %d\n", on);
691 pm_runtime_put_noidle(&client->dev);
699 EXPORT_SYMBOL(mma9551_set_power_state);
702 * mma9551_sleep() - sleep
703 * @freq: Application frequency
705 * Firmware applications run at a certain frequency on the
706 * device. Sleep for one application cycle to make sure the
707 * application had time to run once and initialize set values.
709 void mma9551_sleep(int freq)
711 int sleep_val = 1000 / freq;
714 usleep_range(sleep_val * 1000, 20000);
716 msleep_interruptible(sleep_val);
718 EXPORT_SYMBOL(mma9551_sleep);
721 * mma9551_read_accel_chan() - read accelerometer channel
722 * @client: I2C client
724 * @val: Pointer to the accelerometer value read
727 * Read accelerometer value for the specified channel.
729 * Locking note: This function must be called with the device lock held.
730 * Locking is not handled inside the function. Callers should ensure they
731 * serialize access to the HW.
733 * Returns: IIO_VAL_INT on success, negative value on failure.
735 int mma9551_read_accel_chan(struct i2c_client *client,
736 const struct iio_chan_spec *chan,
743 switch (chan->channel2) {
745 reg_addr = MMA9551_AFE_X_ACCEL_REG;
748 reg_addr = MMA9551_AFE_Y_ACCEL_REG;
751 reg_addr = MMA9551_AFE_Z_ACCEL_REG;
757 ret = mma9551_set_power_state(client, true);
761 ret = mma9551_read_status_word(client, MMA9551_APPID_AFE,
762 reg_addr, &raw_accel);
771 mma9551_set_power_state(client, false);
774 EXPORT_SYMBOL(mma9551_read_accel_chan);
777 * mma9551_read_accel_scale() - read accelerometer scale
778 * @val: Pointer to the accelerometer scale (int value)
779 * @val2: Pointer to the accelerometer scale (micro value)
781 * Read accelerometer scale.
783 * Returns: IIO_VAL_INT_PLUS_MICRO.
785 int mma9551_read_accel_scale(int *val, int *val2)
790 return IIO_VAL_INT_PLUS_MICRO;
792 EXPORT_SYMBOL(mma9551_read_accel_scale);
795 * mma9551_app_reset() - reset application
796 * @client: I2C client
797 * @app_mask: Application to reset
799 * Reset the given application (using the Reset/Suspend/Clear
800 * Control Application)
802 * Returns: 0 on success, negative value on failure.
804 int mma9551_app_reset(struct i2c_client *client, u32 app_mask)
806 return mma9551_write_config_byte(client, MMA9551_APPID_RSC,
808 MMA9551_RSC_OFFSET(app_mask),
809 MMA9551_RSC_VAL(app_mask));
811 EXPORT_SYMBOL(mma9551_app_reset);
813 MODULE_AUTHOR("Irina Tirdea <irina.tirdea@intel.com>");
814 MODULE_AUTHOR("Vlad Dogaru <vlad.dogaru@intel.com>");
815 MODULE_LICENSE("GPL v2");
816 MODULE_DESCRIPTION("MMA955xL sensors core");