- 根目录:
- drivers
- staging
- iio
- magnetometer
- hmc5843.c
#include <linux/module.h>
#include <linux/init.h>
#include <linux/i2c.h>
#include <linux/slab.h>
#include <linux/types.h>
#include "../iio.h"
#include "../sysfs.h"
#include "magnet.h"
#define HMC5843_I2C_ADDRESS 0x1E
#define HMC5843_CONFIG_REG_A 0x00
#define HMC5843_CONFIG_REG_B 0x01
#define HMC5843_MODE_REG 0x02
#define HMC5843_DATA_OUT_X_MSB_REG 0x03
#define HMC5843_DATA_OUT_X_LSB_REG 0x04
#define HMC5843_DATA_OUT_Y_MSB_REG 0x05
#define HMC5843_DATA_OUT_Y_LSB_REG 0x06
#define HMC5843_DATA_OUT_Z_MSB_REG 0x07
#define HMC5843_DATA_OUT_Z_LSB_REG 0x08
#define HMC5843_STATUS_REG 0x09
#define HMC5843_ID_REG_A 0x0A
#define HMC5843_ID_REG_B 0x0B
#define HMC5843_ID_REG_C 0x0C
#define HMC5843_ID_REG_LENGTH 0x03
#define HMC5843_ID_STRING "H43"
#define RANGE_GAIN_OFFSET 0x05
#define RANGE_0_7 0x00
#define RANGE_1_0 0x01
#define RANGE_1_5 0x02
#define RANGE_2_0 0x03
#define RANGE_3_2 0x04
#define RANGE_3_8 0x05
#define RANGE_4_5 0x06
#define RANGE_6_5 0x07
#define DATA_READY 0x01
#define DATA_OUTPUT_LOCK 0x02
#define VOLTAGE_REGULATOR_ENABLED 0x04
#define MODE_CONVERSION_CONTINUOUS 0x00
#define MODE_CONVERSION_SINGLE 0x01
#define MODE_IDLE 0x02
#define MODE_SLEEP 0x03
#define RATE_OFFSET 0x02
#define RATE_BITMASK 0x1C
#define RATE_5 0x00
#define RATE_10 0x01
#define RATE_20 0x02
#define RATE_50 0x03
#define RATE_100 0x04
#define RATE_200 0x05
#define RATE_500 0x06
#define RATE_NOT_USED 0x07
#define CONF_NORMAL 0x00
#define CONF_POSITIVE_BIAS 0x01
#define CONF_NEGATIVE_BIAS 0x02
#define CONF_NOT_USED 0x03
#define MEAS_CONF_MASK 0x03
static const char *regval_to_scale[] = {
"0.0000006173",
"0.0000007692",
"0.0000010309",
"0.0000012821",
"0.0000018868",
"0.0000021739",
"0.0000025641",
"0.0000035714",
};
static const int regval_to_input_field_mg[] = {
700,
1000,
1500,
2000,
3200,
3800,
4500,
6500
};
static const char *regval_to_samp_freq[] = {
"0.5",
"1",
"2",
"5",
"10",
"20",
"50",
};
static const unsigned short normal_i2c[] = { HMC5843_I2C_ADDRESS,
I2C_CLIENT_END };
struct hmc5843_data {
struct iio_dev *indio_dev;
struct mutex lock;
u8 rate;
u8 meas_conf;
u8 operating_mode;
u8 range;
};
static void hmc5843_init_client(struct i2c_client *client);
static s32 hmc5843_configure(struct i2c_client *client,
u8 operating_mode)
{
return i2c_smbus_write_byte_data(client,
HMC5843_MODE_REG,
(operating_mode & 0x03));
}
static ssize_t hmc5843_read_measurement(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct iio_dev *indio_dev = dev_get_drvdata(dev);
struct i2c_client *client = to_i2c_client(indio_dev->dev.parent);
s16 coordinate_val;
struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
struct hmc5843_data *data = indio_dev->dev_data;
s32 result;
mutex_lock(&data->lock);
result = i2c_smbus_read_byte_data(client, HMC5843_STATUS_REG);
while (!(result & DATA_READY))
result = i2c_smbus_read_byte_data(client, HMC5843_STATUS_REG);
result = i2c_smbus_read_word_data(client, this_attr->address);
mutex_unlock(&data->lock);
if (result < 0)
return -EINVAL;
coordinate_val = (s16)swab16((u16)result);
return sprintf(buf, "%d\n", coordinate_val);
}
static IIO_DEV_ATTR_MAGN_X(hmc5843_read_measurement,
HMC5843_DATA_OUT_X_MSB_REG);
static IIO_DEV_ATTR_MAGN_Y(hmc5843_read_measurement,
HMC5843_DATA_OUT_Y_MSB_REG);
static IIO_DEV_ATTR_MAGN_Z(hmc5843_read_measurement,
HMC5843_DATA_OUT_Z_MSB_REG);
static ssize_t hmc5843_show_operating_mode(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct iio_dev *indio_dev = dev_get_drvdata(dev);
struct hmc5843_data *data = indio_dev->dev_data;
return sprintf(buf, "%d\n", data->operating_mode);
}
static ssize_t hmc5843_set_operating_mode(struct device *dev,
struct device_attribute *attr,
const char *buf,
size_t count)
{
struct iio_dev *indio_dev = dev_get_drvdata(dev);
struct i2c_client *client = to_i2c_client(indio_dev->dev.parent);
struct hmc5843_data *data = indio_dev->dev_data;
struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
unsigned long operating_mode = 0;
s32 status;
int error;
mutex_lock(&data->lock);
error = strict_strtoul(buf, 10, &operating_mode);
if (error) {
count = error;
goto exit;
}
dev_dbg(dev, "set Conversion mode to %lu\n", operating_mode);
if (operating_mode > MODE_SLEEP) {
count = -EINVAL;
goto exit;
}
status = i2c_smbus_write_byte_data(client, this_attr->address,
operating_mode);
if (status) {
count = -EINVAL;
goto exit;
}
data->operating_mode = operating_mode;
exit:
mutex_unlock(&data->lock);
return count;
}
static IIO_DEVICE_ATTR(operating_mode,
S_IWUSR | S_IRUGO,
hmc5843_show_operating_mode,
hmc5843_set_operating_mode,
HMC5843_MODE_REG);
static s32 hmc5843_set_meas_conf(struct i2c_client *client,
u8 meas_conf)
{
struct hmc5843_data *data = i2c_get_clientdata(client);
u8 reg_val;
reg_val = (meas_conf & MEAS_CONF_MASK) | (data->rate << RATE_OFFSET);
return i2c_smbus_write_byte_data(client, HMC5843_CONFIG_REG_A, reg_val);
}
static ssize_t hmc5843_show_measurement_configuration(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct iio_dev *indio_dev = dev_get_drvdata(dev);
struct hmc5843_data *data = indio_dev->dev_data;
return sprintf(buf, "%d\n", data->meas_conf);
}
static ssize_t hmc5843_set_measurement_configuration(struct device *dev,
struct device_attribute *attr,
const char *buf,
size_t count)
{
struct iio_dev *indio_dev = dev_get_drvdata(dev);
struct i2c_client *client = to_i2c_client(indio_dev->dev.parent);
struct hmc5843_data *data = i2c_get_clientdata(client);
unsigned long meas_conf = 0;
int error = strict_strtoul(buf, 10, &meas_conf);
if (error)
return error;
mutex_lock(&data->lock);
dev_dbg(dev, "set mode to %lu\n", meas_conf);
if (hmc5843_set_meas_conf(client, meas_conf)) {
count = -EINVAL;
goto exit;
}
data->meas_conf = meas_conf;
exit:
mutex_unlock(&data->lock);
return count;
}
static IIO_DEVICE_ATTR(meas_conf,
S_IWUSR | S_IRUGO,
hmc5843_show_measurement_configuration,
hmc5843_set_measurement_configuration,
0);
static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("0.5 1 2 5 10 20 50");
static s32 hmc5843_set_rate(struct i2c_client *client,
u8 rate)
{
struct hmc5843_data *data = i2c_get_clientdata(client);
u8 reg_val;
reg_val = (data->meas_conf) | (rate << RATE_OFFSET);
if (rate >= RATE_NOT_USED) {
dev_err(&client->dev,
"This data output rate is not supported \n");
return -EINVAL;
}
return i2c_smbus_write_byte_data(client, HMC5843_CONFIG_REG_A, reg_val);
}
static ssize_t set_sampling_frequency(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
struct iio_dev *indio_dev = dev_get_drvdata(dev);
struct i2c_client *client = to_i2c_client(indio_dev->dev.parent);
struct hmc5843_data *data = indio_dev->dev_data;
unsigned long rate = 0;
if (strncmp(buf, "0.5" , 3) == 0)
rate = RATE_5;
else if (strncmp(buf, "1" , 1) == 0)
rate = RATE_10;
else if (strncmp(buf, "2", 1) == 0)
rate = RATE_20;
else if (strncmp(buf, "5", 1) == 0)
rate = RATE_50;
else if (strncmp(buf, "10", 2) == 0)
rate = RATE_100;
else if (strncmp(buf, "20" , 2) == 0)
rate = RATE_200;
else if (strncmp(buf, "50" , 2) == 0)
rate = RATE_500;
else
return -EINVAL;
mutex_lock(&data->lock);
dev_dbg(dev, "set rate to %lu\n", rate);
if (hmc5843_set_rate(client, rate)) {
count = -EINVAL;
goto exit;
}
data->rate = rate;
exit:
mutex_unlock(&data->lock);
return count;
}
static ssize_t show_sampling_frequency(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct iio_dev *indio_dev = dev_get_drvdata(dev);
struct i2c_client *client = to_i2c_client(indio_dev->dev.parent);
struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
s32 rate;
rate = i2c_smbus_read_byte_data(client, this_attr->address);
if (rate < 0)
return rate;
rate = (rate & RATE_BITMASK) >> RATE_OFFSET;
return sprintf(buf, "%s\n", regval_to_samp_freq[rate]);
}
static IIO_DEVICE_ATTR(sampling_frequency,
S_IWUSR | S_IRUGO,
show_sampling_frequency,
set_sampling_frequency,
HMC5843_CONFIG_REG_A);
static ssize_t show_range(struct device *dev,
struct device_attribute *attr,
char *buf)
{
u8 range;
struct iio_dev *indio_dev = dev_get_drvdata(dev);
struct hmc5843_data *data = indio_dev->dev_data;
range = data->range;
return sprintf(buf, "%d\n", regval_to_input_field_mg[range]);
}
static ssize_t set_range(struct device *dev,
struct device_attribute *attr,
const char *buf,
size_t count)
{
struct iio_dev *indio_dev = dev_get_drvdata(dev);
struct i2c_client *client = to_i2c_client(indio_dev->dev.parent);
struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
struct hmc5843_data *data = indio_dev->dev_data;
unsigned long range = 0;
int error;
mutex_lock(&data->lock);
error = strict_strtoul(buf, 10, &range);
if (error) {
count = error;
goto exit;
}
dev_dbg(dev, "set range to %lu\n", range);
if (range > RANGE_6_5) {
count = -EINVAL;
goto exit;
}
data->range = range;
range = range << RANGE_GAIN_OFFSET;
if (i2c_smbus_write_byte_data(client, this_attr->address, range))
count = -EINVAL;
exit:
mutex_unlock(&data->lock);
return count;
}
static IIO_DEVICE_ATTR(magn_range,
S_IWUSR | S_IRUGO,
show_range,
set_range,
HMC5843_CONFIG_REG_B);
static ssize_t show_scale(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct iio_dev *indio_dev = dev_get_drvdata(dev);
struct hmc5843_data *data = indio_dev->dev_data;
return strlen(strcpy(buf, regval_to_scale[data->range]));
}
static IIO_DEVICE_ATTR(magn_scale,
S_IRUGO,
show_scale,
NULL , 0);
static struct attribute *hmc5843_attributes[] = {
&iio_dev_attr_meas_conf.dev_attr.attr,
&iio_dev_attr_operating_mode.dev_attr.attr,
&iio_dev_attr_sampling_frequency.dev_attr.attr,
&iio_dev_attr_magn_range.dev_attr.attr,
&iio_dev_attr_magn_scale.dev_attr.attr,
&iio_dev_attr_magn_x_raw.dev_attr.attr,
&iio_dev_attr_magn_y_raw.dev_attr.attr,
&iio_dev_attr_magn_z_raw.dev_attr.attr,
&iio_const_attr_sampling_frequency_available.dev_attr.attr,
NULL
};
static const struct attribute_group hmc5843_group = {
.attrs = hmc5843_attributes,
};
static int hmc5843_detect(struct i2c_client *client,
struct i2c_board_info *info)
{
unsigned char id_str[HMC5843_ID_REG_LENGTH];
if (client->addr != HMC5843_I2C_ADDRESS)
return -ENODEV;
if (i2c_smbus_read_i2c_block_data(client, HMC5843_ID_REG_A,
HMC5843_ID_REG_LENGTH, id_str)
!= HMC5843_ID_REG_LENGTH)
return -ENODEV;
if (0 != strncmp(id_str, HMC5843_ID_STRING, HMC5843_ID_REG_LENGTH))
return -ENODEV;
return 0;
}
static void hmc5843_init_client(struct i2c_client *client)
{
struct hmc5843_data *data = i2c_get_clientdata(client);
hmc5843_set_meas_conf(client, data->meas_conf);
hmc5843_set_rate(client, data->rate);
hmc5843_configure(client, data->operating_mode);
i2c_smbus_write_byte_data(client, HMC5843_CONFIG_REG_B, data->range);
mutex_init(&data->lock);
pr_info("HMC5843 initialized\n");
}
static int hmc5843_probe(struct i2c_client *client,
const struct i2c_device_id *id)
{
struct hmc5843_data *data;
int err = 0;
data = kzalloc(sizeof(struct hmc5843_data), GFP_KERNEL);
if (!data) {
err = -ENOMEM;
goto exit;
}
data->meas_conf = CONF_NORMAL;
data->range = RANGE_1_0;
data->operating_mode = MODE_CONVERSION_CONTINUOUS;
i2c_set_clientdata(client, data);
hmc5843_init_client(client);
data->indio_dev = iio_allocate_device();
if (!data->indio_dev) {
err = -ENOMEM;
goto exit_free1;
}
data->indio_dev->attrs = &hmc5843_group;
data->indio_dev->dev.parent = &client->dev;
data->indio_dev->dev_data = (void *)(data);
data->indio_dev->driver_module = THIS_MODULE;
data->indio_dev->modes = INDIO_DIRECT_MODE;
err = iio_device_register(data->indio_dev);
if (err)
goto exit_free2;
return 0;
exit_free2:
iio_free_device(data->indio_dev);
exit_free1:
kfree(data);
exit:
return err;
}
static int hmc5843_remove(struct i2c_client *client)
{
struct hmc5843_data *data = i2c_get_clientdata(client);
hmc5843_configure(client, MODE_SLEEP);
iio_device_unregister(data->indio_dev);
kfree(i2c_get_clientdata(client));
return 0;
}
static int hmc5843_suspend(struct i2c_client *client, pm_message_t mesg)
{
hmc5843_configure(client, MODE_SLEEP);
return 0;
}
static int hmc5843_resume(struct i2c_client *client)
{
struct hmc5843_data *data = i2c_get_clientdata(client);
hmc5843_configure(client, data->operating_mode);
return 0;
}
static const struct i2c_device_id hmc5843_id[] = {
{ "hmc5843", 0 },
{ }
};
static struct i2c_driver hmc5843_driver = {
.driver = {
.name = "hmc5843",
},
.id_table = hmc5843_id,
.probe = hmc5843_probe,
.remove = hmc5843_remove,
.detect = hmc5843_detect,
.address_list = normal_i2c,
.suspend = hmc5843_suspend,
.resume = hmc5843_resume,
};
static int __init hmc5843_init(void)
{
return i2c_add_driver(&hmc5843_driver);
}
static void __exit hmc5843_exit(void)
{
i2c_del_driver(&hmc5843_driver);
}
MODULE_AUTHOR("Shubhrajyoti Datta <shubhrajyoti@ti.com");
MODULE_DESCRIPTION("HMC5843 driver");
MODULE_LICENSE("GPL");
module_init(hmc5843_init);
module_exit(hmc5843_exit);
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240
- 241
- 242
- 243
- 244
- 245
- 246
- 247
- 248
- 249
- 250
- 251
- 252
- 253
- 254
- 255
- 256
- 257
- 258
- 259
- 260
- 261
- 262
- 263
- 264
- 265
- 266
- 267
- 268
- 269
- 270
- 271
- 272
- 273
- 274
- 275
- 276
- 277
- 278
- 279
- 280
- 281
- 282
- 283
- 284
- 285
- 286
- 287
- 288
- 289
- 290
- 291
- 292
- 293
- 294
- 295
- 296
- 297
- 298
- 299
- 300
- 301
- 302
- 303
- 304
- 305
- 306
- 307
- 308
- 309
- 310
- 311
- 312
- 313
- 314
- 315
- 316
- 317
- 318
- 319
- 320
- 321
- 322
- 323
- 324
- 325
- 326
- 327
- 328
- 329
- 330
- 331
- 332
- 333
- 334
- 335
- 336
- 337
- 338
- 339
- 340
- 341
- 342
- 343
- 344
- 345
- 346
- 347
- 348
- 349
- 350
- 351
- 352
- 353
- 354
- 355
- 356
- 357
- 358
- 359
- 360
- 361
- 362
- 363
- 364
- 365
- 366
- 367
- 368
- 369
- 370
- 371
- 372
- 373
- 374
- 375
- 376
- 377
- 378
- 379
- 380
- 381
- 382
- 383
- 384
- 385
- 386
- 387
- 388
- 389
- 390
- 391
- 392
- 393
- 394
- 395
- 396
- 397
- 398
- 399
- 400
- 401
- 402
- 403
- 404
- 405
- 406
- 407
- 408
- 409
- 410
- 411
- 412
- 413
- 414
- 415
- 416
- 417
- 418
- 419
- 420
- 421
- 422
- 423
- 424
- 425
- 426
- 427
- 428
- 429
- 430
- 431
- 432
- 433
- 434
- 435
- 436
- 437
- 438
- 439
- 440
- 441
- 442
- 443
- 444
- 445
- 446
- 447
- 448
- 449
- 450
- 451
- 452
- 453
- 454
- 455
- 456
- 457
- 458
- 459
- 460
- 461
- 462
- 463
- 464
- 465
- 466
- 467
- 468
- 469
- 470
- 471
- 472
- 473
- 474
- 475
- 476
- 477
- 478
- 479
- 480
- 481
- 482
- 483
- 484
- 485
- 486
- 487
- 488
- 489
- 490
- 491
- 492
- 493
- 494
- 495
- 496
- 497
- 498
- 499
- 500
- 501
- 502
- 503
- 504
- 505
- 506
- 507
- 508
- 509
- 510
- 511
- 512
- 513
- 514
- 515
- 516
- 517
- 518
- 519
- 520
- 521
- 522
- 523
- 524
- 525
- 526
- 527
- 528
- 529
- 530
- 531
- 532
- 533
- 534
- 535
- 536
- 537
- 538
- 539
- 540
- 541
- 542
- 543
- 544
- 545
- 546
- 547
- 548
- 549
- 550
- 551
- 552
- 553
- 554
- 555
- 556
- 557
- 558
- 559
- 560
- 561
- 562
- 563
- 564
- 565
- 566
- 567
- 568
- 569
- 570
- 571
- 572
- 573
- 574
- 575
- 576
- 577
- 578
- 579
- 580
- 581
- 582
- 583
- 584
- 585
- 586
- 587
- 588
- 589
- 590
- 591
- 592
- 593
- 594
- 595
- 596
- 597
- 598
- 599
- 600
- 601
- 602
- 603
- 604
- 605
- 606
- 607
- 608
- 609
- 610
- 611
- 612
- 613
- 614
- 615
- 616
- 617
- 618
- 619
- 620
- 621
- 622
- 623
- 624
- 625
- 626
- 627
- 628
- 629
- 630
- 631
- 632
- 633