1 // SPDX-License-Identifier: GPL-2.0-only
3 * Common code for Freescale MMA955x Intelligent Sensor Platform drivers
4 * Copyright (c) 2014, Intel Corporation.
7 #include <linux/module.h>
9 #include <linux/delay.h>
10 #include <linux/iio/iio.h>
11 #include <linux/pm_runtime.h>
12 #include "mma9551_core.h"
14 /* Command masks for mailbox write command */
15 #define MMA9551_CMD_READ_VERSION_INFO 0x00
16 #define MMA9551_CMD_READ_CONFIG 0x10
17 #define MMA9551_CMD_WRITE_CONFIG 0x20
18 #define MMA9551_CMD_READ_STATUS 0x30
20 /* Mailbox read command */
21 #define MMA9551_RESPONSE_COCO BIT(7)
23 /* Error-Status codes returned in mailbox read command */
24 #define MMA9551_MCI_ERROR_NONE 0x00
25 #define MMA9551_MCI_ERROR_PARAM 0x04
26 #define MMA9551_MCI_INVALID_COUNT 0x19
27 #define MMA9551_MCI_ERROR_COMMAND 0x1C
28 #define MMA9551_MCI_ERROR_INVALID_LENGTH 0x21
29 #define MMA9551_MCI_ERROR_FIFO_BUSY 0x22
30 #define MMA9551_MCI_ERROR_FIFO_ALLOCATED 0x23
31 #define MMA9551_MCI_ERROR_FIFO_OVERSIZE 0x24
33 /* GPIO Application */
34 #define MMA9551_GPIO_POL_MSB 0x08
35 #define MMA9551_GPIO_POL_LSB 0x09
37 /* Sleep/Wake application */
38 #define MMA9551_SLEEP_CFG 0x06
39 #define MMA9551_SLEEP_CFG_SNCEN BIT(0)
40 #define MMA9551_SLEEP_CFG_FLEEN BIT(1)
41 #define MMA9551_SLEEP_CFG_SCHEN BIT(2)
44 #define MMA9551_AFE_X_ACCEL_REG 0x00
45 #define MMA9551_AFE_Y_ACCEL_REG 0x02
46 #define MMA9551_AFE_Z_ACCEL_REG 0x04
48 /* Reset/Suspend/Clear application */
49 #define MMA9551_RSC_RESET 0x00
50 #define MMA9551_RSC_OFFSET(mask) (3 - (ffs(mask) - 1) / 8)
51 #define MMA9551_RSC_VAL(mask) (mask >> (((ffs(mask) - 1) / 8) * 8))
54 * A response is composed of:
55 * - control registers: MB0-3
56 * - data registers: MB4-31
58 * A request is composed of:
59 * - mbox to write to (always 0)
60 * - control registers: MB1-4
61 * - data registers: MB5-31
63 #define MMA9551_MAILBOX_CTRL_REGS 4
64 #define MMA9551_MAX_MAILBOX_DATA_REGS 28
65 #define MMA9551_MAILBOX_REGS 32
67 #define MMA9551_I2C_READ_RETRIES 5
68 #define MMA9551_I2C_READ_DELAY 50 /* us */
70 struct mma9551_mbox_request
{
71 u8 start_mbox
; /* Always 0. */
74 * See Section 5.3.1 of the MMA955xL Software Reference Manual.
76 * Bit 7: reserved, always 0
78 * Bits 3-0: upper bits of register offset
83 u8 buf
[MMA9551_MAX_MAILBOX_DATA_REGS
- 1];
86 struct mma9551_mbox_response
{
89 * See Section 5.3.3 of the MMA955xL Software Reference Manual.
92 * Bits 6-0: Error code.
97 u8 buf
[MMA9551_MAX_MAILBOX_DATA_REGS
];
100 struct mma9551_version_info
{
108 static int mma9551_transfer(struct i2c_client
*client
,
109 u8 app_id
, u8 command
, u16 offset
,
110 u8
*inbytes
, int num_inbytes
,
111 u8
*outbytes
, int num_outbytes
)
113 struct mma9551_mbox_request req
;
114 struct mma9551_mbox_response rsp
;
115 struct i2c_msg in
, out
;
116 u8 req_len
, err_code
;
119 if (offset
>= 1 << 12) {
120 dev_err(&client
->dev
, "register offset too large\n");
124 req_len
= 1 + MMA9551_MAILBOX_CTRL_REGS
+ num_inbytes
;
127 req
.cmd_off
= command
| (offset
>> 8);
128 req
.lower_off
= offset
;
130 if (command
== MMA9551_CMD_WRITE_CONFIG
)
131 req
.nbytes
= num_inbytes
;
133 req
.nbytes
= num_outbytes
;
135 memcpy(req
.buf
, inbytes
, num_inbytes
);
137 out
.addr
= client
->addr
;
140 out
.buf
= (u8
*)&req
;
142 ret
= i2c_transfer(client
->adapter
, &out
, 1);
144 dev_err(&client
->dev
, "i2c write failed\n");
148 retries
= MMA9551_I2C_READ_RETRIES
;
150 udelay(MMA9551_I2C_READ_DELAY
);
152 in
.addr
= client
->addr
;
154 in
.len
= sizeof(rsp
);
157 ret
= i2c_transfer(client
->adapter
, &in
, 1);
159 dev_err(&client
->dev
, "i2c read failed\n");
163 if (rsp
.coco_err
& MMA9551_RESPONSE_COCO
)
165 } while (--retries
> 0);
168 dev_err(&client
->dev
,
169 "timed out while waiting for command response\n");
173 if (rsp
.app_id
!= app_id
) {
174 dev_err(&client
->dev
,
175 "app_id mismatch in response got %02x expected %02x\n",
180 err_code
= rsp
.coco_err
& ~MMA9551_RESPONSE_COCO
;
181 if (err_code
!= MMA9551_MCI_ERROR_NONE
) {
182 dev_err(&client
->dev
, "read returned error %x\n", err_code
);
186 if (rsp
.nbytes
!= rsp
.req_bytes
) {
187 dev_err(&client
->dev
,
188 "output length mismatch got %d expected %d\n",
189 rsp
.nbytes
, rsp
.req_bytes
);
194 memcpy(outbytes
, rsp
.buf
, num_outbytes
);
200 * mma9551_read_config_byte() - read 1 configuration byte
201 * @client: I2C client
202 * @app_id: Application ID
203 * @reg: Application register
204 * @val: Pointer to store value read
206 * Read one configuration byte from the device using MMA955xL command format.
207 * Commands to the MMA955xL platform consist of a write followed
208 * by one or more reads.
210 * Locking note: This function must be called with the device lock held.
211 * Locking is not handled inside the function. Callers should ensure they
212 * serialize access to the HW.
214 * Returns: 0 on success, negative value on failure.
216 int mma9551_read_config_byte(struct i2c_client
*client
, u8 app_id
,
219 return mma9551_transfer(client
, app_id
, MMA9551_CMD_READ_CONFIG
,
220 reg
, NULL
, 0, val
, 1);
222 EXPORT_SYMBOL_NS(mma9551_read_config_byte
, "IIO_MMA9551");
225 * mma9551_write_config_byte() - write 1 configuration byte
226 * @client: I2C client
227 * @app_id: Application ID
228 * @reg: Application register
229 * @val: Value to write
231 * Write one configuration byte from the device using MMA955xL command format.
232 * Commands to the MMA955xL platform consist of a write followed by one or
235 * Locking note: This function must be called with the device lock held.
236 * Locking is not handled inside the function. Callers should ensure they
237 * serialize access to the HW.
239 * Returns: 0 on success, negative value on failure.
241 int mma9551_write_config_byte(struct i2c_client
*client
, u8 app_id
,
244 return mma9551_transfer(client
, app_id
, MMA9551_CMD_WRITE_CONFIG
, reg
,
247 EXPORT_SYMBOL_NS(mma9551_write_config_byte
, "IIO_MMA9551");
250 * mma9551_read_status_byte() - read 1 status byte
251 * @client: I2C client
252 * @app_id: Application ID
253 * @reg: Application register
254 * @val: Pointer to store value read
256 * Read one status byte from the device using MMA955xL command format.
257 * Commands to the MMA955xL platform consist of a write followed by one or
260 * Locking note: This function must be called with the device lock held.
261 * Locking is not handled inside the function. Callers should ensure they
262 * serialize access to the HW.
264 * Returns: 0 on success, negative value on failure.
266 int mma9551_read_status_byte(struct i2c_client
*client
, u8 app_id
,
269 return mma9551_transfer(client
, app_id
, MMA9551_CMD_READ_STATUS
,
270 reg
, NULL
, 0, val
, 1);
272 EXPORT_SYMBOL_NS(mma9551_read_status_byte
, "IIO_MMA9551");
275 * mma9551_read_config_word() - read 1 config word
276 * @client: I2C client
277 * @app_id: Application ID
278 * @reg: Application register
279 * @val: Pointer to store value read
281 * Read one configuration word from the device using MMA955xL command format.
282 * Commands to the MMA955xL platform consist of a write followed by one or
285 * Locking note: This function must be called with the device lock held.
286 * Locking is not handled inside the function. Callers should ensure they
287 * serialize access to the HW.
289 * Returns: 0 on success, negative value on failure.
291 int mma9551_read_config_word(struct i2c_client
*client
, u8 app_id
,
297 ret
= mma9551_transfer(client
, app_id
, MMA9551_CMD_READ_CONFIG
,
298 reg
, NULL
, 0, (u8
*)&v
, 2);
302 *val
= be16_to_cpu(v
);
306 EXPORT_SYMBOL_NS(mma9551_read_config_word
, "IIO_MMA9551");
309 * mma9551_write_config_word() - write 1 config word
310 * @client: I2C client
311 * @app_id: Application ID
312 * @reg: Application register
313 * @val: Value to write
315 * Write one configuration word from the device using MMA955xL command format.
316 * Commands to the MMA955xL platform consist of a write followed by one or
319 * Locking note: This function must be called with the device lock held.
320 * Locking is not handled inside the function. Callers should ensure they
321 * serialize access to the HW.
323 * Returns: 0 on success, negative value on failure.
325 int mma9551_write_config_word(struct i2c_client
*client
, u8 app_id
,
328 __be16 v
= cpu_to_be16(val
);
330 return mma9551_transfer(client
, app_id
, MMA9551_CMD_WRITE_CONFIG
, reg
,
331 (u8
*)&v
, 2, NULL
, 0);
333 EXPORT_SYMBOL_NS(mma9551_write_config_word
, "IIO_MMA9551");
336 * mma9551_read_status_word() - read 1 status word
337 * @client: I2C client
338 * @app_id: Application ID
339 * @reg: Application register
340 * @val: Pointer to store value read
342 * Read one status word from the device using MMA955xL command format.
343 * Commands to the MMA955xL platform consist of a write followed by one or
346 * Locking note: This function must be called with the device lock held.
347 * Locking is not handled inside the function. Callers should ensure they
348 * serialize access to the HW.
350 * Returns: 0 on success, negative value on failure.
352 int mma9551_read_status_word(struct i2c_client
*client
, u8 app_id
,
358 ret
= mma9551_transfer(client
, app_id
, MMA9551_CMD_READ_STATUS
,
359 reg
, NULL
, 0, (u8
*)&v
, 2);
363 *val
= be16_to_cpu(v
);
367 EXPORT_SYMBOL_NS(mma9551_read_status_word
, "IIO_MMA9551");
370 * mma9551_read_config_words() - read multiple config words
371 * @client: I2C client
372 * @app_id: Application ID
373 * @reg: Application register
374 * @len: Length of array to read (in words)
375 * @buf: Array of words to read
377 * Read multiple configuration registers (word-sized registers).
379 * Locking note: This function must be called with the device lock held.
380 * Locking is not handled inside the function. Callers should ensure they
381 * serialize access to the HW.
383 * Returns: 0 on success, negative value on failure.
385 int mma9551_read_config_words(struct i2c_client
*client
, u8 app_id
,
386 u16 reg
, u8 len
, u16
*buf
)
389 __be16 be_buf
[MMA9551_MAX_MAILBOX_DATA_REGS
/ 2];
391 if (len
> ARRAY_SIZE(be_buf
)) {
392 dev_err(&client
->dev
, "Invalid buffer size %d\n", len
);
396 ret
= mma9551_transfer(client
, app_id
, MMA9551_CMD_READ_CONFIG
,
397 reg
, NULL
, 0, (u8
*)be_buf
, len
* sizeof(u16
));
401 for (i
= 0; i
< len
; i
++)
402 buf
[i
] = be16_to_cpu(be_buf
[i
]);
406 EXPORT_SYMBOL_NS(mma9551_read_config_words
, "IIO_MMA9551");
409 * mma9551_read_status_words() - read multiple status words
410 * @client: I2C client
411 * @app_id: Application ID
412 * @reg: Application register
413 * @len: Length of array to read (in words)
414 * @buf: Array of words to read
416 * Read multiple status registers (word-sized registers).
418 * Locking note: This function must be called with the device lock held.
419 * Locking is not handled inside the function. Callers should ensure they
420 * serialize access to the HW.
422 * Returns: 0 on success, negative value on failure.
424 int mma9551_read_status_words(struct i2c_client
*client
, u8 app_id
,
425 u16 reg
, u8 len
, u16
*buf
)
428 __be16 be_buf
[MMA9551_MAX_MAILBOX_DATA_REGS
/ 2];
430 if (len
> ARRAY_SIZE(be_buf
)) {
431 dev_err(&client
->dev
, "Invalid buffer size %d\n", len
);
435 ret
= mma9551_transfer(client
, app_id
, MMA9551_CMD_READ_STATUS
,
436 reg
, NULL
, 0, (u8
*)be_buf
, len
* sizeof(u16
));
440 for (i
= 0; i
< len
; i
++)
441 buf
[i
] = be16_to_cpu(be_buf
[i
]);
445 EXPORT_SYMBOL_NS(mma9551_read_status_words
, "IIO_MMA9551");
448 * mma9551_write_config_words() - write multiple config words
449 * @client: I2C client
450 * @app_id: Application ID
451 * @reg: Application register
452 * @len: Length of array to write (in words)
453 * @buf: Array of words to write
455 * Write multiple configuration registers (word-sized registers).
457 * Locking note: This function must be called with the device lock held.
458 * Locking is not handled inside the function. Callers should ensure they
459 * serialize access to the HW.
461 * Returns: 0 on success, negative value on failure.
463 int mma9551_write_config_words(struct i2c_client
*client
, u8 app_id
,
464 u16 reg
, u8 len
, u16
*buf
)
467 __be16 be_buf
[(MMA9551_MAX_MAILBOX_DATA_REGS
- 1) / 2];
469 if (len
> ARRAY_SIZE(be_buf
)) {
470 dev_err(&client
->dev
, "Invalid buffer size %d\n", len
);
474 for (i
= 0; i
< len
; i
++)
475 be_buf
[i
] = cpu_to_be16(buf
[i
]);
477 return mma9551_transfer(client
, app_id
, MMA9551_CMD_WRITE_CONFIG
,
478 reg
, (u8
*)be_buf
, len
* sizeof(u16
), NULL
, 0);
480 EXPORT_SYMBOL_NS(mma9551_write_config_words
, "IIO_MMA9551");
483 * mma9551_update_config_bits() - update bits in register
484 * @client: I2C client
485 * @app_id: Application ID
486 * @reg: Application register
487 * @mask: Mask for the bits to update
488 * @val: Value of the bits to update
490 * Update bits in the given register using a bit mask.
492 * Locking note: This function must be called with the device lock held.
493 * Locking is not handled inside the function. Callers should ensure they
494 * serialize access to the HW.
496 * Returns: 0 on success, negative value on failure.
498 int mma9551_update_config_bits(struct i2c_client
*client
, u8 app_id
,
499 u16 reg
, u8 mask
, u8 val
)
504 ret
= mma9551_read_config_byte(client
, app_id
, reg
, &orig
);
514 return mma9551_write_config_byte(client
, app_id
, reg
, tmp
);
516 EXPORT_SYMBOL_NS(mma9551_update_config_bits
, "IIO_MMA9551");
519 * mma9551_gpio_config() - configure gpio
520 * @client: I2C client
521 * @pin: GPIO pin to configure
522 * @app_id: Application ID
523 * @bitnum: Bit number of status register being assigned to the GPIO pin.
524 * @polarity: The polarity parameter is described in section 6.2.2, page 66,
525 * of the Software Reference Manual. Basically, polarity=0 means
526 * the interrupt line has the same value as the selected bit,
527 * while polarity=1 means the line is inverted.
529 * Assign a bit from an application’s status register to a specific GPIO pin.
531 * Locking note: This function must be called with the device lock held.
532 * Locking is not handled inside the function. Callers should ensure they
533 * serialize access to the HW.
535 * Returns: 0 on success, negative value on failure.
537 int mma9551_gpio_config(struct i2c_client
*client
, enum mma9551_gpio_pin pin
,
538 u8 app_id
, u8 bitnum
, int polarity
)
540 u8 reg
, pol_mask
, pol_val
;
543 if (pin
> mma9551_gpio_max
) {
544 dev_err(&client
->dev
, "bad GPIO pin\n");
549 * Pin 6 is configured by regs 0x00 and 0x01, pin 7 by 0x02 and
554 ret
= mma9551_write_config_byte(client
, MMA9551_APPID_GPIO
,
557 dev_err(&client
->dev
, "error setting GPIO app_id\n");
561 ret
= mma9551_write_config_byte(client
, MMA9551_APPID_GPIO
,
564 dev_err(&client
->dev
, "error setting GPIO bit number\n");
570 reg
= MMA9551_GPIO_POL_LSB
;
574 reg
= MMA9551_GPIO_POL_LSB
;
578 reg
= MMA9551_GPIO_POL_MSB
;
582 reg
= MMA9551_GPIO_POL_MSB
;
586 pol_val
= polarity
? pol_mask
: 0;
588 ret
= mma9551_update_config_bits(client
, MMA9551_APPID_GPIO
, reg
,
591 dev_err(&client
->dev
, "error setting GPIO polarity\n");
595 EXPORT_SYMBOL_NS(mma9551_gpio_config
, "IIO_MMA9551");
598 * mma9551_read_version() - read device version information
599 * @client: I2C client
601 * Read version information and print device id and firmware version.
603 * Locking note: This function must be called with the device lock held.
604 * Locking is not handled inside the function. Callers should ensure they
605 * serialize access to the HW.
607 * Returns: 0 on success, negative value on failure.
609 int mma9551_read_version(struct i2c_client
*client
)
611 struct mma9551_version_info info
;
614 ret
= mma9551_transfer(client
, MMA9551_APPID_VERSION
, 0x00, 0x00,
615 NULL
, 0, (u8
*)&info
, sizeof(info
));
619 dev_info(&client
->dev
, "device ID 0x%x, firmware version %02x.%02x\n",
620 be32_to_cpu(info
.device_id
), info
.fw_version
[0],
625 EXPORT_SYMBOL_NS(mma9551_read_version
, "IIO_MMA9551");
628 * mma9551_set_device_state() - sets HW power mode
629 * @client: I2C client
630 * @enable: Use true to power on device, false to cause the device
633 * Set power on/off for device using the Sleep/Wake Application.
634 * When enable is true, power on chip and enable doze mode.
635 * When enable is false, enter sleep mode (device remains in the
636 * lowest-power mode).
638 * Locking note: This function must be called with the device lock held.
639 * Locking is not handled inside the function. Callers should ensure they
640 * serialize access to the HW.
642 * Returns: 0 on success, negative value on failure.
644 int mma9551_set_device_state(struct i2c_client
*client
, bool enable
)
646 return mma9551_update_config_bits(client
, MMA9551_APPID_SLEEP_WAKE
,
648 MMA9551_SLEEP_CFG_SNCEN
|
649 MMA9551_SLEEP_CFG_FLEEN
|
650 MMA9551_SLEEP_CFG_SCHEN
,
651 enable
? MMA9551_SLEEP_CFG_SCHEN
|
652 MMA9551_SLEEP_CFG_FLEEN
:
653 MMA9551_SLEEP_CFG_SNCEN
);
655 EXPORT_SYMBOL_NS(mma9551_set_device_state
, "IIO_MMA9551");
658 * mma9551_set_power_state() - sets runtime PM state
659 * @client: I2C client
660 * @on: Use true to power on device, false to power off
662 * Resume or suspend the device using Runtime PM.
663 * The device will suspend after the autosuspend delay.
665 * Returns: 0 on success, negative value on failure.
667 int mma9551_set_power_state(struct i2c_client
*client
, bool on
)
673 ret
= pm_runtime_resume_and_get(&client
->dev
);
675 pm_runtime_mark_last_busy(&client
->dev
);
676 ret
= pm_runtime_put_autosuspend(&client
->dev
);
680 dev_err(&client
->dev
,
681 "failed to change power state to %d\n", on
);
689 EXPORT_SYMBOL_NS(mma9551_set_power_state
, "IIO_MMA9551");
692 * mma9551_sleep() - sleep
693 * @freq: Application frequency
695 * Firmware applications run at a certain frequency on the
696 * device. Sleep for one application cycle to make sure the
697 * application had time to run once and initialize set values.
699 void mma9551_sleep(int freq
)
701 int sleep_val
= 1000 / freq
;
704 usleep_range(sleep_val
* 1000, 20000);
706 msleep_interruptible(sleep_val
);
708 EXPORT_SYMBOL_NS(mma9551_sleep
, "IIO_MMA9551");
711 * mma9551_read_accel_chan() - read accelerometer channel
712 * @client: I2C client
714 * @val: Pointer to the accelerometer value read
717 * Read accelerometer value for the specified channel.
719 * Locking note: This function must be called with the device lock held.
720 * Locking is not handled inside the function. Callers should ensure they
721 * serialize access to the HW.
723 * Returns: IIO_VAL_INT on success, negative value on failure.
725 int mma9551_read_accel_chan(struct i2c_client
*client
,
726 const struct iio_chan_spec
*chan
,
733 switch (chan
->channel2
) {
735 reg_addr
= MMA9551_AFE_X_ACCEL_REG
;
738 reg_addr
= MMA9551_AFE_Y_ACCEL_REG
;
741 reg_addr
= MMA9551_AFE_Z_ACCEL_REG
;
747 ret
= mma9551_set_power_state(client
, true);
751 ret
= mma9551_read_status_word(client
, MMA9551_APPID_AFE
,
752 reg_addr
, &raw_accel
);
761 mma9551_set_power_state(client
, false);
764 EXPORT_SYMBOL_NS(mma9551_read_accel_chan
, "IIO_MMA9551");
767 * mma9551_read_accel_scale() - read accelerometer scale
768 * @val: Pointer to the accelerometer scale (int value)
769 * @val2: Pointer to the accelerometer scale (micro value)
771 * Read accelerometer scale.
773 * Returns: IIO_VAL_INT_PLUS_MICRO.
775 int mma9551_read_accel_scale(int *val
, int *val2
)
780 return IIO_VAL_INT_PLUS_MICRO
;
782 EXPORT_SYMBOL_NS(mma9551_read_accel_scale
, "IIO_MMA9551");
785 * mma9551_app_reset() - reset application
786 * @client: I2C client
787 * @app_mask: Application to reset
789 * Reset the given application (using the Reset/Suspend/Clear
790 * Control Application)
792 * Returns: 0 on success, negative value on failure.
794 int mma9551_app_reset(struct i2c_client
*client
, u32 app_mask
)
796 return mma9551_write_config_byte(client
, MMA9551_APPID_RSC
,
798 MMA9551_RSC_OFFSET(app_mask
),
799 MMA9551_RSC_VAL(app_mask
));
801 EXPORT_SYMBOL_NS(mma9551_app_reset
, "IIO_MMA9551");
803 MODULE_AUTHOR("Irina Tirdea <irina.tirdea@intel.com>");
804 MODULE_AUTHOR("Vlad Dogaru <vlad.dogaru@intel.com>");
805 MODULE_LICENSE("GPL v2");
806 MODULE_DESCRIPTION("MMA955xL sensors core");