12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 2bd5f47ecSJean Delvare /* 3bd5f47ecSJean Delvare * Apple Motion Sensor driver (I2C variant) 4bd5f47ecSJean Delvare * 5bd5f47ecSJean Delvare * Copyright (C) 2005 Stelian Pop (stelian@popies.net) 6bd5f47ecSJean Delvare * Copyright (C) 2006 Michael Hanselmann (linux-kernel@hansmi.ch) 7bd5f47ecSJean Delvare * 8bd5f47ecSJean Delvare * Clean room implementation based on the reverse engineered Mac OS X driver by 9bd5f47ecSJean Delvare * Johannes Berg <johannes@sipsolutions.net>, documentation available at 10bd5f47ecSJean Delvare * http://johannes.sipsolutions.net/PowerBook/Apple_Motion_Sensor_Specification 11bd5f47ecSJean Delvare */ 12bd5f47ecSJean Delvare 13bd5f47ecSJean Delvare #include <linux/module.h> 14bd5f47ecSJean Delvare #include <linux/types.h> 15bd5f47ecSJean Delvare #include <linux/errno.h> 16bd5f47ecSJean Delvare #include <linux/init.h> 17bd5f47ecSJean Delvare #include <linux/delay.h> 18bd5f47ecSJean Delvare 19bd5f47ecSJean Delvare #include "ams.h" 20bd5f47ecSJean Delvare 21bd5f47ecSJean Delvare /* AMS registers */ 22bd5f47ecSJean Delvare #define AMS_COMMAND 0x00 /* command register */ 23bd5f47ecSJean Delvare #define AMS_STATUS 0x01 /* status register */ 24bd5f47ecSJean Delvare #define AMS_CTRL1 0x02 /* read control 1 (number of values) */ 25bd5f47ecSJean Delvare #define AMS_CTRL2 0x03 /* read control 2 (offset?) */ 26bd5f47ecSJean Delvare #define AMS_CTRL3 0x04 /* read control 3 (size of each value?) */ 27bd5f47ecSJean Delvare #define AMS_DATA1 0x05 /* read data 1 */ 28bd5f47ecSJean Delvare #define AMS_DATA2 0x06 /* read data 2 */ 29bd5f47ecSJean Delvare #define AMS_DATA3 0x07 /* read data 3 */ 30bd5f47ecSJean Delvare #define AMS_DATA4 0x08 /* read data 4 */ 31bd5f47ecSJean Delvare #define AMS_DATAX 0x20 /* data X */ 32bd5f47ecSJean Delvare #define AMS_DATAY 0x21 /* data Y */ 33bd5f47ecSJean Delvare #define AMS_DATAZ 0x22 /* data Z */ 34bd5f47ecSJean Delvare #define AMS_FREEFALL 0x24 /* freefall int control */ 35bd5f47ecSJean Delvare #define AMS_SHOCK 0x25 /* shock int control */ 36bd5f47ecSJean Delvare #define AMS_SENSLOW 0x26 /* sensitivity low limit */ 37bd5f47ecSJean Delvare #define AMS_SENSHIGH 0x27 /* sensitivity high limit */ 38bd5f47ecSJean Delvare #define AMS_CTRLX 0x28 /* control X */ 39bd5f47ecSJean Delvare #define AMS_CTRLY 0x29 /* control Y */ 40bd5f47ecSJean Delvare #define AMS_CTRLZ 0x2A /* control Z */ 41bd5f47ecSJean Delvare #define AMS_UNKNOWN1 0x2B /* unknown 1 */ 42bd5f47ecSJean Delvare #define AMS_UNKNOWN2 0x2C /* unknown 2 */ 43bd5f47ecSJean Delvare #define AMS_UNKNOWN3 0x2D /* unknown 3 */ 44bd5f47ecSJean Delvare #define AMS_VENDOR 0x2E /* vendor */ 45bd5f47ecSJean Delvare 46bd5f47ecSJean Delvare /* AMS commands - use with the AMS_COMMAND register */ 47bd5f47ecSJean Delvare enum ams_i2c_cmd { 48bd5f47ecSJean Delvare AMS_CMD_NOOP = 0, 49bd5f47ecSJean Delvare AMS_CMD_VERSION, 50bd5f47ecSJean Delvare AMS_CMD_READMEM, 51bd5f47ecSJean Delvare AMS_CMD_WRITEMEM, 52bd5f47ecSJean Delvare AMS_CMD_ERASEMEM, 53bd5f47ecSJean Delvare AMS_CMD_READEE, 54bd5f47ecSJean Delvare AMS_CMD_WRITEEE, 55bd5f47ecSJean Delvare AMS_CMD_RESET, 56bd5f47ecSJean Delvare AMS_CMD_START, 57bd5f47ecSJean Delvare }; 58bd5f47ecSJean Delvare 59*8daa9c1dSUwe Kleine-König static int ams_i2c_probe(struct i2c_client *client); 602dec3a7aSUwe Kleine-König static void ams_i2c_remove(struct i2c_client *client); 61bd5f47ecSJean Delvare 62bd5f47ecSJean Delvare static const struct i2c_device_id ams_id[] = { 6381e5d864SBenjamin Herrenschmidt { "MAC,accelerometer_1", 0 }, 64bd5f47ecSJean Delvare { } 65bd5f47ecSJean Delvare }; 66bd5f47ecSJean Delvare MODULE_DEVICE_TABLE(i2c, ams_id); 67bd5f47ecSJean Delvare 68bd5f47ecSJean Delvare static struct i2c_driver ams_i2c_driver = { 69bd5f47ecSJean Delvare .driver = { 70bd5f47ecSJean Delvare .name = "ams", 71bd5f47ecSJean Delvare }, 72*8daa9c1dSUwe Kleine-König .probe_new = ams_i2c_probe, 73bd5f47ecSJean Delvare .remove = ams_i2c_remove, 74bd5f47ecSJean Delvare .id_table = ams_id, 75bd5f47ecSJean Delvare }; 76bd5f47ecSJean Delvare 77bd5f47ecSJean Delvare static s32 ams_i2c_read(u8 reg) 78bd5f47ecSJean Delvare { 79bd5f47ecSJean Delvare return i2c_smbus_read_byte_data(ams_info.i2c_client, reg); 80bd5f47ecSJean Delvare } 81bd5f47ecSJean Delvare 82bd5f47ecSJean Delvare static int ams_i2c_write(u8 reg, u8 value) 83bd5f47ecSJean Delvare { 84bd5f47ecSJean Delvare return i2c_smbus_write_byte_data(ams_info.i2c_client, reg, value); 85bd5f47ecSJean Delvare } 86bd5f47ecSJean Delvare 87bd5f47ecSJean Delvare static int ams_i2c_cmd(enum ams_i2c_cmd cmd) 88bd5f47ecSJean Delvare { 89bd5f47ecSJean Delvare s32 result; 90bd5f47ecSJean Delvare int count = 3; 91bd5f47ecSJean Delvare 92bd5f47ecSJean Delvare ams_i2c_write(AMS_COMMAND, cmd); 93bd5f47ecSJean Delvare msleep(5); 94bd5f47ecSJean Delvare 95bd5f47ecSJean Delvare while (count--) { 96bd5f47ecSJean Delvare result = ams_i2c_read(AMS_COMMAND); 97bd5f47ecSJean Delvare if (result == 0 || result & 0x80) 98bd5f47ecSJean Delvare return 0; 99bd5f47ecSJean Delvare 100bd5f47ecSJean Delvare schedule_timeout_uninterruptible(HZ / 20); 101bd5f47ecSJean Delvare } 102bd5f47ecSJean Delvare 103bd5f47ecSJean Delvare return -1; 104bd5f47ecSJean Delvare } 105bd5f47ecSJean Delvare 106bd5f47ecSJean Delvare static void ams_i2c_set_irq(enum ams_irq reg, char enable) 107bd5f47ecSJean Delvare { 108bd5f47ecSJean Delvare if (reg & AMS_IRQ_FREEFALL) { 109bd5f47ecSJean Delvare u8 val = ams_i2c_read(AMS_CTRLX); 110bd5f47ecSJean Delvare if (enable) 111bd5f47ecSJean Delvare val |= 0x80; 112bd5f47ecSJean Delvare else 113bd5f47ecSJean Delvare val &= ~0x80; 114bd5f47ecSJean Delvare ams_i2c_write(AMS_CTRLX, val); 115bd5f47ecSJean Delvare } 116bd5f47ecSJean Delvare 117bd5f47ecSJean Delvare if (reg & AMS_IRQ_SHOCK) { 118bd5f47ecSJean Delvare u8 val = ams_i2c_read(AMS_CTRLY); 119bd5f47ecSJean Delvare if (enable) 120bd5f47ecSJean Delvare val |= 0x80; 121bd5f47ecSJean Delvare else 122bd5f47ecSJean Delvare val &= ~0x80; 123bd5f47ecSJean Delvare ams_i2c_write(AMS_CTRLY, val); 124bd5f47ecSJean Delvare } 125bd5f47ecSJean Delvare 126bd5f47ecSJean Delvare if (reg & AMS_IRQ_GLOBAL) { 127bd5f47ecSJean Delvare u8 val = ams_i2c_read(AMS_CTRLZ); 128bd5f47ecSJean Delvare if (enable) 129bd5f47ecSJean Delvare val |= 0x80; 130bd5f47ecSJean Delvare else 131bd5f47ecSJean Delvare val &= ~0x80; 132bd5f47ecSJean Delvare ams_i2c_write(AMS_CTRLZ, val); 133bd5f47ecSJean Delvare } 134bd5f47ecSJean Delvare } 135bd5f47ecSJean Delvare 136bd5f47ecSJean Delvare static void ams_i2c_clear_irq(enum ams_irq reg) 137bd5f47ecSJean Delvare { 138bd5f47ecSJean Delvare if (reg & AMS_IRQ_FREEFALL) 139bd5f47ecSJean Delvare ams_i2c_write(AMS_FREEFALL, 0); 140bd5f47ecSJean Delvare 141bd5f47ecSJean Delvare if (reg & AMS_IRQ_SHOCK) 142bd5f47ecSJean Delvare ams_i2c_write(AMS_SHOCK, 0); 143bd5f47ecSJean Delvare } 144bd5f47ecSJean Delvare 145bd5f47ecSJean Delvare static u8 ams_i2c_get_vendor(void) 146bd5f47ecSJean Delvare { 147bd5f47ecSJean Delvare return ams_i2c_read(AMS_VENDOR); 148bd5f47ecSJean Delvare } 149bd5f47ecSJean Delvare 150bd5f47ecSJean Delvare static void ams_i2c_get_xyz(s8 *x, s8 *y, s8 *z) 151bd5f47ecSJean Delvare { 152bd5f47ecSJean Delvare *x = ams_i2c_read(AMS_DATAX); 153bd5f47ecSJean Delvare *y = ams_i2c_read(AMS_DATAY); 154bd5f47ecSJean Delvare *z = ams_i2c_read(AMS_DATAZ); 155bd5f47ecSJean Delvare } 156bd5f47ecSJean Delvare 157*8daa9c1dSUwe Kleine-König static int ams_i2c_probe(struct i2c_client *client) 158bd5f47ecSJean Delvare { 159bd5f47ecSJean Delvare int vmaj, vmin; 160bd5f47ecSJean Delvare int result; 161bd5f47ecSJean Delvare 162bd5f47ecSJean Delvare /* There can be only one */ 163bd5f47ecSJean Delvare if (unlikely(ams_info.has_device)) 164bd5f47ecSJean Delvare return -ENODEV; 165bd5f47ecSJean Delvare 166bd5f47ecSJean Delvare ams_info.i2c_client = client; 167bd5f47ecSJean Delvare 168bd5f47ecSJean Delvare if (ams_i2c_cmd(AMS_CMD_RESET)) { 169bd5f47ecSJean Delvare printk(KERN_INFO "ams: Failed to reset the device\n"); 170bd5f47ecSJean Delvare return -ENODEV; 171bd5f47ecSJean Delvare } 172bd5f47ecSJean Delvare 173bd5f47ecSJean Delvare if (ams_i2c_cmd(AMS_CMD_START)) { 174bd5f47ecSJean Delvare printk(KERN_INFO "ams: Failed to start the device\n"); 175bd5f47ecSJean Delvare return -ENODEV; 176bd5f47ecSJean Delvare } 177bd5f47ecSJean Delvare 178bd5f47ecSJean Delvare /* get version/vendor information */ 179bd5f47ecSJean Delvare ams_i2c_write(AMS_CTRL1, 0x02); 180bd5f47ecSJean Delvare ams_i2c_write(AMS_CTRL2, 0x85); 181bd5f47ecSJean Delvare ams_i2c_write(AMS_CTRL3, 0x01); 182bd5f47ecSJean Delvare 183bd5f47ecSJean Delvare ams_i2c_cmd(AMS_CMD_READMEM); 184bd5f47ecSJean Delvare 185bd5f47ecSJean Delvare vmaj = ams_i2c_read(AMS_DATA1); 186bd5f47ecSJean Delvare vmin = ams_i2c_read(AMS_DATA2); 187bd5f47ecSJean Delvare if (vmaj != 1 || vmin != 52) { 188bd5f47ecSJean Delvare printk(KERN_INFO "ams: Incorrect device version (%d.%d)\n", 189bd5f47ecSJean Delvare vmaj, vmin); 190bd5f47ecSJean Delvare return -ENODEV; 191bd5f47ecSJean Delvare } 192bd5f47ecSJean Delvare 193bd5f47ecSJean Delvare ams_i2c_cmd(AMS_CMD_VERSION); 194bd5f47ecSJean Delvare 195bd5f47ecSJean Delvare vmaj = ams_i2c_read(AMS_DATA1); 196bd5f47ecSJean Delvare vmin = ams_i2c_read(AMS_DATA2); 197bd5f47ecSJean Delvare if (vmaj != 0 || vmin != 1) { 198bd5f47ecSJean Delvare printk(KERN_INFO "ams: Incorrect firmware version (%d.%d)\n", 199bd5f47ecSJean Delvare vmaj, vmin); 200bd5f47ecSJean Delvare return -ENODEV; 201bd5f47ecSJean Delvare } 202bd5f47ecSJean Delvare 203bd5f47ecSJean Delvare /* Disable interrupts */ 204bd5f47ecSJean Delvare ams_i2c_set_irq(AMS_IRQ_ALL, 0); 205bd5f47ecSJean Delvare 206bd5f47ecSJean Delvare result = ams_sensor_attach(); 207bd5f47ecSJean Delvare if (result < 0) 208bd5f47ecSJean Delvare return result; 209bd5f47ecSJean Delvare 210bd5f47ecSJean Delvare /* Set default values */ 211bd5f47ecSJean Delvare ams_i2c_write(AMS_SENSLOW, 0x15); 212bd5f47ecSJean Delvare ams_i2c_write(AMS_SENSHIGH, 0x60); 213bd5f47ecSJean Delvare ams_i2c_write(AMS_CTRLX, 0x08); 214bd5f47ecSJean Delvare ams_i2c_write(AMS_CTRLY, 0x0F); 215bd5f47ecSJean Delvare ams_i2c_write(AMS_CTRLZ, 0x4F); 216bd5f47ecSJean Delvare ams_i2c_write(AMS_UNKNOWN1, 0x14); 217bd5f47ecSJean Delvare 218bd5f47ecSJean Delvare /* Clear interrupts */ 219bd5f47ecSJean Delvare ams_i2c_clear_irq(AMS_IRQ_ALL); 220bd5f47ecSJean Delvare 221bd5f47ecSJean Delvare ams_info.has_device = 1; 222bd5f47ecSJean Delvare 223bd5f47ecSJean Delvare /* Enable interrupts */ 224bd5f47ecSJean Delvare ams_i2c_set_irq(AMS_IRQ_ALL, 1); 225bd5f47ecSJean Delvare 226bd5f47ecSJean Delvare printk(KERN_INFO "ams: Found I2C based motion sensor\n"); 227bd5f47ecSJean Delvare 228bd5f47ecSJean Delvare return 0; 229bd5f47ecSJean Delvare } 230bd5f47ecSJean Delvare 231ed5c2f5fSUwe Kleine-König static void ams_i2c_remove(struct i2c_client *client) 232bd5f47ecSJean Delvare { 233bd5f47ecSJean Delvare if (ams_info.has_device) { 234bd5f47ecSJean Delvare ams_sensor_detach(); 235bd5f47ecSJean Delvare 236bd5f47ecSJean Delvare /* Disable interrupts */ 237bd5f47ecSJean Delvare ams_i2c_set_irq(AMS_IRQ_ALL, 0); 238bd5f47ecSJean Delvare 239bd5f47ecSJean Delvare /* Clear interrupts */ 240bd5f47ecSJean Delvare ams_i2c_clear_irq(AMS_IRQ_ALL); 241bd5f47ecSJean Delvare 242bd5f47ecSJean Delvare printk(KERN_INFO "ams: Unloading\n"); 243bd5f47ecSJean Delvare 244bd5f47ecSJean Delvare ams_info.has_device = 0; 245bd5f47ecSJean Delvare } 246bd5f47ecSJean Delvare } 247bd5f47ecSJean Delvare 248bd5f47ecSJean Delvare static void ams_i2c_exit(void) 249bd5f47ecSJean Delvare { 250bd5f47ecSJean Delvare i2c_del_driver(&ams_i2c_driver); 251bd5f47ecSJean Delvare } 252bd5f47ecSJean Delvare 253bd5f47ecSJean Delvare int __init ams_i2c_init(struct device_node *np) 254bd5f47ecSJean Delvare { 255bd5f47ecSJean Delvare /* Set implementation stuff */ 256bd5f47ecSJean Delvare ams_info.of_node = np; 257bd5f47ecSJean Delvare ams_info.exit = ams_i2c_exit; 258bd5f47ecSJean Delvare ams_info.get_vendor = ams_i2c_get_vendor; 259bd5f47ecSJean Delvare ams_info.get_xyz = ams_i2c_get_xyz; 260bd5f47ecSJean Delvare ams_info.clear_irq = ams_i2c_clear_irq; 261bd5f47ecSJean Delvare ams_info.bustype = BUS_I2C; 262bd5f47ecSJean Delvare 263e9bb94cdSMinghao Chi return i2c_add_driver(&ams_i2c_driver); 264bd5f47ecSJean Delvare } 265