Madhusudhan Chikkature | cfbc619 | 2008-11-12 13:27:11 -0800 | [diff] [blame] | 1 | /* |
| 2 | * drivers/w1/slaves/w1_bq27000.c |
| 3 | * |
| 4 | * Copyright (C) 2007 Texas Instruments, Inc. |
| 5 | * |
| 6 | * This file is licensed under the terms of the GNU General Public License |
| 7 | * version 2. This program is licensed "as is" without any warranty of any |
| 8 | * kind, whether express or implied. |
| 9 | * |
| 10 | */ |
| 11 | |
| 12 | #include <linux/kernel.h> |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/device.h> |
| 15 | #include <linux/types.h> |
| 16 | #include <linux/platform_device.h> |
| 17 | #include <linux/mutex.h> |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 18 | #include <linux/power/bq27xxx_battery.h> |
Madhusudhan Chikkature | cfbc619 | 2008-11-12 13:27:11 -0800 | [diff] [blame] | 19 | |
| 20 | #include "../w1.h" |
| 21 | #include "../w1_int.h" |
| 22 | #include "../w1_family.h" |
| 23 | |
| 24 | #define HDQ_CMD_READ (0) |
| 25 | #define HDQ_CMD_WRITE (1<<7) |
| 26 | |
| 27 | static int F_ID; |
| 28 | |
NeilBrown | 9f3519d | 2012-02-15 18:21:42 +0100 | [diff] [blame] | 29 | static int w1_bq27000_read(struct device *dev, unsigned int reg) |
Madhusudhan Chikkature | cfbc619 | 2008-11-12 13:27:11 -0800 | [diff] [blame] | 30 | { |
| 31 | u8 val; |
NeilBrown | 9f3519d | 2012-02-15 18:21:42 +0100 | [diff] [blame] | 32 | struct w1_slave *sl = container_of(dev->parent, struct w1_slave, dev); |
Madhusudhan Chikkature | cfbc619 | 2008-11-12 13:27:11 -0800 | [diff] [blame] | 33 | |
NeilBrown | b02f8be | 2012-05-18 15:59:52 +1000 | [diff] [blame] | 34 | mutex_lock(&sl->master->bus_mutex); |
Madhusudhan Chikkature | cfbc619 | 2008-11-12 13:27:11 -0800 | [diff] [blame] | 35 | w1_write_8(sl->master, HDQ_CMD_READ | reg); |
| 36 | val = w1_read_8(sl->master); |
NeilBrown | b02f8be | 2012-05-18 15:59:52 +1000 | [diff] [blame] | 37 | mutex_unlock(&sl->master->bus_mutex); |
Madhusudhan Chikkature | cfbc619 | 2008-11-12 13:27:11 -0800 | [diff] [blame] | 38 | |
| 39 | return val; |
| 40 | } |
NeilBrown | 9f3519d | 2012-02-15 18:21:42 +0100 | [diff] [blame] | 41 | |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 42 | static struct bq27xxx_platform_data bq27000_battery_info = { |
NeilBrown | 9f3519d | 2012-02-15 18:21:42 +0100 | [diff] [blame] | 43 | .read = w1_bq27000_read, |
| 44 | .name = "bq27000-battery", |
Andrew F. Davis | 424cfde | 2015-09-22 14:35:07 -0500 | [diff] [blame] | 45 | .chip = BQ27000, |
NeilBrown | 9f3519d | 2012-02-15 18:21:42 +0100 | [diff] [blame] | 46 | }; |
Madhusudhan Chikkature | cfbc619 | 2008-11-12 13:27:11 -0800 | [diff] [blame] | 47 | |
| 48 | static int w1_bq27000_add_slave(struct w1_slave *sl) |
| 49 | { |
| 50 | int ret; |
Madhusudhan Chikkature | cfbc619 | 2008-11-12 13:27:11 -0800 | [diff] [blame] | 51 | struct platform_device *pdev; |
| 52 | |
NeilBrown | 9f3519d | 2012-02-15 18:21:42 +0100 | [diff] [blame] | 53 | pdev = platform_device_alloc("bq27000-battery", -1); |
Madhusudhan Chikkature | cfbc619 | 2008-11-12 13:27:11 -0800 | [diff] [blame] | 54 | if (!pdev) { |
| 55 | ret = -ENOMEM; |
| 56 | return ret; |
| 57 | } |
NeilBrown | 9f3519d | 2012-02-15 18:21:42 +0100 | [diff] [blame] | 58 | ret = platform_device_add_data(pdev, |
| 59 | &bq27000_battery_info, |
| 60 | sizeof(bq27000_battery_info)); |
Wei Yongjun | 0ece1bb | 2013-04-30 15:28:35 -0700 | [diff] [blame] | 61 | if (ret) |
| 62 | goto pdev_add_failed; |
Madhusudhan Chikkature | cfbc619 | 2008-11-12 13:27:11 -0800 | [diff] [blame] | 63 | pdev->dev.parent = &sl->dev; |
| 64 | |
| 65 | ret = platform_device_add(pdev); |
| 66 | if (ret) |
| 67 | goto pdev_add_failed; |
| 68 | |
| 69 | dev_set_drvdata(&sl->dev, pdev); |
| 70 | |
| 71 | goto success; |
| 72 | |
| 73 | pdev_add_failed: |
Wei Yongjun | 0ece1bb | 2013-04-30 15:28:35 -0700 | [diff] [blame] | 74 | platform_device_put(pdev); |
Madhusudhan Chikkature | cfbc619 | 2008-11-12 13:27:11 -0800 | [diff] [blame] | 75 | success: |
| 76 | return ret; |
| 77 | } |
| 78 | |
| 79 | static void w1_bq27000_remove_slave(struct w1_slave *sl) |
| 80 | { |
| 81 | struct platform_device *pdev = dev_get_drvdata(&sl->dev); |
| 82 | |
| 83 | platform_device_unregister(pdev); |
| 84 | } |
| 85 | |
| 86 | static struct w1_family_ops w1_bq27000_fops = { |
| 87 | .add_slave = w1_bq27000_add_slave, |
| 88 | .remove_slave = w1_bq27000_remove_slave, |
| 89 | }; |
| 90 | |
| 91 | static struct w1_family w1_bq27000_family = { |
NeilBrown | 4b7e4f8 | 2014-11-19 14:04:20 +1100 | [diff] [blame] | 92 | .fid = W1_FAMILY_BQ27000, |
Madhusudhan Chikkature | cfbc619 | 2008-11-12 13:27:11 -0800 | [diff] [blame] | 93 | .fops = &w1_bq27000_fops, |
| 94 | }; |
| 95 | |
| 96 | static int __init w1_bq27000_init(void) |
| 97 | { |
| 98 | if (F_ID) |
| 99 | w1_bq27000_family.fid = F_ID; |
| 100 | |
| 101 | return w1_register_family(&w1_bq27000_family); |
| 102 | } |
| 103 | |
| 104 | static void __exit w1_bq27000_exit(void) |
| 105 | { |
| 106 | w1_unregister_family(&w1_bq27000_family); |
| 107 | } |
| 108 | |
| 109 | |
| 110 | module_init(w1_bq27000_init); |
| 111 | module_exit(w1_bq27000_exit); |
| 112 | |
| 113 | module_param(F_ID, int, S_IRUSR); |
| 114 | MODULE_PARM_DESC(F_ID, "1-wire slave FID for BQ device"); |
NeilBrown | 4b7e4f8 | 2014-11-19 14:04:20 +1100 | [diff] [blame] | 115 | MODULE_ALIAS("w1-family-" __stringify(W1_FAMILY_BQ27000)); |
Madhusudhan Chikkature | cfbc619 | 2008-11-12 13:27:11 -0800 | [diff] [blame] | 116 | MODULE_LICENSE("GPL"); |
| 117 | MODULE_AUTHOR("Texas Instruments Ltd"); |
| 118 | MODULE_DESCRIPTION("HDQ/1-wire slave driver bq27000 battery monitor chip"); |