├── .gitignore ├── LICENSE ├── Makefile ├── README.md ├── dkms.conf └── framework_laptop.c /.gitignore: -------------------------------------------------------------------------------- 1 | *.o 2 | *.ko 3 | *.mod.* 4 | *.mod 5 | modules.order 6 | Module.symvers 7 | .*.cmd 8 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Valid-License-Identifier: GPL-2.0 2 | Valid-License-Identifier: GPL-2.0-only 3 | Valid-License-Identifier: GPL-2.0+ 4 | Valid-License-Identifier: GPL-2.0-or-later 5 | SPDX-URL: https://spdx.org/licenses/GPL-2.0.html 6 | Usage-Guide: 7 | To use this license in source code, put one of the following SPDX 8 | tag/value pairs into a comment according to the placement 9 | guidelines in the licensing rules documentation. 10 | For 'GNU General Public License (GPL) version 2 only' use: 11 | SPDX-License-Identifier: GPL-2.0 12 | or 13 | SPDX-License-Identifier: GPL-2.0-only 14 | For 'GNU General Public License (GPL) version 2 or any later version' use: 15 | SPDX-License-Identifier: GPL-2.0+ 16 | or 17 | SPDX-License-Identifier: GPL-2.0-or-later 18 | License-Text: 19 | 20 | GNU GENERAL PUBLIC LICENSE 21 | Version 2, June 1991 22 | 23 | Copyright (C) 1989, 1991 Free Software Foundation, Inc. 24 | 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 25 | Everyone is permitted to copy and distribute verbatim copies 26 | of this license document, but changing it is not allowed. 27 | 28 | Preamble 29 | 30 | The licenses for most software are designed to take away your 31 | freedom to share and change it. By contrast, the GNU General Public 32 | License is intended to guarantee your freedom to share and change free 33 | software--to make sure the software is free for all its users. This 34 | General Public License applies to most of the Free Software 35 | Foundation's software and to any other program whose authors commit to 36 | using it. (Some other Free Software Foundation software is covered by 37 | the GNU Library General Public License instead.) You can apply it to 38 | your programs, too. 39 | 40 | When we speak of free software, we are referring to freedom, not 41 | price. Our General Public Licenses are designed to make sure that you 42 | have the freedom to distribute copies of free software (and charge for 43 | this service if you wish), that you receive source code or can get it 44 | if you want it, that you can change the software or use pieces of it 45 | in new free programs; and that you know you can do these things. 46 | 47 | To protect your rights, we need to make restrictions that forbid 48 | anyone to deny you these rights or to ask you to surrender the rights. 49 | These restrictions translate to certain responsibilities for you if you 50 | distribute copies of the software, or if you modify it. 51 | 52 | For example, if you distribute copies of such a program, whether 53 | gratis or for a fee, you must give the recipients all the rights that 54 | you have. You must make sure that they, too, receive or can get the 55 | source code. And you must show them these terms so they know their 56 | rights. 57 | 58 | We protect your rights with two steps: (1) copyright the software, and 59 | (2) offer you this license which gives you legal permission to copy, 60 | distribute and/or modify the software. 61 | 62 | Also, for each author's protection and ours, we want to make certain 63 | that everyone understands that there is no warranty for this free 64 | software. If the software is modified by someone else and passed on, we 65 | want its recipients to know that what they have is not the original, so 66 | that any problems introduced by others will not reflect on the original 67 | authors' reputations. 68 | 69 | Finally, any free program is threatened constantly by software 70 | patents. We wish to avoid the danger that redistributors of a free 71 | program will individually obtain patent licenses, in effect making the 72 | program proprietary. To prevent this, we have made it clear that any 73 | patent must be licensed for everyone's free use or not licensed at all. 74 | 75 | The precise terms and conditions for copying, distribution and 76 | modification follow. 77 | 78 | GNU GENERAL PUBLIC LICENSE 79 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 80 | 81 | 0. This License applies to any program or other work which contains 82 | a notice placed by the copyright holder saying it may be distributed 83 | under the terms of this General Public License. The "Program", below, 84 | refers to any such program or work, and a "work based on the Program" 85 | means either the Program or any derivative work under copyright law: 86 | that is to say, a work containing the Program or a portion of it, 87 | either verbatim or with modifications and/or translated into another 88 | language. (Hereinafter, translation is included without limitation in 89 | the term "modification".) Each licensee is addressed as "you". 90 | 91 | Activities other than copying, distribution and modification are not 92 | covered by this License; they are outside its scope. The act of 93 | running the Program is not restricted, and the output from the Program 94 | is covered only if its contents constitute a work based on the 95 | Program (independent of having been made by running the Program). 96 | Whether that is true depends on what the Program does. 97 | 98 | 1. You may copy and distribute verbatim copies of the Program's 99 | source code as you receive it, in any medium, provided that you 100 | conspicuously and appropriately publish on each copy an appropriate 101 | copyright notice and disclaimer of warranty; keep intact all the 102 | notices that refer to this License and to the absence of any warranty; 103 | and give any other recipients of the Program a copy of this License 104 | along with the Program. 105 | 106 | You may charge a fee for the physical act of transferring a copy, and 107 | you may at your option offer warranty protection in exchange for a fee. 108 | 109 | 2. You may modify your copy or copies of the Program or any portion 110 | of it, thus forming a work based on the Program, and copy and 111 | distribute such modifications or work under the terms of Section 1 112 | above, provided that you also meet all of these conditions: 113 | 114 | a) You must cause the modified files to carry prominent notices 115 | stating that you changed the files and the date of any change. 116 | 117 | b) You must cause any work that you distribute or publish, that in 118 | whole or in part contains or is derived from the Program or any 119 | part thereof, to be licensed as a whole at no charge to all third 120 | parties under the terms of this License. 121 | 122 | c) If the modified program normally reads commands interactively 123 | when run, you must cause it, when started running for such 124 | interactive use in the most ordinary way, to print or display an 125 | announcement including an appropriate copyright notice and a 126 | notice that there is no warranty (or else, saying that you provide 127 | a warranty) and that users may redistribute the program under 128 | these conditions, and telling the user how to view a copy of this 129 | License. (Exception: if the Program itself is interactive but 130 | does not normally print such an announcement, your work based on 131 | the Program is not required to print an announcement.) 132 | 133 | These requirements apply to the modified work as a whole. If 134 | identifiable sections of that work are not derived from the Program, 135 | and can be reasonably considered independent and separate works in 136 | themselves, then this License, and its terms, do not apply to those 137 | sections when you distribute them as separate works. But when you 138 | distribute the same sections as part of a whole which is a work based 139 | on the Program, the distribution of the whole must be on the terms of 140 | this License, whose permissions for other licensees extend to the 141 | entire whole, and thus to each and every part regardless of who wrote it. 142 | 143 | Thus, it is not the intent of this section to claim rights or contest 144 | your rights to work written entirely by you; rather, the intent is to 145 | exercise the right to control the distribution of derivative or 146 | collective works based on the Program. 147 | 148 | In addition, mere aggregation of another work not based on the Program 149 | with the Program (or with a work based on the Program) on a volume of 150 | a storage or distribution medium does not bring the other work under 151 | the scope of this License. 152 | 153 | 3. You may copy and distribute the Program (or a work based on it, 154 | under Section 2) in object code or executable form under the terms of 155 | Sections 1 and 2 above provided that you also do one of the following: 156 | 157 | a) Accompany it with the complete corresponding machine-readable 158 | source code, which must be distributed under the terms of Sections 159 | 1 and 2 above on a medium customarily used for software interchange; or, 160 | 161 | b) Accompany it with a written offer, valid for at least three 162 | years, to give any third party, for a charge no more than your 163 | cost of physically performing source distribution, a complete 164 | machine-readable copy of the corresponding source code, to be 165 | distributed under the terms of Sections 1 and 2 above on a medium 166 | customarily used for software interchange; or, 167 | 168 | c) Accompany it with the information you received as to the offer 169 | to distribute corresponding source code. (This alternative is 170 | allowed only for noncommercial distribution and only if you 171 | received the program in object code or executable form with such 172 | an offer, in accord with Subsection b above.) 173 | 174 | The source code for a work means the preferred form of the work for 175 | making modifications to it. For an executable work, complete source 176 | code means all the source code for all modules it contains, plus any 177 | associated interface definition files, plus the scripts used to 178 | control compilation and installation of the executable. However, as a 179 | special exception, the source code distributed need not include 180 | anything that is normally distributed (in either source or binary 181 | form) with the major components (compiler, kernel, and so on) of the 182 | operating system on which the executable runs, unless that component 183 | itself accompanies the executable. 184 | 185 | If distribution of executable or object code is made by offering 186 | access to copy from a designated place, then offering equivalent 187 | access to copy the source code from the same place counts as 188 | distribution of the source code, even though third parties are not 189 | compelled to copy the source along with the object code. 190 | 191 | 4. You may not copy, modify, sublicense, or distribute the Program 192 | except as expressly provided under this License. Any attempt 193 | otherwise to copy, modify, sublicense or distribute the Program is 194 | void, and will automatically terminate your rights under this License. 195 | However, parties who have received copies, or rights, from you under 196 | this License will not have their licenses terminated so long as such 197 | parties remain in full compliance. 198 | 199 | 5. You are not required to accept this License, since you have not 200 | signed it. However, nothing else grants you permission to modify or 201 | distribute the Program or its derivative works. These actions are 202 | prohibited by law if you do not accept this License. Therefore, by 203 | modifying or distributing the Program (or any work based on the 204 | Program), you indicate your acceptance of this License to do so, and 205 | all its terms and conditions for copying, distributing or modifying 206 | the Program or works based on it. 207 | 208 | 6. Each time you redistribute the Program (or any work based on the 209 | Program), the recipient automatically receives a license from the 210 | original licensor to copy, distribute or modify the Program subject to 211 | these terms and conditions. You may not impose any further 212 | restrictions on the recipients' exercise of the rights granted herein. 213 | You are not responsible for enforcing compliance by third parties to 214 | this License. 215 | 216 | 7. If, as a consequence of a court judgment or allegation of patent 217 | infringement or for any other reason (not limited to patent issues), 218 | conditions are imposed on you (whether by court order, agreement or 219 | otherwise) that contradict the conditions of this License, they do not 220 | excuse you from the conditions of this License. If you cannot 221 | distribute so as to satisfy simultaneously your obligations under this 222 | License and any other pertinent obligations, then as a consequence you 223 | may not distribute the Program at all. For example, if a patent 224 | license would not permit royalty-free redistribution of the Program by 225 | all those who receive copies directly or indirectly through you, then 226 | the only way you could satisfy both it and this License would be to 227 | refrain entirely from distribution of the Program. 228 | 229 | If any portion of this section is held invalid or unenforceable under 230 | any particular circumstance, the balance of the section is intended to 231 | apply and the section as a whole is intended to apply in other 232 | circumstances. 233 | 234 | It is not the purpose of this section to induce you to infringe any 235 | patents or other property right claims or to contest validity of any 236 | such claims; this section has the sole purpose of protecting the 237 | integrity of the free software distribution system, which is 238 | implemented by public license practices. Many people have made 239 | generous contributions to the wide range of software distributed 240 | through that system in reliance on consistent application of that 241 | system; it is up to the author/donor to decide if he or she is willing 242 | to distribute software through any other system and a licensee cannot 243 | impose that choice. 244 | 245 | This section is intended to make thoroughly clear what is believed to 246 | be a consequence of the rest of this License. 247 | 248 | 8. If the distribution and/or use of the Program is restricted in 249 | certain countries either by patents or by copyrighted interfaces, the 250 | original copyright holder who places the Program under this License 251 | may add an explicit geographical distribution limitation excluding 252 | those countries, so that distribution is permitted only in or among 253 | countries not thus excluded. In such case, this License incorporates 254 | the limitation as if written in the body of this License. 255 | 256 | 9. The Free Software Foundation may publish revised and/or new versions 257 | of the General Public License from time to time. Such new versions will 258 | be similar in spirit to the present version, but may differ in detail to 259 | address new problems or concerns. 260 | 261 | Each version is given a distinguishing version number. If the Program 262 | specifies a version number of this License which applies to it and "any 263 | later version", you have the option of following the terms and conditions 264 | either of that version or of any later version published by the Free 265 | Software Foundation. If the Program does not specify a version number of 266 | this License, you may choose any version ever published by the Free Software 267 | Foundation. 268 | 269 | 10. If you wish to incorporate parts of the Program into other free 270 | programs whose distribution conditions are different, write to the author 271 | to ask for permission. For software which is copyrighted by the Free 272 | Software Foundation, write to the Free Software Foundation; we sometimes 273 | make exceptions for this. Our decision will be guided by the two goals 274 | of preserving the free status of all derivatives of our free software and 275 | of promoting the sharing and reuse of software generally. 276 | 277 | NO WARRANTY 278 | 279 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 280 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 281 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 282 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 283 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 284 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 285 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 286 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 287 | REPAIR OR CORRECTION. 288 | 289 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 290 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 291 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 292 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 293 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 294 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 295 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 296 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 297 | POSSIBILITY OF SUCH DAMAGES. 298 | 299 | END OF TERMS AND CONDITIONS 300 | 301 | How to Apply These Terms to Your New Programs 302 | 303 | If you develop a new program, and you want it to be of the greatest 304 | possible use to the public, the best way to achieve this is to make it 305 | free software which everyone can redistribute and change under these terms. 306 | 307 | To do so, attach the following notices to the program. It is safest 308 | to attach them to the start of each source file to most effectively 309 | convey the exclusion of warranty; and each file should have at least 310 | the "copyright" line and a pointer to where the full notice is found. 311 | 312 | 313 | Copyright (C) 314 | 315 | This program is free software; you can redistribute it and/or modify 316 | it under the terms of the GNU General Public License as published by 317 | the Free Software Foundation; either version 2 of the License, or 318 | (at your option) any later version. 319 | 320 | This program is distributed in the hope that it will be useful, 321 | but WITHOUT ANY WARRANTY; without even the implied warranty of 322 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 323 | GNU General Public License for more details. 324 | 325 | You should have received a copy of the GNU General Public License 326 | along with this program; if not, write to the Free Software 327 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 328 | 329 | 330 | Also add information on how to contact you by electronic and paper mail. 331 | 332 | If the program is interactive, make it output a short notice like this 333 | when it starts in an interactive mode: 334 | 335 | Gnomovision version 69, Copyright (C) year name of author 336 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 337 | This is free software, and you are welcome to redistribute it 338 | under certain conditions; type `show c' for details. 339 | 340 | The hypothetical commands `show w' and `show c' should show the appropriate 341 | parts of the General Public License. Of course, the commands you use may 342 | be called something other than `show w' and `show c'; they could even be 343 | mouse-clicks or menu items--whatever suits your program. 344 | 345 | You should also get your employer (if you work as a programmer) or your 346 | school, if any, to sign a "copyright disclaimer" for the program, if 347 | necessary. Here is a sample; alter the names: 348 | 349 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 350 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 351 | 352 | , 1 April 1989 353 | Ty Coon, President of Vice 354 | 355 | This General Public License does not permit incorporating your program into 356 | proprietary programs. If your program is a subroutine library, you may 357 | consider it more useful to permit linking proprietary applications with the 358 | library. If this is what you want to do, use the GNU Library General 359 | Public License instead of this License. 360 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | ifneq ($(KERNELRELEASE),) 2 | # kbuild part of makefile 3 | obj-m := framework_laptop.o 4 | 5 | else 6 | # normal makefile 7 | KDIR ?= /lib/modules/`uname -r`/build 8 | 9 | modules: 10 | 11 | %: 12 | $(MAKE) -C $(KDIR) M=$$PWD $@ 13 | 14 | endif 15 | 16 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | A kernel module that exposes the Framework Laptop (13, 16)'s battery charge limit and LEDs to userspace. 2 | 3 | ## Install 4 | 5 | ### Packages 6 | 7 | On Arch Linux, this module is packaged in the Arch User Repository as 8 | [`framework-laptop-kmod-dkms-git`](https://aur.archlinux.org/packages/framework-laptop-kmod-dkms-git). 9 | You can install it for all your locally installed kernels with your favorite 10 | AUR helper: 11 | 12 | ```console 13 | $ yay -S framework-laptop-kmod-dkms-git 14 | ``` 15 | On Gentoo Linux, this module is packaged in the official Gentoo main repository as 16 | [`framework-laptop-kmod`](https://packages.gentoo.org/packages/app-laptop/framework-laptop-kmod). 17 | You can install it for your current kernel via: 18 | 19 | ```console 20 | $ emerge --ask framework-laptop-kmod 21 | ``` 22 | 23 | ### Building 24 | 25 | By default, this project will try to build a module for your running kernel. 26 | 27 | ```console 28 | $ make 29 | ``` 30 | 31 | If you need to target a different kernel, set `KDIR`: 32 | 33 | ```console 34 | $ make KDIR=/usr/src/linux-6.5 35 | ``` 36 | 37 | You can install the module systemwide with `make modules_install`. 38 | 39 | ## Usage 40 | 41 | If the module is installed systemwide, you can load it with 42 | `modprobe framework_laptop`. If you built it manually, you can also use 43 | `insmod ./framework_laptop.ko`. 44 | 45 | This module requires `cros_ec` and `cros_ec_lpcs` to be loaded and functional. 46 | 47 | > [!NOTE] 48 | > For the Framework Laptop 13 AMD Ryzen 7040 series and the Framework Laptop 16, 49 | > you will either need to apply [this patch series](https://lore.kernel.org/chrome-platform/20231005160701.19987-1-dustin@howett.net/) to your kernel sources, or run kernel version 6.10 or higher. 50 | 51 | ### Battery Charge Limit 52 | 53 | - Exposed via `charge_control_end_threshold`, available on `BAT1` 54 | - `/sys/class/power_supply/BAT1/charge_control_end_threshold` 55 | 56 | ### LEDs 57 | 58 | - `/sys/class/leds/framework_laptop::kbd_backlight` 59 | 60 | ### Fan Control 61 | 62 | This driver supports up to 4 fans, and creates a HWMON interface with the name `framework_laptop`. 63 | 64 | - `fan[1-4]_input` - Read fan speed in RPM (read-only) 65 | - `fan[1-4]_target` - Set target fan speed in RPM 66 | - read-write on the first fan, write-only on the others 67 | - `fan[1-4]_fault` - Fan removed indicator (read-only) 68 | - `fan[1-4]_alarm` - Fan stall indicator (read-only) 69 | - `pwm[1-4]` - Fan speed control in percent 0-100 (write-only) 70 | - `pwm[1-4]_enable` - Enable automatic fan control (write-only) 71 | - Currently you can write anything to enable, but writing `2` is recommended in case the driver is updated to support disabling automatic fan control. 72 | - Writing to the other interfaces will disable automatic fan control. 73 | - `pwm[1-4]_min` - returns 0 (read-only) 74 | - `pwm[1-4]_max` - returns 100 (read-only) 75 | 76 | ### Privacy Switches 77 | 78 | This driver exposes the privacy switches as a custom SysFS interface under `/sys/devices/platform/framework_laptop/framework_privacy`. 79 | It follows the [existing format of the `dell-privacy` driver](https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-platform-dell-privacy-wmi). 80 | -------------------------------------------------------------------------------- /dkms.conf: -------------------------------------------------------------------------------- 1 | # dkms.conf 2 | # 3 | # Allow DKMS to automatically build and install this module when a new kernel is installed. 4 | 5 | MAKE="'make' KDIR=/lib/modules/${kernelver}/build" 6 | CLEAN="'make' KDIR=/lib/modules/${kernelver}/build clean" 7 | PACKAGE_NAME=framework_laptop 8 | DEST_MODULE_LOCATION=/kernel/drivers/platform/x86 9 | BUILD_MODULE_NAME=framework_laptop 10 | PACKAGE_VERSION=1 11 | BUILD_MODULE_LOCATION=. 12 | 13 | -------------------------------------------------------------------------------- /framework_laptop.c: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: GPL-2.0+ 2 | /* 3 | * Framework Laptop ACPI Driver 4 | * 5 | * Copyright (C) 2022 Dustin L. Howett 6 | * 7 | * This program is free software; you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License version 2 as 9 | * published by the Free Software Foundation. 10 | */ 11 | 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | 30 | #include 31 | 32 | #define DRV_NAME "framework_laptop" 33 | #define FRAMEWORK_LAPTOP_EC_DEVICE_NAME "cros-ec-dev" 34 | 35 | static struct platform_device *fwdevice; 36 | static struct device *ec_device; 37 | struct framework_data { 38 | struct platform_device *pdev; 39 | struct led_classdev kb_led; 40 | struct device *hwmon_dev; 41 | }; 42 | 43 | #define EC_CMD_CHARGE_LIMIT_CONTROL 0x3E03 44 | 45 | enum ec_chg_limit_control_modes { 46 | /* Disable all setting, charge control by charge_manage */ 47 | CHG_LIMIT_DISABLE = BIT(0), 48 | /* Set maximum and minimum percentage */ 49 | CHG_LIMIT_SET_LIMIT = BIT(1), 50 | /* Host read current setting */ 51 | CHG_LIMIT_GET_LIMIT = BIT(3), 52 | /* Enable override mode, allow charge to full this time */ 53 | CHG_LIMIT_OVERRIDE = BIT(7), 54 | }; 55 | 56 | struct ec_params_ec_chg_limit_control { 57 | /* See enum ec_chg_limit_control_modes */ 58 | uint8_t modes; 59 | uint8_t max_percentage; 60 | uint8_t min_percentage; 61 | } __ec_align1; 62 | 63 | struct ec_response_chg_limit_control { 64 | uint8_t max_percentage; 65 | uint8_t min_percentage; 66 | } __ec_align1; 67 | 68 | #define EC_CMD_PRIVACY_SWITCHES_CHECK_MODE 0x3E14 69 | 70 | struct ec_response_privacy_switches_check { 71 | uint8_t microphone; 72 | uint8_t camera; 73 | } __ec_align1; 74 | 75 | static int charge_limit_control(enum ec_chg_limit_control_modes modes, uint8_t max_percentage) { 76 | struct { 77 | struct cros_ec_command msg; 78 | union { 79 | struct ec_params_ec_chg_limit_control params; 80 | struct ec_response_chg_limit_control resp; 81 | }; 82 | } __packed buf; 83 | struct ec_params_ec_chg_limit_control *params = &buf.params; 84 | struct ec_response_chg_limit_control *resp = &buf.resp; 85 | struct cros_ec_command *msg = &buf.msg; 86 | struct cros_ec_device *ec; 87 | int ret; 88 | 89 | if (!ec_device) 90 | return -ENODEV; 91 | 92 | ec = dev_get_drvdata(ec_device); 93 | 94 | memset(&buf, 0, sizeof(buf)); 95 | 96 | msg->version = 0; 97 | msg->command = EC_CMD_CHARGE_LIMIT_CONTROL; 98 | msg->outsize = sizeof(*params); 99 | msg->insize = sizeof(*resp); 100 | 101 | params->modes = modes; 102 | params->max_percentage = max_percentage; 103 | 104 | ret = cros_ec_cmd_xfer_status(ec, msg); 105 | if (ret < 0) { 106 | return -EIO; 107 | } 108 | 109 | return resp->max_percentage; 110 | } 111 | 112 | // Get the last set keyboard LED brightness 113 | static enum led_brightness kb_led_get(struct led_classdev *led) 114 | { 115 | struct { 116 | struct cros_ec_command msg; 117 | union { 118 | struct ec_params_pwm_get_duty p; 119 | struct ec_response_pwm_get_duty resp; 120 | }; 121 | } __packed buf; 122 | 123 | struct ec_params_pwm_get_duty *p = &buf.p; 124 | struct ec_response_pwm_get_duty *resp = &buf.resp; 125 | struct cros_ec_command *msg = &buf.msg; 126 | struct cros_ec_device *ec; 127 | int ret; 128 | if (!ec_device) 129 | goto out; 130 | 131 | ec = dev_get_drvdata(ec_device); 132 | 133 | memset(&buf, 0, sizeof(buf)); 134 | 135 | p->pwm_type = EC_PWM_TYPE_KB_LIGHT; 136 | 137 | msg->version = 0; 138 | msg->command = EC_CMD_PWM_GET_DUTY; 139 | msg->insize = sizeof(*resp); 140 | msg->outsize = sizeof(*p); 141 | 142 | ret = cros_ec_cmd_xfer_status(ec, msg); 143 | if (ret < 0) { 144 | goto out; 145 | } 146 | 147 | return resp->duty * 100 / EC_PWM_MAX_DUTY; 148 | 149 | out: 150 | return 0; 151 | } 152 | 153 | // Set the keyboard LED brightness 154 | static int kb_led_set(struct led_classdev *led, enum led_brightness value) 155 | { 156 | struct { 157 | struct cros_ec_command msg; 158 | union { 159 | struct ec_params_pwm_set_keyboard_backlight params; 160 | }; 161 | } __packed buf; 162 | 163 | struct ec_params_pwm_set_keyboard_backlight *params = &buf.params; 164 | struct cros_ec_command *msg = &buf.msg; 165 | struct cros_ec_device *ec; 166 | int ret; 167 | 168 | if (!ec_device) 169 | return -EIO; 170 | 171 | ec = dev_get_drvdata(ec_device); 172 | 173 | memset(&buf, 0, sizeof(buf)); 174 | 175 | msg->version = 0; 176 | msg->command = EC_CMD_PWM_SET_KEYBOARD_BACKLIGHT; 177 | msg->insize = 0; 178 | msg->outsize = sizeof(*params); 179 | 180 | params->percent = value; 181 | 182 | ret = cros_ec_cmd_xfer_status(ec, msg); 183 | if (ret < 0) { 184 | return -EIO; 185 | } 186 | 187 | return 0; 188 | } 189 | 190 | 191 | static ssize_t battery_get_threshold(char *buf) 192 | { 193 | int ret; 194 | 195 | ret = charge_limit_control(CHG_LIMIT_GET_LIMIT, 0); 196 | if (ret < 0) 197 | return ret; 198 | 199 | return sysfs_emit(buf, "%d\n", (int)ret); 200 | } 201 | 202 | static ssize_t battery_set_threshold(const char *buf, size_t count) 203 | { 204 | int ret; 205 | int value; 206 | 207 | ret = kstrtouint(buf, 10, &value); 208 | if (ret) 209 | return ret; 210 | 211 | if (value > 100) 212 | return -EINVAL; 213 | 214 | ret = charge_limit_control(CHG_LIMIT_SET_LIMIT, (uint8_t)value); 215 | if (ret < 0) 216 | return ret; 217 | 218 | return count; 219 | } 220 | 221 | static ssize_t charge_control_end_threshold_show(struct device *dev, 222 | struct device_attribute *attr, char *buf) 223 | { 224 | return battery_get_threshold(buf); 225 | } 226 | 227 | static ssize_t charge_control_end_threshold_store(struct device *dev, 228 | struct device_attribute *attr, const char *buf, size_t count) 229 | { 230 | return battery_set_threshold(buf, count); 231 | } 232 | 233 | static DEVICE_ATTR_RW(charge_control_end_threshold); 234 | 235 | static struct attribute *framework_laptop_battery_attrs[] = { 236 | &dev_attr_charge_control_end_threshold.attr, 237 | NULL, 238 | }; 239 | 240 | ATTRIBUTE_GROUPS(framework_laptop_battery); 241 | 242 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 2, 0) 243 | static int framework_laptop_battery_add(struct power_supply *battery, struct acpi_battery_hook *hook) 244 | #else 245 | static int framework_laptop_battery_add(struct power_supply *battery) 246 | #endif 247 | { 248 | // Framework EC only supports 1 battery 249 | if (strcmp(battery->desc->name, "BAT1") != 0) 250 | return -ENODEV; 251 | 252 | if (device_add_groups(&battery->dev, framework_laptop_battery_groups)) 253 | return -ENODEV; 254 | 255 | return 0; 256 | } 257 | 258 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 2, 0) 259 | static int framework_laptop_battery_remove(struct power_supply *battery, struct acpi_battery_hook *hook) 260 | #else 261 | static int framework_laptop_battery_remove(struct power_supply *battery) 262 | #endif 263 | { 264 | device_remove_groups(&battery->dev, framework_laptop_battery_groups); 265 | return 0; 266 | } 267 | 268 | // --- fanN_input --- 269 | // Read the current fan speed from the EC's memory 270 | static ssize_t ec_get_fan_speed(u8 idx, u16 *val) 271 | { 272 | if (!ec_device) 273 | return -ENODEV; 274 | 275 | struct cros_ec_device *ec = dev_get_drvdata(ec_device); 276 | 277 | const u8 offset = EC_MEMMAP_FAN + 2 * idx; 278 | 279 | return ec->cmd_readmem(ec, offset, sizeof(*val), val); 280 | } 281 | 282 | static ssize_t fw_fan_speed_show(struct device *dev, 283 | struct device_attribute *attr, char *buf) 284 | { 285 | struct sensor_device_attribute *sen_attr = to_sensor_dev_attr(attr); 286 | 287 | u16 val; 288 | if (ec_get_fan_speed(sen_attr->index, &val) < 0) { 289 | return -EIO; 290 | } 291 | 292 | if (val == EC_FAN_SPEED_NOT_PRESENT || val == EC_FAN_SPEED_STALLED) { 293 | return sysfs_emit(buf, "%u\n", 0); 294 | } 295 | 296 | // Format as string for sysfs 297 | return sysfs_emit(buf, "%u\n", val); 298 | } 299 | 300 | // --- fanN_target --- 301 | static ssize_t ec_set_target_rpm(u8 idx, u32 *val) 302 | { 303 | int ret; 304 | if (!ec_device) 305 | return -ENODEV; 306 | 307 | struct cros_ec_device *ec = dev_get_drvdata(ec_device); 308 | 309 | struct ec_params_pwm_set_fan_target_rpm_v1 params = { 310 | .rpm = *val, 311 | .fan_idx = idx, 312 | }; 313 | 314 | ret = cros_ec_cmd(ec, 1, EC_CMD_PWM_SET_FAN_TARGET_RPM, ¶ms, 315 | sizeof(params), NULL, 0); 316 | if (ret < 0) 317 | return -EIO; 318 | 319 | return 0; 320 | } 321 | 322 | static ssize_t ec_get_target_rpm(u8 idx, u32 *val) 323 | { 324 | int ret; 325 | if (!ec_device) 326 | return -ENODEV; 327 | 328 | struct cros_ec_device *ec = dev_get_drvdata(ec_device); 329 | 330 | struct ec_response_pwm_get_fan_rpm resp; 331 | 332 | // index isn't supported, it should only return fan 0's target 333 | 334 | ret = cros_ec_cmd(ec, 0, EC_CMD_PWM_GET_FAN_TARGET_RPM, NULL, 0, &resp, 335 | sizeof(resp)); 336 | if (ret < 0) 337 | return -EIO; 338 | 339 | *val = resp.rpm; 340 | 341 | return 0; 342 | } 343 | 344 | static ssize_t fw_fan_target_store(struct device *dev, 345 | struct device_attribute *attr, 346 | const char *buf, size_t count) 347 | { 348 | struct sensor_device_attribute *sen_attr = to_sensor_dev_attr(attr); 349 | u32 val; 350 | 351 | int err; 352 | err = kstrtou32(buf, 10, &val); 353 | if (err < 0) 354 | return err; 355 | 356 | if (ec_set_target_rpm(sen_attr->index, &val) < 0) { 357 | return -EIO; 358 | } 359 | 360 | return count; 361 | } 362 | 363 | static ssize_t fw_fan_target_show(struct device *dev, 364 | struct device_attribute *attr, char *buf) 365 | { 366 | struct sensor_device_attribute *sen_attr = to_sensor_dev_attr(attr); 367 | 368 | // Only fan 0 is supported 369 | if (sen_attr->index != 0) { 370 | return -EINVAL; 371 | } 372 | 373 | u32 val; 374 | if (ec_get_target_rpm(sen_attr->index, &val) < 0) { 375 | return -EIO; 376 | } 377 | 378 | // Format as string for sysfs 379 | return sysfs_emit(buf, "%u\n", val); 380 | } 381 | 382 | // --- fanN_fault --- 383 | static ssize_t fw_fan_fault_show(struct device *dev, 384 | struct device_attribute *attr, char *buf) 385 | { 386 | struct sensor_device_attribute *sen_attr = to_sensor_dev_attr(attr); 387 | 388 | u16 val; 389 | if (ec_get_fan_speed(sen_attr->index, &val) < 0) { 390 | return -EIO; 391 | } 392 | 393 | // Format as string for sysfs 394 | return sysfs_emit(buf, "%u\n", val == EC_FAN_SPEED_NOT_PRESENT); 395 | } 396 | 397 | // --- fanN_alarm --- 398 | static ssize_t fw_fan_alarm_show(struct device *dev, 399 | struct device_attribute *attr, char *buf) 400 | { 401 | struct sensor_device_attribute *sen_attr = to_sensor_dev_attr(attr); 402 | 403 | u16 val; 404 | if (ec_get_fan_speed(sen_attr->index, &val) < 0) { 405 | return -EIO; 406 | } 407 | 408 | // Format as string for sysfs 409 | return sysfs_emit(buf, "%u\n", val == EC_FAN_SPEED_STALLED); 410 | } 411 | 412 | // --- pwmN_enable --- 413 | static ssize_t ec_set_auto_fan_ctrl(u8 idx) 414 | { 415 | int ret; 416 | if (!ec_device) 417 | return -ENODEV; 418 | 419 | struct cros_ec_device *ec = dev_get_drvdata(ec_device); 420 | 421 | struct ec_params_auto_fan_ctrl_v1 params = { 422 | .fan_idx = idx, 423 | }; 424 | 425 | ret = cros_ec_cmd(ec, 1, EC_CMD_THERMAL_AUTO_FAN_CTRL, ¶ms, 426 | sizeof(params), NULL, 0); 427 | if (ret < 0) 428 | return -EIO; 429 | 430 | return 0; 431 | } 432 | 433 | static ssize_t fw_pwm_enable_store(struct device *dev, 434 | struct device_attribute *attr, 435 | const char *buf, size_t count) 436 | { 437 | struct sensor_device_attribute *sen_attr = to_sensor_dev_attr(attr); 438 | 439 | // The EC doesn't take any arguments for this command, 440 | // so we don't need to parse the buffer 441 | // u32 val; 442 | 443 | // int err; 444 | // err = kstrtou32(buf, 10, &val); 445 | // if (err < 0) 446 | // return err; 447 | 448 | if (ec_set_auto_fan_ctrl(sen_attr->index) < 0) { 449 | return -EIO; 450 | } 451 | 452 | return count; 453 | } 454 | 455 | // --- pwmN --- 456 | static ssize_t ec_set_fan_duty(u8 idx, u32 *val) 457 | { 458 | int ret; 459 | if (!ec_device) 460 | return -ENODEV; 461 | 462 | struct cros_ec_device *ec = dev_get_drvdata(ec_device); 463 | 464 | struct ec_params_pwm_set_fan_duty_v1 params = { 465 | .percent = *val, 466 | .fan_idx = idx, 467 | }; 468 | 469 | ret = cros_ec_cmd(ec, 1, EC_CMD_PWM_SET_FAN_DUTY, ¶ms, 470 | sizeof(params), NULL, 0); 471 | if (ret < 0) 472 | return -EIO; 473 | 474 | return 0; 475 | } 476 | 477 | static ssize_t fw_pwm_store(struct device *dev, struct device_attribute *attr, 478 | const char *buf, size_t count) 479 | { 480 | struct sensor_device_attribute *sen_attr = to_sensor_dev_attr(attr); 481 | u32 val; 482 | 483 | int err; 484 | err = kstrtou32(buf, 10, &val); 485 | if (err < 0) 486 | return err; 487 | 488 | if (ec_set_fan_duty(sen_attr->index, &val) < 0) { 489 | return -EIO; 490 | } 491 | 492 | return count; 493 | } 494 | 495 | static ssize_t fw_pwm_min_show(struct device *dev, 496 | struct device_attribute *attr, char *buf) 497 | { 498 | return sysfs_emit(buf, "%i\n", 0); 499 | } 500 | 501 | static ssize_t fw_pwm_max_show(struct device *dev, 502 | struct device_attribute *attr, char *buf) 503 | { 504 | return sysfs_emit(buf, "%i\n", 100); 505 | } 506 | 507 | static ssize_t ec_count_fans(size_t *val) 508 | { 509 | if (!ec_device) 510 | return -ENODEV; 511 | 512 | struct cros_ec_device *ec = dev_get_drvdata(ec_device); 513 | 514 | u16 fans[EC_FAN_SPEED_ENTRIES]; 515 | 516 | int ret = ec->cmd_readmem(ec, EC_MEMMAP_FAN, sizeof(fans), fans); 517 | if (ret < 0) 518 | return -EIO; 519 | 520 | for (size_t i = 0; i < EC_FAN_SPEED_ENTRIES; i++) { 521 | if (fans[i] == EC_FAN_SPEED_NOT_PRESENT) { 522 | *val = i; 523 | return 0; 524 | } 525 | } 526 | 527 | *val = EC_FAN_SPEED_ENTRIES; 528 | return 0; 529 | } 530 | 531 | // --- framework_privacy --- 532 | static ssize_t framework_privacy_show(struct device *dev, 533 | struct device_attribute *attr, char *buf) 534 | { 535 | int ret; 536 | if (!ec_device) 537 | return -ENODEV; 538 | 539 | struct cros_ec_device *ec = dev_get_drvdata(ec_device); 540 | 541 | struct ec_response_privacy_switches_check resp; 542 | 543 | ret = cros_ec_cmd(ec, 0, EC_CMD_PRIVACY_SWITCHES_CHECK_MODE, NULL, 0, 544 | &resp, sizeof(resp)); 545 | if (ret < 0) 546 | return -EIO; 547 | 548 | // Output following dell-privacy's format 549 | return sysfs_emit(buf, "[Microphone] [%s]\n[Camera] [%s]\n", 550 | resp.microphone ? "unmuted" : "muted", 551 | resp.camera ? "unmuted" : "muted"); 552 | } 553 | 554 | #define FW_ATTRS_PER_FAN 8 555 | 556 | // --- hwmon sysfs attributes --- 557 | // clang-format off 558 | static SENSOR_DEVICE_ATTR_RO(fan1_input, fw_fan_speed, 0); // Fan Reading 559 | static SENSOR_DEVICE_ATTR_RW(fan1_target, fw_fan_target, 0); // Target RPM (RW on fan 0 only) 560 | static SENSOR_DEVICE_ATTR_RO(fan1_fault, fw_fan_fault, 0); // Fan Not Present 561 | static SENSOR_DEVICE_ATTR_RO(fan1_alarm, fw_fan_alarm, 0); // Fan Stalled 562 | static SENSOR_DEVICE_ATTR_WO(pwm1_enable, fw_pwm_enable, 0); // Set Fan Control Mode 563 | static SENSOR_DEVICE_ATTR_WO(pwm1, fw_pwm, 0); // Set Fan Speed 564 | static SENSOR_DEVICE_ATTR_RO(pwm1_min, fw_pwm_min, 0); // Min Fan Speed 565 | static SENSOR_DEVICE_ATTR_RO(pwm1_max, fw_pwm_max, 0); // Max Fan Speed 566 | // clang-format on 567 | 568 | static SENSOR_DEVICE_ATTR_RO(fan2_input, fw_fan_speed, 1); 569 | static SENSOR_DEVICE_ATTR_WO(fan2_target, fw_fan_target, 1); 570 | static SENSOR_DEVICE_ATTR_RO(fan2_fault, fw_fan_fault, 1); 571 | static SENSOR_DEVICE_ATTR_RO(fan2_alarm, fw_fan_alarm, 1); 572 | static SENSOR_DEVICE_ATTR_WO(pwm2_enable, fw_pwm_enable, 1); 573 | static SENSOR_DEVICE_ATTR_WO(pwm2, fw_pwm, 1); 574 | static SENSOR_DEVICE_ATTR_RO(pwm2_min, fw_pwm_min, 1); 575 | static SENSOR_DEVICE_ATTR_RO(pwm2_max, fw_pwm_max, 1); 576 | 577 | static SENSOR_DEVICE_ATTR_RO(fan3_input, fw_fan_speed, 2); 578 | static SENSOR_DEVICE_ATTR_WO(fan3_target, fw_fan_target, 2); 579 | static SENSOR_DEVICE_ATTR_RO(fan3_fault, fw_fan_fault, 2); 580 | static SENSOR_DEVICE_ATTR_RO(fan3_alarm, fw_fan_alarm, 2); 581 | static SENSOR_DEVICE_ATTR_WO(pwm3_enable, fw_pwm_enable, 2); 582 | static SENSOR_DEVICE_ATTR_WO(pwm3, fw_pwm, 2); 583 | static SENSOR_DEVICE_ATTR_RO(pwm3_min, fw_pwm_min, 2); 584 | static SENSOR_DEVICE_ATTR_RO(pwm3_max, fw_pwm_max, 2); 585 | 586 | static SENSOR_DEVICE_ATTR_RO(fan4_input, fw_fan_speed, 3); 587 | static SENSOR_DEVICE_ATTR_WO(fan4_target, fw_fan_target, 3); 588 | static SENSOR_DEVICE_ATTR_RO(fan4_fault, fw_fan_fault, 3); 589 | static SENSOR_DEVICE_ATTR_RO(fan4_alarm, fw_fan_alarm, 3); 590 | static SENSOR_DEVICE_ATTR_WO(pwm4_enable, fw_pwm_enable, 3); 591 | static SENSOR_DEVICE_ATTR_WO(pwm4, fw_pwm, 3); 592 | static SENSOR_DEVICE_ATTR_RO(pwm4_min, fw_pwm_min, 3); 593 | static SENSOR_DEVICE_ATTR_RO(pwm4_max, fw_pwm_max, 3); 594 | 595 | static struct attribute 596 | *fw_hwmon_attrs[(EC_FAN_SPEED_ENTRIES * FW_ATTRS_PER_FAN) + 1] = { 597 | &sensor_dev_attr_fan1_input.dev_attr.attr, 598 | &sensor_dev_attr_fan1_target.dev_attr.attr, 599 | &sensor_dev_attr_fan1_fault.dev_attr.attr, 600 | &sensor_dev_attr_fan1_alarm.dev_attr.attr, 601 | &sensor_dev_attr_pwm1_enable.dev_attr.attr, 602 | &sensor_dev_attr_pwm1.dev_attr.attr, 603 | &sensor_dev_attr_pwm1_min.dev_attr.attr, 604 | &sensor_dev_attr_pwm1_max.dev_attr.attr, 605 | 606 | &sensor_dev_attr_fan2_input.dev_attr.attr, 607 | &sensor_dev_attr_fan2_target.dev_attr.attr, 608 | &sensor_dev_attr_fan2_fault.dev_attr.attr, 609 | &sensor_dev_attr_fan2_alarm.dev_attr.attr, 610 | &sensor_dev_attr_pwm2_enable.dev_attr.attr, 611 | &sensor_dev_attr_pwm2.dev_attr.attr, 612 | &sensor_dev_attr_pwm2_min.dev_attr.attr, 613 | &sensor_dev_attr_pwm2_max.dev_attr.attr, 614 | 615 | &sensor_dev_attr_fan3_input.dev_attr.attr, 616 | &sensor_dev_attr_fan3_target.dev_attr.attr, 617 | &sensor_dev_attr_fan3_fault.dev_attr.attr, 618 | &sensor_dev_attr_fan3_alarm.dev_attr.attr, 619 | &sensor_dev_attr_pwm3_enable.dev_attr.attr, 620 | &sensor_dev_attr_pwm3.dev_attr.attr, 621 | &sensor_dev_attr_pwm3_min.dev_attr.attr, 622 | &sensor_dev_attr_pwm3_max.dev_attr.attr, 623 | 624 | &sensor_dev_attr_fan4_input.dev_attr.attr, 625 | &sensor_dev_attr_fan4_target.dev_attr.attr, 626 | &sensor_dev_attr_fan4_fault.dev_attr.attr, 627 | &sensor_dev_attr_fan4_alarm.dev_attr.attr, 628 | &sensor_dev_attr_pwm4_enable.dev_attr.attr, 629 | &sensor_dev_attr_pwm4.dev_attr.attr, 630 | &sensor_dev_attr_pwm4_min.dev_attr.attr, 631 | &sensor_dev_attr_pwm4_max.dev_attr.attr, 632 | 633 | NULL, 634 | }; 635 | 636 | ATTRIBUTE_GROUPS(fw_hwmon); 637 | 638 | // --- generic sysfs attributes --- 639 | static DEVICE_ATTR_RO(framework_privacy); 640 | 641 | static struct attribute *framework_laptop_attrs[] = { 642 | &dev_attr_framework_privacy.attr, 643 | NULL, 644 | }; 645 | 646 | ATTRIBUTE_GROUPS(framework_laptop); 647 | 648 | // --- platform driver --- 649 | static struct acpi_battery_hook framework_laptop_battery_hook = { 650 | .add_battery = framework_laptop_battery_add, 651 | .remove_battery = framework_laptop_battery_remove, 652 | .name = "Framework Laptop Battery Extension", 653 | }; 654 | 655 | static const struct acpi_device_id device_ids[] = { 656 | {"FRMW0001", 0}, 657 | {"FRMW0004", 0}, 658 | {"", 0}, 659 | }; 660 | MODULE_DEVICE_TABLE(acpi, device_ids); 661 | 662 | static const struct dmi_system_id framework_laptop_dmi_table[] __initconst = { 663 | { 664 | /* the Framework Laptop */ 665 | .matches = { 666 | DMI_MATCH(DMI_SYS_VENDOR, "Framework"), 667 | DMI_MATCH(DMI_PRODUCT_NAME, "Laptop"), 668 | }, 669 | }, 670 | { /* sentinel */ } 671 | }; 672 | MODULE_DEVICE_TABLE(dmi, framework_laptop_dmi_table); 673 | 674 | static int device_match_cros_ec(struct device *dev, const void* foo) { 675 | const char* name = dev_name(dev); 676 | if (strncmp(name, "cros-ec-dev", 11)) 677 | return 0; 678 | return 1; 679 | } 680 | 681 | static int framework_probe(struct platform_device *pdev) 682 | { 683 | struct device *dev; 684 | struct framework_data *data; 685 | int ret = 0; 686 | 687 | dev = &pdev->dev; 688 | 689 | ec_device = bus_find_device(&platform_bus_type, NULL, NULL, device_match_cros_ec); 690 | if (!ec_device) { 691 | dev_err(dev, DRV_NAME ": failed to find EC %s.\n", FRAMEWORK_LAPTOP_EC_DEVICE_NAME); 692 | return -EINVAL; 693 | } 694 | ec_device = ec_device->parent; 695 | 696 | data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); 697 | if (!data) 698 | return -ENOMEM; 699 | 700 | platform_set_drvdata(pdev, data); 701 | data->pdev = pdev; 702 | 703 | data->kb_led.name = DRV_NAME "::kbd_backlight"; 704 | data->kb_led.brightness_get = kb_led_get; 705 | data->kb_led.brightness_set_blocking = kb_led_set; 706 | data->kb_led.max_brightness = 100; 707 | ret = devm_led_classdev_register(&pdev->dev, &data->kb_led); 708 | if (ret) 709 | return ret; 710 | 711 | #if 0 712 | /* Register the driver */ 713 | ret = platform_driver_register(&cros_ec_lpc_driver); 714 | if (ret) { 715 | pr_err(DRV_NAME ": can't register driver: %d\n", ret); 716 | return ret; 717 | } 718 | 719 | /* Register the device, and it'll get hooked up automatically */ 720 | ret = platform_device_register(&cros_ec_lpc_device); 721 | if (ret) { 722 | pr_err(DRV_NAME ": can't register device: %d\n", ret); 723 | platform_driver_unregister(&cros_ec_lpc_driver); 724 | } 725 | #endif 726 | 727 | struct cros_ec_device *ec = dev_get_drvdata(ec_device); 728 | if (ec->cmd_readmem) { 729 | // Count the number of fans 730 | size_t fan_count; 731 | if (ec_count_fans(&fan_count) < 0) { 732 | dev_err(dev, DRV_NAME ": failed to count fans.\n"); 733 | return -EINVAL; 734 | } 735 | // NULL terminates the list after the last detected fan 736 | fw_hwmon_attrs[fan_count * FW_ATTRS_PER_FAN] = NULL; 737 | 738 | data->hwmon_dev = hwmon_device_register_with_groups( 739 | dev, DRV_NAME, NULL, fw_hwmon_groups); 740 | if (IS_ERR(data->hwmon_dev)) 741 | return PTR_ERR(data->hwmon_dev); 742 | 743 | } else { 744 | dev_err(dev, DRV_NAME ": fan readings could not be enabled for this EC %s.\n", 745 | FRAMEWORK_LAPTOP_EC_DEVICE_NAME); 746 | } 747 | 748 | battery_hook_register(&framework_laptop_battery_hook); 749 | 750 | return ret; 751 | } 752 | 753 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 11, 0) 754 | static void framework_remove(struct platform_device *pdev) 755 | #else 756 | static int framework_remove(struct platform_device *pdev) 757 | #endif 758 | { 759 | struct framework_data *data; 760 | 761 | data = (struct framework_data *)platform_get_drvdata(pdev); 762 | 763 | battery_hook_unregister(&framework_laptop_battery_hook); 764 | 765 | // Make sure it's not null before we try to unregister it 766 | if (data && data->hwmon_dev) 767 | hwmon_device_unregister(data->hwmon_dev); 768 | 769 | put_device(ec_device); 770 | 771 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 11, 0) 772 | return; 773 | #else 774 | return 0; 775 | #endif 776 | } 777 | 778 | static struct platform_driver framework_driver = { 779 | .driver = { 780 | .name = DRV_NAME, 781 | .acpi_match_table = device_ids, 782 | .dev_groups = framework_laptop_groups, 783 | }, 784 | .probe = framework_probe, 785 | .remove = framework_remove, 786 | }; 787 | 788 | static int __init framework_laptop_init(void) 789 | { 790 | int ret; 791 | 792 | if (!dmi_check_system(framework_laptop_dmi_table)) { 793 | pr_err(DRV_NAME ": unsupported system.\n"); 794 | return -ENODEV; 795 | } 796 | 797 | ret = platform_driver_register(&framework_driver); 798 | if (ret) 799 | goto fail; 800 | 801 | fwdevice = platform_device_alloc(DRV_NAME, PLATFORM_DEVID_NONE); 802 | if (!fwdevice) 803 | { 804 | ret = -ENOMEM; 805 | goto fail_platform_driver; 806 | } 807 | 808 | ret = platform_device_add(fwdevice); 809 | if (ret) 810 | goto fail_device_add; 811 | 812 | return 0; 813 | 814 | fail_device_add: 815 | platform_device_del(fwdevice); 816 | fwdevice = NULL; 817 | 818 | fail_platform_driver: 819 | platform_driver_unregister(&framework_driver); 820 | 821 | fail: 822 | return ret; 823 | } 824 | 825 | static void __exit framework_laptop_exit(void) 826 | { 827 | if (fwdevice) 828 | { 829 | platform_device_unregister(fwdevice); 830 | platform_driver_unregister(&framework_driver); 831 | } 832 | } 833 | 834 | module_init(framework_laptop_init); 835 | module_exit(framework_laptop_exit); 836 | 837 | MODULE_DESCRIPTION("Framework Laptop Platform Driver"); 838 | MODULE_AUTHOR("Dustin L. Howett "); 839 | MODULE_LICENSE("GPL"); 840 | MODULE_ALIAS("platform:" DRV_NAME); 841 | MODULE_SOFTDEP("pre: cros_ec_lpcs"); 842 | --------------------------------------------------------------------------------